Advertisement
NabilaShova

DS Lab 4- Position Linked List

Jul 27th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node{
  5.     int data;
  6.     struct node *next;
  7. }*head;
  8.  
  9. void add(int num)
  10. {
  11.     struct node *temp;
  12.     temp=(struct node*)malloc(sizeof(struct node));
  13.  
  14.     temp->data=num;
  15.  
  16.     if(head==NULL){
  17.         head=temp;
  18.         temp->next=NULL;
  19.     }
  20.     else{
  21.         temp->next=head;
  22.         head=temp;
  23.     }
  24. }
  25.  
  26. void append(int num)
  27. {
  28.     struct node *temp, *right;
  29.  
  30.     temp=(struct node*)malloc(sizeof(struct node));
  31.     temp->data=num;
  32.     right=(struct node*)head;
  33.  
  34.     while(right->next!=NULL)
  35.         right=right->next;
  36.         right->next=temp;
  37.         right=temp;
  38.         right->next=NULL;
  39. }
  40.  
  41. void add_after(int num, int loc)
  42. {
  43.     int i;
  44.     struct node *temp,*left,*right;
  45.     right=head;
  46.     for(i=1;i<loc;i++){
  47.         left=right;
  48.         right=right->next;
  49.     }
  50.  
  51.     temp=(struct node*)malloc(sizeof(struct node));
  52.     temp->data=num;
  53.     left->next=temp;
  54.     left=temp;
  55.     left->next=right;
  56. }
  57.  
  58.  
  59. int count(){
  60.     struct node *temp;
  61.     int count=0;
  62.     temp=head;
  63.         while(temp!=NULL){
  64.             count++;
  65.             temp=temp->next;
  66.         }
  67.         return count;
  68. }
  69.  
  70.  
  71. void insert(int num){
  72.     int c=0;
  73.     struct node *temp;
  74.     temp=head;
  75.     if(temp==NULL){
  76.         add(num);
  77.     }
  78.     else{
  79.         while(temp!=NULL){
  80.             if(temp->data<num)
  81.                 c++;
  82.                 temp=temp->next;
  83.         }
  84.         if(c==0){
  85.             add(num);
  86.         }
  87.         else if(c<count()){
  88.             add_after(num,++c);
  89.         }
  90.         else{
  91.             append(num);
  92.         }
  93.     }
  94. }
  95.  
  96. struct node *positionNode(int index , int x){
  97.  
  98. if(index < 0){
  99.     return NULL;
  100. }
  101.  
  102. int currIndex = 1;
  103.  
  104. struct node *currNode = head ;
  105.  
  106. while(currNode!=NULL && index > currIndex){
  107.  
  108.     currNode = currNode->next;
  109.  
  110.     currIndex++;
  111. }
  112.  
  113.  
  114. if(index > 0 && currNode==NULL){
  115.     return NULL;
  116. }
  117.  
  118.  
  119. node *newNode = (struct node *)malloc(sizeof(struct node));
  120. newNode->data = x;
  121.  
  122.  
  123. if(index==0){
  124.     newNode->next = head;
  125.     head = newNode ;
  126. }
  127. else{
  128.     newNode->next = currNode->next;
  129.     currNode->next = newNode ;
  130. }
  131.  
  132. return newNode ;
  133. }
  134.  
  135. void Display(){
  136.     struct node *temp;
  137.     temp=head;
  138.     if(temp==NULL)
  139.         printf("List is empty\n\n");
  140.     else{
  141.         while(temp!=NULL){
  142.             printf("%d ",temp->data);
  143.             temp=temp->next;
  144.         }
  145.         printf("\n\n");
  146.     }
  147. }
  148.  
  149. int del (int num)
  150. {
  151.     struct node *temp,*prev;
  152.     temp=head;
  153.     while(temp!=NULL){
  154.         if(temp->data==num){
  155.             if(temp==head){
  156.                 head=temp->next;
  157.                 free(temp);
  158.                 return 1;
  159.             }
  160.             else{
  161.                 prev->next=temp->next;
  162.                 free(temp);
  163.                 return 1;
  164.             }
  165.         }
  166.         else{
  167.             prev=temp;
  168.             temp=temp->next;
  169.         }
  170.     }
  171.     return 0;
  172. }
  173.  
  174. void positionDelete(int index)
  175. {
  176.     struct node *temp,*prev;
  177.     int i=1;
  178.     temp=head;
  179.  
  180.     if(index>count())
  181.         printf("Invalid\n");
  182.     else{
  183.         while(1){
  184.         if(i==index){
  185.             if(temp==head){
  186.                 head=temp->next;
  187.                 free(temp);
  188.                 printf("Delete Succesfully\n");
  189.                 break;
  190.             }
  191.             else{
  192.                 prev->next=temp->next;
  193.                 free(temp);
  194.                 printf("Delete Succesfully\n");
  195.                 break;
  196.             }
  197.         }
  198.         else{
  199.             i++;
  200.             prev=temp;
  201.             temp=temp->next;
  202.         }
  203.         }
  204.     }
  205. }
  206.  
  207. int main()
  208. {
  209.     int num,n,op,ch,pos,x,chs,index;
  210.     while(1){
  211.         printf("Press 1 for Insert\nPress 2 for Display\nPress 3 for Size\nPress 4 for delete\nPress 5 for exit\n\n");
  212.  
  213.         scanf("%d", &op);
  214.         if(op==1){
  215.             printf("1 for normal insert\n2 for Position insert\n");
  216.             scanf("%d",&ch);
  217.             if(ch==1){
  218.                 printf("Enter Number: ");
  219.                 scanf("%d",&num);
  220.                 insert(num);
  221.             }
  222.             else if(ch==2){
  223.                 printf("Enter Position : ");
  224.                 scanf("%d", &pos);
  225.                 printf("Enter Number: ");
  226.                 scanf("%d", &x);
  227.                 positionNode(pos-1,x);
  228.             }
  229.             else
  230.                 printf("Invalid\n\n");
  231.         }
  232.  
  233.         else if(op==2){
  234.             Display();
  235.         }
  236.         else if(op==3)
  237.             printf("Size is %d\n", count());
  238.         else if(op==4){
  239.             printf("Press 1 for num delete\nPress 2 for position delete\n");
  240.             scanf("%d",&chs);
  241.             if(chs==1){
  242.                 printf("Enter num for delete : ");
  243.                 scanf("%d", &n);
  244.                 if(del(n)==1)
  245.                     printf("%d is deleted succesfully\n",n);
  246.                 else
  247.                     printf("%d is not found\n");
  248.             }
  249.             else if(chs==2){
  250.                 printf("Enter the position : ");
  251.                 scanf("%d", &index);
  252.                 positionDelete(index);
  253.             }
  254.  
  255.             else
  256.                 printf("Invalid Option\n\n");
  257.             }
  258.         else
  259.             break;
  260.     }
  261.  
  262.     return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement