Advertisement
Zinak

linklist

Oct 27th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node{
  4.     int q;
  5.     struct node *re;
  6. }node;
  7. node *lead;
  8. void atend(int g)
  9. {
  10.     node *sub,*an;
  11.     an=lead;
  12.     if(lead==NULL){
  13.         lead=(node*)malloc(sizeof(node));
  14.         lead->q=g;
  15.         lead->re=NULL;
  16.     }
  17.     else{
  18.         sub=(node*)malloc(sizeof(node));
  19.         sub->q=g;
  20.         sub->re=NULL;
  21.         while(an->re!=NULL){
  22.             an=an->re;
  23.         }
  24.         an->re=sub;
  25.     }
  26.     return;
  27. }
  28. void atfirst(int a)
  29. {
  30.     if(lead==NULL){
  31.         lead=(node*)malloc(sizeof(node));
  32.         lead->q=a;
  33.         lead->re=NULL;
  34.         return;
  35.     }
  36.     node *sub;
  37.     sub=(node*)malloc(sizeof(node));
  38.     sub->q=a;
  39.     sub->re=lead;
  40.     lead=sub;
  41.     return;
  42. }
  43. void atn(int v,int p)
  44. {
  45.     node *sub,*end=lead;
  46.     if(lead==NULL){
  47.         printf("List is empty. Create list by pressing 1 or 2.\n");
  48.         return;
  49.     }
  50.     sub=(node*)malloc(sizeof(node));
  51.     sub->q=v;
  52.     if(p==1){
  53.         sub->re=lead;
  54.         lead=sub;
  55.         return;
  56.     }
  57.     p-=2;
  58.     while(p--){
  59.         end=end->re;
  60.         if(end->re==NULL)break;
  61.     }
  62.     sub->re=end->re;
  63.     end->re=sub;
  64.     return;
  65. }
  66. void ataftern(int v,int p)
  67. {
  68.     node *sub,*end=lead;
  69.     if(lead==NULL){
  70.         printf("List is empty. Create list by pressing 1 or 2.\n");
  71.         return;
  72.     }
  73.     sub=(node*)malloc(sizeof(node));
  74.     sub->q=v;
  75.     if(p==1){
  76.         sub->re=end->re;
  77.         end->re=sub;
  78.         return;
  79.     }
  80.     p-=1;
  81.     while(p--){
  82.         end=end->re;
  83.         if(end->re==NULL)break;
  84.     }
  85.     sub->re=end->re;
  86.     end->re=sub;
  87.     return;
  88. }
  89. void eran(int d)
  90. {
  91.     node *nn,*sub=lead;
  92.     if(lead==NULL)return;
  93.     if(d==1){
  94.         nn=lead;
  95.         lead=nn->re;
  96.         free(nn);
  97.         return;
  98.     }
  99.     d-=2;
  100.     while(d--){
  101.         if(sub->re==NULL){
  102.             printf("Doesn't exist.\n");
  103.             return;
  104.         }
  105.         sub=sub->re;
  106.     }
  107.     nn=sub->re;
  108.     sub->re=nn->re;
  109.     free(nn);
  110.     return;
  111. }
  112. void eravl(int z)
  113. {
  114.     node *nn,*sub=lead;
  115.     int x=lookfor(z);
  116.     if(x==-1){
  117.         printf("Not found\n");
  118.         return;
  119.     }
  120.     if(x==1){
  121.         nn=lead;
  122.         lead=nn->re;
  123.         free(nn);
  124.         return;
  125.     }
  126.     x-=2;
  127.     while(x--){
  128.         sub=sub->re;
  129.     }
  130.     nn=sub->re;
  131.     sub->re=nn->re;
  132.     free(nn);
  133.     return;
  134. }
  135. int lookfor(int z)
  136. {
  137.     int nt=1;
  138.     node *sub=lead;
  139.     while(sub->q!=z){
  140.         nt++;
  141.         if(sub->re==NULL){
  142.             nt=0;
  143.             break;
  144.         }
  145.         sub=sub->re;
  146.     }
  147.     return nt;
  148. }
  149. void listprint()
  150. {
  151.     node *sub;
  152.     sub=lead;
  153.     while(sub!=NULL){
  154.         printf("%d ",sub->q);
  155.         sub=sub->re;
  156.     }
  157.     return;
  158. }
  159.  
  160. int max()
  161. {
  162. node *sub=lead;
  163. int p=0;
  164. while(sub!=NULL){
  165.         if(sub->q>p)
  166.           p=sub->q;
  167.         sub=sub->re;
  168.     }
  169.     return p;
  170. }
  171.  
  172.  
  173. int main()
  174. {
  175.     lead=NULL;
  176.     int z,w,r;
  177.     printf("1.Value at end\n");
  178.     printf("2.Value at first\n");
  179.     printf("3.Value at n-th place\n");
  180.     printf("4.Value at after n-th place\n");
  181.     printf("5.Delete n-th node\n");
  182.     printf("6.Delete a value\n");
  183.     printf("7.Find a value\n");
  184.     printf("8.Print the list\n");
  185.     printf("9.Print the max\n");
  186.     while(1){
  187.         scanf("%d",&z);
  188.         switch(z){
  189.         case 1:
  190.             scanf("%d",&w);
  191.             atend(w);
  192.             break;
  193.         case 2:
  194.             scanf("%d",&w);
  195.             atfirst(w);
  196.             break;
  197.         case 3:
  198.             printf("Value > ");
  199.             scanf("%d",&r);
  200.             printf("Which position > ");
  201.             scanf("%d",&w);
  202.             atn(r,w);
  203.             break;
  204.         case 4:
  205.             printf("Value > ");
  206.             scanf("%d",&r);
  207.             printf("After which position > ");
  208.             scanf("%d",&w);
  209.             ataftern(r,w);
  210.             break;
  211.         case 5:
  212.             scanf("%d",&r);
  213.             eran(r);
  214.             break;
  215.         case 6:
  216.             scanf("%d",&r);
  217.             eravl(r);
  218.             break;
  219.         case 7:
  220.            {
  221.             scanf("%d",&r);
  222.             int kl=lookfor(r);
  223.             if(kl==0)printf("Not found\n");
  224.             else printf("Position %d\n",kl);
  225.             break;
  226.            }
  227.         case 8:
  228.             listprint();
  229.             printf("\n");
  230.             break;
  231.         case 9:
  232.             max();
  233.             printf("%d",max());
  234.             printf("\n");
  235.             break;
  236.         }
  237.     }
  238.     return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement