hasancse1991

linked-list-with-string

Jan 15th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.91 KB | None | 0 0
  1. /*
  2.     Linked List implementation by C programming Language
  3.     Operations: create list, insert item to last, insert item to first,
  4.         insert item to middle, search an item, delete an item, print list
  5.     Programmed by: Hasan Abdullah
  6.     Contact: http://hellohasan.com/
  7. */
  8.  
  9. #include<iostream>
  10. #include<stdio.h>
  11. #include<stdlib.h>
  12.  
  13. using namespace std;
  14.  
  15. struct linked_list
  16. {
  17.     string number;
  18.     struct linked_list *next;
  19. };
  20.  
  21. typedef struct linked_list node;
  22. node *head=NULL, *last=NULL;
  23.  
  24. void create_linked_list();
  25. void print_linked_list();
  26. void insert_at_last(string value);
  27. void insert_at_first(string value);
  28. void insert_after(string key, string value);
  29. void delete_item(string value);
  30. void search_item(string value);
  31.  
  32. int main()
  33. {
  34.     string key, value;
  35.  
  36.     //Create a linked list
  37.     printf("Create Linked List\n");
  38.     create_linked_list();
  39.     print_linked_list();
  40.  
  41.  
  42.     //Insert value at last position to existing Linked List
  43.     printf("\nInsert new item at last\n");
  44.     //scanf("%d", &value);
  45.     getline(cin, value);
  46.     insert_at_last(value);
  47.     print_linked_list();
  48.  
  49.  
  50.     //Insert value at first position to existing Linked List
  51.     printf("\nInsert new item at first\n");
  52.     //scanf("%d", &value);
  53.     getline(cin, value);
  54.     insert_at_first(value);
  55.     print_linked_list();
  56.  
  57.  
  58.     //Insert value after a defined value to existing Linked List
  59.     printf("\nEnter a KEY (existing item of List), after that you want to insert a value\n");
  60.     //scanf("%d", &key);
  61.     getline(cin, key);
  62.     printf("\nInsert new item after %s KEY\n", key.c_str());
  63.     //scanf("%d", &value);
  64.     getline(cin, value);
  65.     insert_after(key, value);
  66.     print_linked_list();
  67.  
  68.  
  69.     //Search an item from Linked List
  70.     printf("\nEnter an item to search it from List\n");
  71.     //scanf("%d", &value);
  72.     getline(cin, value);
  73.     search_item(value);
  74.  
  75.  
  76.     //Delete value from List
  77.     printf("\nEnter a value, which you want to delete from list\n");
  78.     //scanf("%d", &value);
  79.     getline(cin, value);
  80.     delete_item(value);
  81.     print_linked_list();
  82.  
  83.  
  84.     return 0;
  85. }
  86.  
  87.  
  88. /*
  89.     User Defined Functions
  90. */
  91. void create_linked_list()
  92. {
  93.     string val;
  94.  
  95.     while(1)
  96.     {
  97.         printf("Input a number. (Enter -1 to exit)\n");
  98.  
  99.         //scanf("%d", &val);
  100.         getline(cin,val);
  101.  
  102.         if(val=="-1")
  103.             break;
  104.  
  105.         insert_at_last(val);
  106.     }
  107.  
  108. }
  109.  
  110.  
  111. void insert_at_last(string value)
  112. {
  113.     node *temp_node;
  114.     temp_node = (node *) malloc(sizeof(node));
  115.  
  116.     temp_node->number=value;
  117.     temp_node->next=NULL;
  118.  
  119.     //For the 1st element
  120.     if(head==NULL)
  121.     {
  122.         head=temp_node;
  123.         last=temp_node;
  124.     }
  125.     else
  126.     {
  127.         last->next=temp_node;
  128.         last=temp_node;
  129.     }
  130.  
  131. }
  132.  
  133.  
  134. void insert_at_first(string value)
  135. {
  136.     node *temp_node = (node *) malloc(sizeof(node));
  137.  
  138.     temp_node->number=value;
  139.     temp_node->next = head;
  140.  
  141.     head = temp_node;
  142. }
  143.  
  144. void insert_after(string key, string value)
  145. {
  146.     node *myNode = head;
  147.     int flag = 0;
  148.  
  149.     while(myNode!=NULL)
  150.     {
  151.         if(myNode->number==key)
  152.         {
  153.             node *newNode = (node *) malloc(sizeof(node));
  154.             newNode->number = value;
  155.             newNode->next = myNode->next;
  156.             myNode->next = newNode;
  157.  
  158.             printf("%s is inserted after %s\n", value.c_str(), key.c_str());
  159.  
  160.             flag = 1;
  161.  
  162.  
  163.             break;
  164.         }
  165.         else
  166.             myNode = myNode->next;
  167.     }
  168.  
  169.     if(flag==0)
  170.         printf("Key not found!\n");
  171.  
  172. }
  173.  
  174.  
  175. void delete_item(string value)
  176. {
  177.     node *myNode = head, *previous=NULL;
  178.     int flag = 0;
  179.  
  180.     while(myNode!=NULL)
  181.     {
  182.         if(myNode->number==value)
  183.         {
  184.             if(previous==NULL)
  185.                 head = myNode->next;
  186.             else
  187.                 previous->next = myNode->next;
  188.  
  189.             printf("%s is deleted from list\n", value.c_str());
  190.  
  191.             flag = 1;
  192.             free(myNode); //need to free up the memory to prevent memory leak
  193.             break;
  194.         }
  195.  
  196.         previous = myNode;
  197.         myNode = myNode->next;
  198.     }
  199.  
  200.     if(flag==0)
  201.         printf("Key not found!\n");
  202. }
  203.  
  204.  
  205. void search_item(string value)
  206. {
  207.     node *searchNode = head;
  208.     int flag = 0;
  209.  
  210.     while(searchNode!=NULL)
  211.     {
  212.         if(searchNode->number==value)
  213.         {
  214.             printf("%s is present in this list. Memory address is %d\n", value.c_str(), searchNode);
  215.             flag = 1;
  216.             break;
  217.         }
  218.         else
  219.             searchNode = searchNode->next;
  220.     }
  221.  
  222.     if(flag==0)
  223.         printf("Item not found\n");
  224.  
  225. }
  226.  
  227.  
  228. void print_linked_list()
  229. {
  230.     printf("\nYour full linked list is\n");
  231.  
  232.     node *myList;
  233.     myList = head;
  234.  
  235.     while(myList!=NULL)
  236.     {
  237.         //printf("%s ", myList->number);
  238.         cout<<myList->number<<endl;
  239.  
  240.         myList = myList->next;
  241.     }
  242.     puts("");
  243. }
Advertisement
Add Comment
Please, Sign In to add comment