Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct Node {
  5.     int data;
  6.     struct Node *next;
  7. } node;
  8.  
  9. node *head=NULL;
  10.  
  11. void insert_end(int data)
  12. {
  13.     node *newnode;
  14.     newnode=(node*)malloc(sizeof(node));
  15.     newnode->data=data;
  16.     newnode->next=NULL;
  17.     if(head==NULL)
  18.     {
  19.         head=newnode;
  20.     }
  21.     else
  22.     {
  23.         node* current=head;
  24.         while(current->next!=NULL)
  25.         {
  26.             current=current->next;
  27.         }
  28.         current->next=newnode;
  29.     }
  30. }
  31.  
  32. void insert_beginning(int data)
  33. {
  34.     node* newnode;
  35.     newnode=(node*)malloc(sizeof(node));
  36.     newnode->data=data;
  37.     newnode->next=head;
  38.     head=newnode;
  39. }
  40.  
  41. void insert_any_position(int data)
  42. {
  43.     int count=1,n;
  44.     printf("Enter the position: ");
  45.     scanf("%d",&n);
  46.     node* newnode;
  47.     newnode=(node*)malloc(sizeof(node));
  48.     newnode->data=data;
  49.     newnode->next=NULL;
  50.     if(n==1)
  51.     {
  52.         newnode->next=head;
  53.         head=newnode;
  54.     }
  55.     else
  56.     {
  57.         node*current=head;
  58.         while(count<n-1)
  59.         {
  60.             current=current->next;
  61.             count++;
  62.         }
  63.         newnode->next=current->next;
  64.         current->next=newnode;
  65.     }
  66. }
  67. void del_Data()
  68. {
  69.     int n;
  70.     node* current=head;
  71.     node* previous;
  72.     printf("Which data you want to delete: ");
  73.     scanf("%d",&n);
  74.     if(head->data==n)
  75.     {
  76.         head=head->next;
  77.         free(current);
  78.     }
  79.     else
  80.     {
  81.         while(current!=NULL && current->data!=n)
  82.         {
  83.             previous=current;
  84.             current=current->next;
  85.         }
  86.         if(current->data==n)
  87.         {
  88.             previous->next=current->next;
  89.             free(current);
  90.         }
  91.  
  92.     }
  93. }
  94. void del_position()
  95. {
  96.     node*current=head;
  97.     node*previous;
  98.     int count=1,n;
  99.     printf("Enter the position: ");
  100.     scanf("%d",&n);
  101.     if(n==1)
  102.     {
  103.         head=head->next;
  104.         free(current);
  105.     }
  106.     else
  107.     {
  108.         node*current=head;
  109.         while(count<n)
  110.         {
  111.             previous=current;
  112.             current=current->next;
  113.             count++;
  114.         }
  115.         previous->next=current->next;
  116.         free(current);
  117.     }
  118. }
  119.  
  120. void print()
  121. {
  122.     node*current=head;
  123.     printf("Newlist is: ");
  124.     while(current!=NULL)
  125.     {
  126.         printf("%d ",current->data);
  127.         current=current->next;
  128.     }
  129.     printf("\n\n");
  130. }
  131. void sum()
  132. {
  133.     node* current=head;
  134.     int sum=0;
  135.     while(current!=NULL)
  136.     {
  137.         sum+=current->data;
  138.  
  139.         current=current->next;
  140.     }
  141.     printf("Sum Of Total Elements: %d\n",sum);
  142. }
  143. void count()
  144. {
  145.     node* current=head;
  146.     int count=0;
  147.     while(current!=NULL)
  148.     {
  149.         count++;
  150.         current=current->next;
  151.     }
  152.     printf("Total Elements Left: %d\n",count);
  153. }
  154. void searchData()
  155. {
  156.     node*current=head;
  157.     int f=0,n;
  158.     printf("search data : ");
  159.     scanf("%d", &n);
  160.     while(current!=NULL)
  161.     {
  162.         if(current->data==n)
  163.         {
  164.             f=1;
  165.             break;
  166.         }
  167.         current=current->next;
  168.     }
  169.     if(f==1)
  170.     {
  171.         printf("found data\n");
  172.     }
  173.     else
  174.     {
  175.         printf("Not found data\n");
  176.     }
  177. }
  178.  
  179.  
  180.  
  181. int main()
  182. {
  183.     int i,n,num,a,b,c;
  184.     printf("Enter The Elements: ");
  185.     scanf("%d",&num);
  186.     while(num--)
  187.     {
  188.         printf("Enter The number: ");
  189.         scanf("%d",&n);
  190.         printf("                      1.End!            2.beginning!\n");
  191.         printf("                      3.Any position!   \n");
  192.         scanf("%d",&a);
  193.         if(a==1) insert_end(n);
  194.         else if(a==2)insert_beginning(n);
  195.         else if(a==3) insert_any_position(n);
  196.         print();
  197.     }
  198.     printf("How many data you want to delete: ");
  199.     scanf("%d",&b);
  200.     printf("\n");
  201.     while(b--)
  202.     {
  203.         printf("                      1.Delete data      2.Delete Position\n");
  204.         scanf("%d",&c);
  205.         if(c==2) del_position();
  206.         if(c==1) del_Data();
  207.         print();
  208.     }
  209.     count();
  210.     sum();
  211.     searchData();
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement