Advertisement
Misbah_Uddin_Tareq

Data Assignment

Dec 11th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *next;
  8. };
  9.  
  10. struct node *head=NULL,*new_node=NULL,*temp=NULL;
  11. int i,n;
  12.  
  13. void storing_node();
  14. void print_linked_list();
  15. void insert_at_first_position();
  16. void insert_after_a_given_position();
  17. void insert_at_the_end_of_linked_list();
  18. void delate_from_linked_list();
  19.  
  20. int main()
  21. {
  22.     printf("Adab sir.\n");
  23.     printf("I am Misbah Uddin Tareq\n");
  24.     printf("My Id is 1912020092\n");
  25.     printf("___________________\n\n");
  26.  
  27.     printf("First of all,you have to store node in Linked List.So,\n");
  28.     storing_node();
  29.     printf("\nCurrently these are the standard of your link list\n");
  30.     print_linked_list();
  31.     printf("\n");
  32.  
  33.     printf("\nIf you want to insert an item at first position of your Linked List,then: ");
  34.     insert_at_first_position();
  35.     printf("\nCurrently these are the standard of your link list\n");
  36.     print_linked_list();
  37.     printf("\n");
  38.  
  39.     printf("\nIf you want to insert an item at any position of your Linked List,then: ");
  40.     insert_after_a_given_position();
  41.     printf("\n");
  42.  
  43.     printf("\nIf you want to insert an item at the end of your Linked List,then: ");
  44.     insert_at_the_end_of_linked_list();
  45.     printf("\nCurrently these are the standard of your link list\n");
  46.     print_linked_list();
  47.     printf("\n");
  48.  
  49.     printf("\nIf you want to delate an item from your Linked List,then: ");
  50.     delate_from_linked_list();
  51.  
  52.     printf("\n\nThank you sir.\n");
  53.  
  54.     return 0;
  55. }
  56.  
  57. void storing_node()
  58. {
  59.  
  60.     printf("Please,enter the number of elemnet you want to store: ");
  61.     scanf("%d",&n);
  62.  
  63.     printf("Now,enter all of your %d element respectively:\n",n);
  64.     for(i=1; i<=n; i++)
  65.     {
  66.         new_node = (struct node *) malloc(sizeof(struct node));
  67.  
  68.         if(new_node == NULL)
  69.         {
  70.             printf("ERROR!");
  71.             break;
  72.         }
  73.  
  74.         scanf("%d",&new_node->data);
  75.         new_node->next=NULL;
  76.  
  77.         if(head == NULL)
  78.             head=temp=new_node;
  79.         else
  80.         {
  81.             temp->next=new_node;
  82.             temp=new_node;
  83.         }
  84.     }
  85. }
  86.  
  87. void print_linked_list()
  88. {
  89.     temp=head;
  90.     while(temp!=NULL)
  91.     {
  92.         printf("%d ",temp->data);
  93.         temp=temp->next;
  94.     }
  95. }
  96.  
  97. void insert_at_first_position()
  98. {
  99.     printf("\nPlease,enter the number you want to insert at first position: ");
  100.  
  101.     new_node = (struct node*) malloc(sizeof(struct node));
  102.     scanf("%d",&new_node->data);
  103.     new_node->next=head;
  104.     head=new_node;
  105. }
  106.  
  107. void insert_after_a_given_position()
  108. {
  109.     bool p = false;
  110.     int item;
  111.     printf("\nPlease,enter the element after which you want to insert: ");
  112.     scanf("%d",&item);
  113.  
  114.     if(head==NULL)
  115.         printf("Sorry,item is not found !!");
  116.     else
  117.     {
  118.         temp = head;
  119.         while(temp!=NULL)
  120.         {
  121.             if(item == temp->data)
  122.             {
  123.                 new_node = (struct node*) malloc(sizeof(struct node));
  124.  
  125.                 printf("Please,enter the item you want to insert: ");
  126.                 scanf("%d",&new_node->data);
  127.                 new_node->next = temp->next;
  128.                 temp->next = new_node;
  129.                 p=true;
  130.             }
  131.             temp=temp->next;
  132.         }
  133.  
  134.         if(!p)
  135.             printf("Sorry,item is not found !!");
  136.         else
  137.         {
  138.             printf("\nCurrently these are the standard of your link list\n");
  139.             print_linked_list();
  140.         }
  141.     }
  142. }
  143.  
  144. void insert_at_the_end_of_linked_list()
  145. {
  146.     temp = head;
  147.     while(temp->next!=NULL)
  148.         temp=temp->next;
  149.  
  150.     printf("\nPlease,enter the number you want to insert at the end: ");
  151.     new_node = (struct node*) malloc(sizeof(struct node));
  152.     scanf("%d",&new_node->data);
  153.     temp->next = new_node;
  154.     new_node->next = NULL;
  155. }
  156.  
  157. void delate_from_linked_list()
  158. {
  159.     int item;
  160.     bool p=false;
  161.     struct node *x;
  162.  
  163.     printf("\nPlease,enter the element you want to delete: ");
  164.     scanf("%d",&item);
  165.  
  166.     if(head == NULL)
  167.         printf("Sorry,deletion is not Possible !!");
  168.     else if(item == head->data)
  169.     {
  170.         head = head->next;
  171.         p=true;
  172.     }
  173.     else
  174.     {
  175.         temp=head;
  176.         while(temp!=NULL)
  177.         {
  178.             if(item == temp->data)
  179.             {
  180.                 p=true;
  181.                 break;
  182.             }
  183.             x = temp;
  184.             temp = temp->next;
  185.         }
  186.  
  187.         if(!p)
  188.             printf("Sorry,item is not found !!");
  189.         else
  190.         {
  191.             x->next = temp->next;
  192.             free(temp);
  193.         }
  194.  
  195.     }
  196.  
  197.     if(p)
  198.     {
  199.         printf("\nCurrently these are the standard of your link list\n");
  200.         print_linked_list();
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement