[text] secret

Viewer

  1. // A complete working C++ program to
  2. // demonstrate all insertion methods
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. // A linked list node
  7. class Node
  8. {
  9.     public:
  10.     string data;
  11.     Node* next;
  12.     Node* prev;
  13. };
  14.  
  15.  
  16. void push(Node** head_ref, string new_data)
  17. {
  18.     /* 1. allocate node */
  19.     Node* new_node = new Node();
  20.  
  21.     /* 2. put in the data */
  22.     new_node->data = new_data;
  23.  
  24.     /* 3. Make next of new node as head
  25.     and previous as NULL */
  26.     new_node->next = (*head_ref);
  27.     new_node->prev = NULL;
  28.  
  29.     /* 4. change prev of head node to new node */
  30.     if ((*head_ref) != NULL)
  31.         (*head_ref)->prev = new_node;
  32.  
  33.     /* 5. move the head to point to the new node */
  34.     (*head_ref) = new_node;
  35. }
  36.  
  37. void insertAfter(Node* prev_node, string new_data)
  38. {
  39.     /*1. check if the given prev_node is NULL */
  40.     if (prev_node == NULL)
  41.     {
  42.         cout<<"the given previous node cannot be NULL";
  43.         return;
  44.     }
  45.  
  46.     /* 2. allocate new node */
  47.     Node* new_node = new Node();
  48.  
  49.     /* 3. put in the data */
  50.     new_node->data = new_data;
  51.  
  52.     /* 4. Make next of new node as next of prev_node */
  53.     new_node->next = prev_node->next;
  54.  
  55.     /* 5. Make the next of prev_node as new_node */
  56.     prev_node->next = new_node;
  57.  
  58.     /* 6. Make prev_node as previous of new_node */
  59.     new_node->prev = prev_node;
  60.  
  61.     /* 7. Change previous of new_node's next node */
  62.     if (new_node->next != NULL)
  63.         new_node->next->prev = new_node;
  64. }
  65.  
  66. void append(Node** head_ref, string new_data)
  67. {
  68.     /* 1. allocate node */
  69.     Node* new_node = new Node();
  70.  
  71.     Node* last = *head_ref; /* used in step 5*/
  72.  
  73.     /* 2. put in the data */
  74.     new_node->data = new_data;
  75.  
  76.     /* 3. This new node is going to be the last node, so
  77.         make next of it as NULL*/
  78.     new_node->next = NULL;
  79.  
  80.     /* 4. If the Linked List is empty, then make the new
  81.         node as head */
  82.     if (*head_ref == NULL)
  83.     {
  84.         new_node->prev = NULL;
  85.         *head_ref = new_node;
  86.         return;
  87.     }
  88.  
  89.     /* 5. Else traverse till the last node */
  90.     while (last->next != NULL)
  91.         last = last->next;
  92.  
  93.     /* 6. Change the next of last node */
  94.     last->next = new_node;
  95.  
  96.     /* 7. Make last node as previous of new node */
  97.     new_node->prev = last;
  98.  
  99.     return;
  100. }
  101.  
  102. void deleteNode(Node** head_ref, string key)
  103. {
  104.      
  105.     // Store head node
  106.     Node* temp = *head_ref;
  107.     Node* prev = NULL;
  108.      
  109.     // If head node itself holds
  110.     // the key to be deleted
  111.     if (temp != NULL && temp->data == key)
  112.     {
  113.         *head_ref = temp->next; // Changed head
  114.         delete temp;            // free old head
  115.         return;
  116.     }
  117.  
  118.     // Else Search for the key to be deleted,
  119.     // keep track of the previous node as we
  120.     // need to change 'prev->next' */
  121.       else
  122.     {
  123.     while (temp != NULL && temp->data != key)
  124.     {
  125.         prev = temp;
  126.         temp = temp->next;
  127.     }
  128.  
  129.     // If key was not present in linked list
  130.     if (temp == NULL)
  131.         return;
  132.  
  133.     // Unlink the node from linked list
  134.     prev->next = temp->next;
  135.  
  136.     // Free memory
  137.     delete temp;
  138.     }
  139. }
  140.  
  141. bool search(Node* head, string ngalan) 
  142.     Node* current = head; // Initialize current 
  143.     while (current != NULL) 
  144.     { 
  145.         if (current->data == ngalan) 
  146.         {
  147.             cout << "Found "; 
  148.             current = current->next;
  149.             return 1;
  150.         }
  151.         else
  152.         {
  153.             cout << "Not found "; 
  154.             return 1;
  155.         }
  156.             
  157.     } 
  158.     
  159.  
  160. void printList(Node* node)
  161. {
  162.     Node* last;
  163.     cout<<"\nAng naa sa sulod sa list kay silang: \n";
  164.     while (node != NULL)
  165.     {
  166.         cout<<" "<<node->data<<" ";
  167.         last = node;
  168.         node = node->next;
  169.     }
  170.  }
  171.  
  172. /* Driver program to test above functions*/
  173. int main()
  174. {
  175.     int number;
  176.     string ngalan;
  177.     /* Start with the empty list */
  178.     Node* head = NULL;
  179.           for (int i = 0; i < 5; i++) 
  180.         {
  181.  
  182.             cout << "Unsa imong ngalan: ";
  183.             cin >> ngalan;
  184.             // Insert 7 at the beginning. So
  185.             // linked list becomes 7->6->NULL
  186.             push(&head, ngalan);
  187.  
  188.             printList(head);
  189.             cout << "\n";
  190.         }
  191.         for (int i = 0; i < 2; i++) 
  192.         {
  193.  
  194.             cout << "Kinsay e delete? ";
  195.             cin >> ngalan;
  196.             // Insert 7 at the beginning. So
  197.             // linked list becomes 7->6->NULL
  198.             deleteNode(&head, ngalan);
  199.  
  200.             printList(head);
  201.             cout << "\n";
  202.         }
  203.         for (int i = 0; i < 2; i++) 
  204.         {
  205.  
  206.             cout << "Kinsay e search? ";
  207.             cin >> ngalan;
  208.             // Insert 7 at the beginning. So
  209.             // linked list becomes 7->6->NULL
  210.             search(head, ngalan);
  211.  
  212.             printList(head);
  213.             cout << "\n";
  214.         }
  215.  
  216.  
  217.  
  218. }

Editor

You can edit this paste and save as new:


File Description
  • secret
  • Paste Code
  • 20 Sep-2021
  • 4.91 Kb
You can Share it: