Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5.     int data;
  6.     struct node *link;
  7. };
  8. struct node* head=NULL;
  9. int l;
  10. void insertAtPosition(int j)
  11. {  int i;
  12.     struct node* temp;
  13.     temp=(struct node*)malloc(sizeof(struct node));
  14.     int pos;
  15.     sos:
  16.     {
  17.     printf("Enter Position: ");
  18.     scanf("%d",&pos);
  19.     }
  20.      if(pos<=j)
  21.     {
  22.     printf("Enter %d Position node data:",pos);
  23.    scanf("%d",&temp->data);
  24.     temp->link=NULL;
  25.     struct node* p=head;
  26.     if(pos==1)
  27.  
  28.     {
  29.         temp->link=head;
  30.         head=temp;
  31.     }
  32.     else
  33.     {
  34.         for(i=2; i<pos; i++)
  35.         {
  36.             p=p->link;
  37.         }
  38.         temp->link=p->link;
  39.         p->link=temp;
  40.     }
  41.     }
  42.     else if(pos==j+1)
  43.     {  printf("Enter %d Position node data:",pos);
  44.    scanf("%d",&temp->data);
  45.     temp->link=NULL;
  46.     struct node* p=head;
  47.         while(p->link!=NULL)
  48.         {
  49.             p=p->link;
  50.         }
  51.         p->link=temp;
  52.     }
  53.     else
  54.     {
  55.         printf("Invalid Position!\n");
  56.         goto sos;
  57.     }
  58. }
  59. void reverseDispList(int l)
  60. {
  61.     struct node *p, *q;
  62.     int i=0,j=l,k,temp;
  63.     p=q=head;
  64.     while(i<j)
  65. {
  66.  
  67.         k=0;
  68.         while(k<j)
  69.         {
  70.             q=q->link;
  71.             k++;
  72.         }
  73.         temp=p->data;
  74.         p->data=q->data;
  75.         q->data=temp;
  76.     i++;
  77.     j--;
  78.     p=p->link;
  79.     q=head;
  80.     }
  81.  
  82. }
  83.  
  84.  
  85. void insert(int data)
  86. {
  87.     struct node* temp;
  88.     temp=(struct node*)malloc(sizeof(struct node));
  89.     temp->data=data;
  90.     temp->link=NULL;
  91.     if(head==NULL)
  92.     {
  93.         head=temp;
  94.     }
  95.  
  96.     else
  97.     {
  98.         struct node *p=head;
  99.         while(p->link!=NULL)
  100.         {
  101.             p=p->link;
  102.         }
  103.         p->link=temp;
  104.  
  105.  
  106.     }
  107. }
  108. void deletenode(int j)
  109. {
  110.     int n,i;
  111.     struct node*p;
  112.     p=head;
  113.     sos:{
  114.     printf("Enter Node which you want to delete: ");
  115.     scanf("%d",&n);
  116.     }
  117.     if(n<=j+1)
  118.     {
  119.         if(n==1)
  120.     {
  121.         head=p->link;
  122.         p->link=NULL;
  123.         free(p);
  124.     }
  125.     else
  126.     {
  127.         struct node*q;
  128.  
  129.         for(i=1; i<n-1; i++)
  130.         {
  131.             p=p->link;
  132.         }
  133.     q=p->link;
  134.     p->link=q->link;
  135.     q->link=NULL;
  136.     free(q);
  137.     }
  138. }
  139. else
  140. {
  141.     printf("Invalid Imput!");
  142.     goto sos;
  143. }
  144. }
  145. void length()
  146. {
  147.     struct node* temp;
  148.     temp=head;
  149.      while(temp!=NULL)
  150.     {
  151.         l++;
  152.         temp=temp->link;
  153.     }
  154. }
  155. void display()
  156. {
  157.     struct node* temp;
  158.     temp=head;
  159.     while(temp!=NULL)
  160.     {
  161.         printf("%d ",temp->data);
  162.         temp=temp->link;
  163.     }
  164. }
  165. int main()
  166. {
  167.     int n,i=5;
  168.     int p=i;
  169.     while(p--)
  170.     {
  171.         printf("Enter node data:");
  172.         scanf("%d",&n);
  173.         insert(n);
  174.     }
  175.     display();
  176.     printf("\n");
  177.      length();
  178.     printf("The node Length id: %d\n",l);
  179.     insertAtPosition(l);
  180.     display();
  181.     printf("\n");
  182.     reverseDispList(l);
  183.     printf("the reverse is:\n");
  184.     display();
  185.      printf("\n");
  186.     deletenode(l);
  187.     display();
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement