0

This is my code to append an integer in linked list.

#include "iostream"
using namespace std;

class LinkList
{
private:
      struct Node
      {
        int data;
        Node* link;
      }*p;

public:
      LinkList();
      ~LinkList();

       void Print();         // Prints the contents of linkedlist
       void Append(int num); // Adds a new node at the end of the linkedlist
       void Delete(int num); // Deletes the specified node from the linkedlist

       void AddatBeg(int num);// Adds a new node at the beginning of the linkedlist
       void AddAfter(int c, int num); // Adds a new node after specified number of nodes
       int Count();          // Counts number of nodes present in the linkedlist

};

LinkList::LinkList()
{
  p = NULL;
}

LinkList::~LinkList()
{
  if (p == NULL)
        return;

  Node* tmp;
  while(p != NULL)
  {
       tmp = p->link ;
     delete p;
       p = tmp;
  }
}

// Prints the contents of linkedlist
void LinkList::Print()
{
  if (p == NULL)
  {
        cout<< "EMPTY";
        return;
  }

  //Traverse
  Node* tmp = p;
  while(tmp != NULL)
  {
       cout<<tmp->data<<endl;
       tmp = tmp->link ;
  }
}

// Adds a new node at the end of the linkedlist
void LinkList::Append(int num)
{
      Node *newNode;

      newNode = new Node;
      newNode->data = num;
      newNode->link = NULL;

       if(p == NULL)
      {
       //create first node
         p = newNode;
      }
       else
      {
             //Traverse
            Node *tmp = p;
             while(tmp->link != NULL)
            {
                  tmp = tmp->link;
            }

             //add node to the end
        tmp->link = newNode;
      }
}

// Deletes the specified node from the linkedlist
void LinkList::Delete( int num )
{
   Node *tmp;

   tmp = p;
   //If node to be delete is first node
   if( tmp->data == num )
   {
      p = tmp->link;
      delete tmp;
      return;
   }

   // traverse list till the last but one node is reached
   Node *tmp2 = tmp;
   while( tmp!=NULL )
   {
      if( tmp->data == num )
      {
         tmp2->link = tmp->link;
         delete tmp;
         return;
      }

      tmp2 = tmp;
      tmp = tmp->link;
   }
   cout<< "\nElement "<<num<<" not Found." ;
}

int LinkList::Count()
{
      Node *tmp;
       int c = 0;

       //Traverse the entire Linked List
       for (tmp = p; tmp != NULL; tmp = tmp->link)
            c++;

       return (c);
}

int main()
{
      LinkList* pobj = new LinkList();
      pobj->Append(11);
      pobj->Print();

       delete pobj;
       return 0;
}

What I am looking for is a code where I can insert elements of arrays in linked list. For example, if there are two arrays containing elements (1,2,3) and (4,5,6), Code should create two Linked lists and the address of first node of each linked list should be stored in an array so that running a for loop would print all linked list in sequence. Example:

Linked List 1 = (1,2,3)
Linked List 2 = (4,5,6)

Number of arrays and number of elements inside arrays will be dynamic.

sajal
  • 69
  • 1
  • 13
  • 1
    Why do you want linked lists? (evidence suggests they're *rarely* useful). If you really *must* use linked lists, is there some reason you can't use `std:::list`? – Jerry Coffin Jul 17 '13 at 07:33
  • This question appears to be off-topic and belongs on http://codereview.stackexchange.com/ – TemplateRex Jul 17 '13 at 08:40
  • @JerryCoffin : The whole idea is to store a list of phone numbers in memory and search any number in the fastest possible way. – sajal Jul 17 '13 at 08:58
  • @spiritwolfform : I tried storing them in an array but was getting error as I was simply trying to store the address in an int array. – sajal Jul 17 '13 at 08:59
  • 2
    @sajal "The whole idea is to store a list of phone numbers in memory and search any number in the fastest possible way" for this how can linked list be useful can you please elaborate more?? I think 2-d arrays would helped you better as they provide random access to all the elements unlike Linked list, you can use vectors(in c++) or create your own arrays that will grow dynamically if required ( like vector grows) This way your search will be fast as you can apply a lot of algorithms and your access will be random – Sanyam Goel Jul 17 '13 at 09:12
  • @SanyamGoel: Thanks for the suggestion. But even if I use vector, I want to store all the vectors in an array ad then display them sequentially. – sajal Jul 17 '13 at 12:33

2 Answers2

2

NOTE: I suppose that your question is for learning purposes. If not, please not implement your own linked list, use std::vector. Prefer vector to std::list, linked lists have less performance than vectors, because cache misses. Check this question about performance comparisons between std::list and syd::vector.

Linked lists are commonly implemented as double-linked lists, to make push_back and pop_back operations O(1) (That is, no traverse needed for push_back).

The way to implement this is storing in the node not only a link to the next node, also a link to the previous node:

struct node
{
    node* previous;
    node* next;
    int data;
};

And the linked list class stores a node that represents the end of the list, in addition to the starting node:

node* begin;
node* end;

Note that I said "A node that represents the end of the list", not "the last node of the list". That is, end->previous points to the last node of the list. end->next points to anything (nullptr). Check this picture.

So by this way, push_back implementation only puts end->preious pointing to the new node (and new_node->next pointing to end), and new_node->previous pointing to the value of end->previous after the insertion.

Community
  • 1
  • 1
Manu343726
  • 13,969
  • 4
  • 40
  • 75
  • Even if I use std::vector, what I am looking for is an array of vectors that would display all the vectors one by one. – sajal Jul 17 '13 at 12:32
  • Then change the data member of the node and use std::vector. But the simplest way is to use a vector of vectors ( `std::vector>` ) – Manu343726 Jul 17 '13 at 15:34
2

Then you should use this a 2D vector std::vector< std::vector<int> > vect;

Sanyam Goel
  • 2,138
  • 22
  • 40
  • do not forget to put space after > as >> is an operator and would produce a compile time error – Sanyam Goel Jul 17 '13 at 12:44
  • @sajal chk the answer and since you want the size of list containing the list as well to grow this is solution for that. – Sanyam Goel Jul 17 '13 at 12:49
  • 1
    Thanks a lot. Exactly the solution which I was looking for. – sajal Jul 17 '13 at 13:26
  • 2
    @SanyamGoel: Note that in C++11, leaving out the space will not cause a problem. – Jerry Coffin Jul 17 '13 at 14:06
  • @JerryCoffin thanks for updating I am not working on c++11 but will be switching to it soon so this will be helpful info for me :) – Sanyam Goel Jul 17 '13 at 14:09
  • @SanyamGoel : Just one more help.Like we have a 2D vector, can we have something as 2D tree. Or rather a tree of tree. – sajal Jul 17 '13 at 14:11
  • @sajal tree is already a non-linear data structure so 2d tree does not make a sense – Sanyam Goel Jul 17 '13 at 14:13
  • You can create a n-array (having any number of nodes) tree (not talking about a binary tree) that will help you in answering your question . To be more precise you can have a tree having vector nodes where vector will further have elements of type node ;) this way each node can point to another node having vector and so on.. – Sanyam Goel Jul 17 '13 at 14:14