Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,083 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,412 people online right now. Registration is fast and FREE... Join Now!





Linked List Class

Implements the Linked List Data Structure.

Submitted By: born2c0de
Actions:
Rating:
Views: 58,966

Language: C++

Last Modified: January 18, 2008

Snippet


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class linklist
  6. {
  7.      private:
  8.  
  9.              struct node
  10.          {
  11.               int data;
  12.             node *link;
  13.          }*p;
  14.  
  15.    public:
  16.  
  17.              linklist();
  18.          void append( int num );
  19.          void add_as_first( int num );
  20.          void addafter( int c, int num );
  21.          void del( int num );
  22.          void display();
  23.          int count();
  24.          ~linklist();
  25. };
  26.  
  27. linklist::linklist()
  28. {
  29.      p=NULL;
  30. }
  31.  
  32. void linklist::append(int num)
  33. {
  34.      node *q,*t;
  35.  
  36.    if( p == NULL )
  37.    {
  38.         p = new node;
  39.       p->data = num;
  40.       p->link = NULL;
  41.    }
  42.    else
  43.    {
  44.         q = p;
  45.       while( q->link != NULL )
  46.            q = q->link;
  47.  
  48.       t = new node;
  49.       t->data = num;
  50.       t->link = NULL;
  51.       q->link = t;
  52.    }
  53. }
  54.  
  55. void linklist::add_as_first(int num)
  56. {
  57.      node *q;
  58.  
  59.    q = new node;
  60.    q->data = num;
  61.    q->link = p;
  62.    p = q;
  63. }
  64.  
  65. void linklist::addafter( int c, int num)
  66. {
  67.      node *q,*t;
  68.    int i;
  69.    for(i=0,q=p;i<c;i++)
  70.    {
  71.         q = q->link;
  72.       if( q == NULL )
  73.       {
  74.            cout<<"\nThere are less than "<<c<<" elements.";
  75.          return;
  76.       }
  77.    }
  78.  
  79.    t = new node;
  80.    t->data = num;
  81.    t->link = q->link;
  82.    q->link = t;
  83. }
  84.  
  85. void linklist::del( int num )
  86. {
  87.      node *q,*r;
  88.    q = p;
  89.    if( q->data == num )
  90.    {
  91.         p = q->link;
  92.       delete q;
  93.       return;
  94.    }
  95.  
  96.    r = q;
  97.    while( q!=NULL )
  98.    {
  99.         if( q->data == num )
  100.       {
  101.            r->link = q->link;
  102.          delete q;
  103.          return;
  104.       }
  105.  
  106.       r = q;
  107.       q = q->link;
  108.    }
  109.    cout<<"\nElement "<<num<<" not Found.";
  110. }
  111.  
  112. void linklist::display()
  113. {
  114.      node *q;
  115.    cout<<endl;
  116.  
  117.    for( q = p ; q != NULL ; q = q->link )
  118.         cout<<endl<<q->data;
  119.  
  120. }
  121.  
  122. int linklist::count()
  123. {
  124.      node *q;
  125.    int c=0;
  126.    for( q=p ; q != NULL ; q = q->link )
  127.         c++;
  128.  
  129.    return c;
  130. }
  131.  
  132. linklist::~linklist()
  133. {
  134.      node *q;
  135.    if( p == NULL )
  136.         return;
  137.  
  138.    while( p != NULL )
  139.    {
  140.         q = p->link;
  141.       delete p;
  142.       p = q;
  143.    }
  144. }
  145.  
  146. int main()
  147. {
  148.      linklist ll;
  149.    cout<<"No. of elements = "<<ll.count();
  150.    ll.append(12);
  151.    ll.append(13);
  152.    ll.append(23);
  153.    ll.append(43);
  154.    ll.append(44);
  155.    ll.append(50);
  156.  
  157.    ll.add_as_first(2);
  158.    ll.add_as_first(1);
  159.  
  160.    ll.addafter(3,333);
  161.    ll.addafter(6,666);
  162.  
  163.    ll.display();
  164.    cout<<"\nNo. of elements = "<<ll.count();
  165.  
  166.    ll.del(333);
  167.    ll.del(12);
  168.    ll.del(98);
  169.    cout<<"\nNo. of elements = "<<ll.count();
  170.    return 0;
  171. }

Copy & Paste


Comments


everest_01 2008-08-17 21:33:11

Nice job It is to the point, clear and useful

NevilleDNZ 2008-11-27 05:09:16

Hi... I revengineered your Linked List Data Structure code sinppet http://www.dreamincode.net/forums/showtopic73767.htm in an "earlier" language. N

rissvann 2008-12-23 15:13:53

that is good, i wish i can do things like that


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month