rajamit872

Implementation of Linked List(Basics)

Oct 25th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. void append(struct node ** , int );
  6. void addtobeg(struct node ** , int );
  7. void addafter(struct node* , int , int);
  8. void display(struct node *);
  9. int count(struct node*);
  10. void del(struct node**,int);
  11.  
  12. struct node
  13. {
  14. int data;
  15. struct node *link;
  16. };
  17.  
  18. void main()
  19. {
  20.  
  21. struct node *p;
  22. p = NULL;
  23. clrscr();
  24. printf("\n\nNumber of element in the linked list %d\n",count(p));
  25.  
  26. append(&p,14);
  27. append(&p,51);
  28. append(&p,34);
  29. append(&p,76);
  30. append(&p,90);
  31. append(&p,154);
  32.  
  33. //clrscr();
  34. printf("\nElement in the linked list after inserting \n");
  35. display(p);
  36.  
  37. addtobeg(&p,657);
  38. addtobeg(&p,99);
  39. addtobeg(&p,99);
  40.  
  41. printf("\nElement in the linked list after inserting at begning\n");
  42. display(p);
  43.  
  44. addafter(p,7,0);
  45. addafter(p,17,4);
  46. addafter(p,56,2);
  47.  
  48. printf("\nElement in the linked list after inserting in the middle \n");
  49. display(p);
  50. printf("No of element in the linked list::%d", count(p));
  51.  
  52.  
  53. del(&p,657);
  54. del(&p,99);
  55. del(&p,14);
  56.  
  57.  
  58. display(p);
  59.  
  60. printf("No of element in the linked list::%d" , count(p));
  61.  
  62. getch();
  63. }
  64.  
  65.  
  66.     void append(struct node**q, int num)
  67.     {
  68.       struct node *temp, *r;
  69.     if(*q==NULL)
  70.     {
  71.       temp = (struct node *)malloc(sizeof(struct node));
  72.       temp->data = num;
  73.       temp->link = NULL;
  74.       *q = temp;
  75.     }
  76.     else
  77.     {
  78.       temp = *q;
  79.  
  80.       /*go to the last node*/
  81.       while(temp->link!=NULL)
  82.       temp = temp->link;
  83.       /*add node at the end */
  84.       r =  (struct node *)malloc(sizeof(struct node));
  85.       r->data = num;
  86.       r->link = NULL;
  87.       temp->link = r;
  88.     }
  89.  
  90.     }
  91.  
  92.  
  93.         /*Adds a new node at the begning of the lunked list*/
  94.  
  95.         void addtobeg(struct node **q, int num)
  96.         {
  97.            struct node *temp;
  98.  
  99.            /*add nes node*/
  100.            temp =  (struct node *)malloc(sizeof(struct node));
  101.            temp->data = num;
  102.            temp->link = *q;
  103.            *q = temp;
  104.         }
  105.  
  106.  
  107.         /*Adds a new node after specified node*/
  108.         void addafter(struct node *q , int loc , int num)
  109.         {
  110.           struct node *temp, *r;
  111.           int i;
  112.           temp=q;
  113.  
  114.           /*skip to desired position */
  115.           for(i=0;i<loc;i++)
  116.           {
  117.             temp=temp->link;
  118.               /* if end of the linked list is encountered */
  119.               if(temp==NULL)
  120.               {
  121.               printf("There are less than %d element in list\n",loc);
  122.               return ;
  123.               }
  124.           }
  125.               /* insert new node */
  126.               r = (struct node *)malloc(sizeof(struct node)) ;
  127.               r->data = num;
  128.               r->link = temp->data;
  129.               temp->link = r;
  130.         }
  131.  
  132.  
  133.         /* displays the contenet of element */
  134.         void display(struct node *q)
  135.         {
  136.           /* traverse the entire linked list */
  137.           while(q!=NULL)
  138.           {
  139.            printf("%d  ", q->data);
  140.            q=q->link;
  141.           }
  142.           printf("\n");
  143.         }
  144.  
  145.  
  146.         /* counts the number of nodes present in the linked list*/
  147.            int count(struct node *q)
  148.            {
  149.          int c=0;
  150.           /* traverse the entire linked list */
  151.           while(q!=NULL)
  152.           {
  153.             q=q->link;
  154.             c++;
  155.           }
  156.           return c;
  157.            }
  158.  
  159.  
  160.  
  161.             void del(struct node **q, int num)
  162.             {
  163.                struct node *temp, *old;
  164.                temp = *q;
  165.  
  166.                while(temp!=NULL)
  167.                {
  168.                  if(temp->data == num)
  169.                  {  /*if node is deleted at the 1st position*/
  170.                    if(temp==*q)
  171.                 *q=temp->link;
  172.  
  173.                 /*delete the intermediate node in the lnked list*/
  174.                    else
  175.                 old->link=temp->link;
  176.  
  177.                 /*free the node the occupied the memory*/
  178.                 free(temp);
  179.                 return ;
  180.                  }
  181.  
  182.                  /*traverse the entire linked list*/
  183.                  else
  184.                  {
  185.                    old = temp;    //old point to the previous node
  186.                    temp= temp->link;//go to the next node
  187.  
  188.                  }
  189.                }
  190.                printf("\n\nElement is nod found  %d",num);
  191.             }
Advertisement
Add Comment
Please, Sign In to add comment