HmHimu

linear list

Jan 31st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct Node
  5. {
  6.     int data;
  7.     struct Node *next;
  8. }*node,*temp;
  9.  
  10. struct Head
  11. {
  12.     int count;
  13.     struct Node *p;
  14. }*head;
  15.  
  16.  
  17.  
  18.  
  19. void createNode()
  20. {
  21.     node=(struct Node*)malloc(sizeof(struct Node));
  22.     scanf("%d", &node->data);
  23.     node->next=NULL;
  24.     node->next=head->p;
  25.     head->p=node;
  26.     head->count++;
  27.  
  28. }
  29.  
  30.  
  31.  
  32. void add_last()
  33. {
  34.     temp=head->p;
  35.  
  36.     while(temp->next!=NULL)
  37.     {
  38.         temp=temp->next;
  39.     }
  40.     node=(struct Node*)malloc(sizeof(struct Node));
  41.     scanf("%d", &node->data);
  42.     node->next=NULL;
  43.  
  44.     temp->next=node;
  45.     head->count++;
  46. }
  47.  
  48. void delate_first()
  49. {
  50.     temp=head->p;
  51.     head->p=temp->next;
  52.     free(temp);
  53.     head->count--;
  54. }
  55.  
  56. void show()
  57. { int i;
  58.     temp=head->p;
  59.  
  60.         for(i=1;i<=head->count;i++)
  61.         {
  62.         printf("\n %d ", temp->data);
  63.         temp=temp->next;
  64.         }
  65.     }
  66.  
  67.  
  68. int main()
  69. {
  70.     int a,i,n,m;
  71.     head=(struct Head*)malloc(sizeof(struct Head));
  72.     head->count=0;
  73.     head->p=NULL;
  74.  
  75.  
  76.     printf("where you want to add node?\n");
  77.     printf("At first or at last");
  78.     printf("press 1 for add first & press 2 for add last");
  79.     scanf("%d",&a);
  80.     if(a==1)
  81.     {
  82.  
  83.         printf("how many node you want to create?");
  84.         scanf("%d",&n);
  85.         for(m=1;m<=n;m++)
  86.         {
  87.            createNode();
  88.         }
  89.          show();
  90.  
  91.     }
  92.  
  93.     if(a==2)
  94.     {
  95.         printf("at first create a node");
  96.            createNode();
  97.  
  98.         add_last();
  99.  
  100.         show();
  101.     }
  102.  
  103.  
  104.     delate_first();
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. }
Add Comment
Please, Sign In to add comment