Advertisement
tarikulislam

LinkedListManue

Jul 24th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. /*........External Documentation........
  2.  
  3.     @@Linear Linked List
  4.     Basic Implimentation with manue.
  5.  
  6.     Written By:
  7.         Name: Md. Tarikul Islam.
  8.         ID NO# 011 151 013
  9.         United International University
  10. .......................................*/
  11.  
  12. #include <cstdio>
  13. #include <iostream>
  14. #include <cstdlib>
  15.  
  16. using namespace std;
  17.  
  18. struct list {
  19.     int data;
  20.     struct list *next;
  21. };
  22. typedef struct list node;
  23. // All the prototypes of functions
  24. void display (node *head);
  25. void insertatbegin (node *head);
  26. void insertatmiddle (node *head);
  27. void insertatlast (node *head);
  28. int sum (node *head);
  29. int maximum (node *head);
  30. int minimum (node *head);
  31. void linearsearch (node *head);
  32. void reverselist (node *head, int length);
  33. void bubble_sort(node *head, int length);
  34. // Main function Begins:
  35. int main(){
  36.  
  37.     int i, n;
  38.     node *start, *prev, *temp;
  39.  
  40.     start = new node();
  41.     start->next = NULL;
  42.     prev = start;  // making dummy node
  43.  
  44.     printf("How many data: ");
  45.     scanf("%d", &n);
  46.  
  47.     printf("\nEnter %d data:\n\n", n);
  48.     // taking n data to create linked list.
  49.     for ( i = 0; i < n; i++) {
  50.  
  51.         temp = new node();
  52.         scanf("%d", &temp->data);
  53.         prev->next = temp;
  54.         prev = temp;
  55.     }
  56.  
  57.     printf("\nBefore Insertion: ");
  58.     display(start);
  59.  
  60.     while (1) {
  61.  
  62.         printf("\n\tMANUE:\n\n");
  63.         printf("\t1.Insert at the begin:\n\t2.Insert at the middle:\n\t3.Insert at the end:\n\t4.Display\n\t5.Exit:\n\t6.Sum:\n\t7.Maximum:\n\t8.Minimum\n\t9.Linear Search:\n\t10.Reverse List:\n\t11.Sort\n\n");
  64.         int option;
  65.  
  66.         printf("\nOption_: ");
  67.         scanf("%d", &option);
  68.  
  69.             switch(option){
  70.  
  71.                 case 1:
  72.                     insertatbegin(start);
  73.                     break;
  74.                 case 2:
  75.                     insertatmiddle(start);
  76.                     break;
  77.                 case 3:
  78.                     insertatlast(start);
  79.                     break;
  80.                 case 4:
  81.                     display(start);
  82.                     break;
  83.                 case 5:
  84.                     exit(0);
  85.                 case 6:
  86.                     printf("\nSum is: %d\n", sum(start));
  87.                     break;
  88.                 case 7:
  89.                     printf("\nMaximum : %d\n", maximum(start));
  90.                     break;
  91.                 case 8:
  92.                     printf("\nMinimum: %d\n", minimum(start));
  93.                     break;
  94.                 case 9:
  95.                     linearsearch(start);
  96.                     break;
  97.                 case 10:
  98.                     reverselist(start, n);
  99.                     break;
  100.                 case 11:
  101.                     bubble_sort(start, n);
  102.                     break;
  103.                 default:
  104.                     break;
  105.                 }
  106.     }
  107.     return 0;
  108.     // Main Function End.
  109. }
  110. // Function body Begins
  111. void display (node *head) {
  112.  
  113.     node *temp;
  114.     temp = head->next;
  115.  
  116.     printf("\nDisplay: ");
  117.  
  118.     while (temp != NULL) {
  119.  
  120.         printf("%d ", temp->data);
  121.         temp = temp->next;
  122.     }
  123.     printf("\n");
  124. }
  125.  
  126. void insertatbegin (node *head) {
  127.  
  128.     node *temp;
  129.     temp = new node();
  130.  
  131.     printf("Insert new data: ");
  132.     scanf("%d", &temp->data);
  133.  
  134.     temp->next = head->next;
  135.     head->next = temp;
  136.  
  137. }
  138.  
  139. void insertatlast (node *head) {
  140.  
  141.     node *temp1, *temp;
  142.     temp = new node();
  143.  
  144.     printf("Insert new data: ");
  145.     scanf("%d", &temp->data);
  146.  
  147.    temp1 = head->next;
  148.  
  149.     while (temp1->next != NULL) {
  150.  
  151.         temp1 = temp1->next;
  152.     }
  153.  
  154.     temp1->next = temp;
  155.     temp->next = NULL;
  156. }
  157.  
  158. void insertatmiddle (node *head) {
  159.  
  160.     node *temp1, *temp;
  161.     temp1 = new node();
  162.  
  163.     printf("Insert new data: ");
  164.     scanf("%d", &temp1->data);
  165.  
  166.     int n = temp1->data;
  167.     temp = head;
  168.  
  169.     while ((temp->next != NULL) && (temp->next->data < n))
  170.         temp = temp->next;
  171.  
  172.     temp1->next = temp->next;
  173.     temp->next = temp1;
  174. }
  175.  
  176. int sum (node *head) {
  177.  
  178.     node *temp;
  179.     temp = head->next;
  180.     int sum = 0;
  181.  
  182.     while (temp != NULL){
  183.  
  184.         sum = sum + temp->data;
  185.         temp = temp->next;
  186.     }
  187.  
  188.     return sum;
  189. }
  190.  
  191. int maximum (node *head) {
  192.  
  193.     node *temp;
  194.     temp = head->next;
  195.     int mx = temp->data;
  196.     while ( temp != NULL) {
  197.  
  198.         if (mx < temp->data)
  199.             mx = temp->data;
  200.  
  201.         temp = temp->next;
  202.     }
  203.     return mx;
  204. }
  205.  
  206. int minimum (node *head) {
  207.  
  208.     node *temp;
  209.     temp = head->next;
  210.     int mn = temp->data;
  211.  
  212.     while (temp != NULL) {
  213.  
  214.         if ( mn > temp->data)
  215.             mn = temp->data;
  216.  
  217.         temp = temp->next;
  218.     }
  219.     return mn;
  220. }
  221.  
  222. void linearsearch (node *head) {
  223.  
  224.     node *temp;
  225.     temp = head->next;
  226.     int key, flag;
  227.     printf("key: ");
  228.     scanf("%d", &key);
  229.  
  230.     while (temp != NULL) {
  231.  
  232.         if (key == temp->data)
  233.             flag = 1;
  234.  
  235.         temp = temp->next;
  236.     }
  237.     if (flag == 1)
  238.         printf("\nFound\n");
  239.     else
  240.         printf("\nNot Found\n");
  241. }
  242.  
  243. void reverselist(node *head, int length) {
  244.  
  245.     node *start, *temp;
  246.     start = NULL;
  247.  
  248.     for (int i = 1; i <= length; i++){
  249.         temp = head->next;
  250.  
  251.         while (temp->next != start)
  252.             temp = temp->next;
  253.         printf("%d ", temp->data);
  254.         start = temp;
  255.  
  256.     }
  257.     printf("\n");
  258. }
  259.  
  260. void bubble_sort(node *head, int length) {
  261.  
  262.     node *temp;
  263.     int tp;
  264.  
  265.     for (int i = 1; i <= length; i++) {
  266.  
  267.         temp = head->next;
  268.  
  269.         while (temp->next != NULL){
  270.  
  271.             if ( (temp->data) > (temp->next->data)) {
  272.  
  273.                 tp = temp->data;
  274.                 temp->data = temp->next->data;
  275.                 temp->next->data = tp;
  276.             }
  277.             temp = temp->next;
  278.         }
  279.  
  280.     }
  281.         display(head);
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement