Advertisement
muftY

doubly delete_by_position nd insert_at_first (muftY)

Feb 25th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct data
  5. {
  6.     int a;
  7.  
  8.     struct data *next;
  9.     struct data *prev;
  10. } data;
  11.  
  12. data *head=NULL;
  13. data *tail=NULL;
  14.  
  15. void ins_at_first(int x)
  16. {
  17.     data *new_node=(data*)malloc(sizeof(data));
  18.     new_node->a=x;
  19.     new_node->next=NULL;
  20.     new_node->prev=NULL;
  21.  
  22.     if(head==NULL)
  23.     {
  24.         head=new_node;
  25.         return;
  26.     }
  27.     new_node->next=head;
  28.     head->prev=NULL;
  29.     head=new_node;
  30.  
  31.     return;
  32. }
  33.  
  34. void del_by_pos(int n)
  35. {
  36.  
  37.     if(head==NULL) //jokhn kicui nai
  38.     {
  39.         printf("     List Empty\n");
  40.         return;
  41.     }
  42.     if(n==1) //jokhn n er man 1 dewa hy
  43.     {
  44.         data *del=head;
  45.         if(head->next==NULL) //jokhn n er man 1 dewa hy ebng list e oi ekti matroi node cilo
  46.         {
  47.             head=NULL;
  48.             tail=NULL;
  49.             free(del);
  50.             return;
  51.         }
  52.         head=head->next;
  53.         head->prev=NULL;
  54.         free(del);
  55.         return;
  56.  
  57.  
  58.     }
  59.     data *temp=head;
  60.     n=n-2;
  61.  
  62.     while(n--)
  63.     {
  64.         temp=temp->next;
  65.         if(temp->next==NULL) //jodi node sonkhar besi n er man dewa hy then ei "if" kaj korbe
  66.                             //(mane node ache 5ta . ami n=100 tomo node dlt korte cai then)
  67.         {
  68.             printf("    Invalid Position\n");
  69.             return;
  70.         }
  71.  
  72.     }
  73.     data *del=temp->next;
  74.     if(del->next==NULL) // list er last node dlt korar khetre
  75.     {
  76.  
  77.         tail=temp;
  78.         temp->next=NULL;
  79.         free(del);
  80.         return;
  81.     }
  82.     del->next->prev=temp; //list er moddher savabik node dlt korte bakituku.
  83.     temp->next=del->next;
  84.     free(del);
  85. }
  86.  
  87. void print()
  88. {
  89.     data *temp=head;
  90.     if(temp==NULL)
  91.     {
  92.         printf("     Nothing To Print Vai\n");
  93.         return;
  94.     }
  95.  
  96.     while(temp!=NULL)
  97.     {
  98.         printf("%d ",temp->a);
  99.         temp=temp->next;
  100.     }
  101.     printf("\n");
  102. }
  103.  
  104.  
  105.  
  106. int main()
  107. {
  108.     int a,v,w,x,n,m,o;
  109.     while(1)
  110.     {
  111.         printf("1.Insert At First\n");
  112.         printf("2.Insert At End\n");
  113.         printf("3.Insert At n_th Position\n");
  114.  
  115.         printf("4.Delete By Position\n");
  116.         printf("5.Delete By Value\n");
  117.  
  118.         printf("8.Print\n");
  119.         printf("9.RESET LINK LIST\n");
  120.  
  121.         printf("\n 0 for Al-Bida{-)\n");
  122.         printf("\n             Command:");
  123.         scanf("%d",&w);
  124.         if(w==1)
  125.         {
  126.             printf("Enter an integer Number:");
  127.             scanf("%d",&x);
  128.             ins_at_first(x);
  129.  
  130.         }
  131. //        if(w==2)
  132. //        {
  133. //            printf("Enter an integer Number:");
  134. //            scanf("%d",&x);
  135. //            ins_at_end(x);
  136. //        }
  137. //        if(w==3)
  138. //        {
  139. //            printf("Declear The Position:");
  140. //            scanf("%d",&n);
  141. //            printf("Enter an integer Number:");
  142. //            scanf("%d",&x);
  143. //            ins_at_nth(n,x);
  144. //        }
  145.  
  146.         if(w==4)
  147.         {
  148.             printf("\nDeclear The Specific Position:");
  149.             scanf("%d",&n);
  150.  
  151.             del_by_pos(n);
  152.  
  153.         }
  154.  
  155.  
  156.         if(w==8)
  157.         {
  158.             printf("\n            Result: ");
  159.             print();
  160.             printf("\n");
  161.         }
  162.  
  163. //        if(w==9)
  164. //        {
  165. //            printf("\n      Reseted Successfully\n");
  166. //            reset();
  167. //        }
  168.  
  169.         if(w==0)
  170.         {
  171.             printf("\n    Jajakalla_khayer..[muftY]\n");
  172.             break;
  173.         }
  174.     }
  175. }
  176.  
  177. //    del_by_pos(5);
  178. //    print();
  179. //    ins_at_first(9);
  180. //    print();
  181. //    del_by_pos(1);
  182. //    print();
  183. //    ins_at_first(5);
  184. //    ins_at_first(7);
  185. //
  186. //    print();
  187. //    del_by_pos(1);
  188. //    print();
  189. //    ins_at_first(3);
  190. //    ins_at_first(2);
  191. //    ins_at_first(4);
  192. //
  193. //    print();
  194. //    del_by_pos(3);
  195. //    print();
  196. //    del_by_pos(5);
  197. //    print();
  198. //    del_by_pos(455);
  199. //    print();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement