Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct ListNode {
  5.     string data;
  6.     struct ListNode* next;
  7. };
  8. void insert(ListNode& node, string value);
  9. bool del(ListNode& node, string value);
  10. int isEmpty(const ListNode& node);
  11. void printList(const ListNode& start);
  12. void instructions(void);
  13. int main(void) {
  14.     ListNode start = {"dummy", nullptr};
  15.     int choice;
  16.     string item;
  17.     instructions();
  18.     cout << "? ";
  19.     cin >> choice;
  20.     while (choice != 3) {
  21.         switch (choice) {
  22.             case 1:
  23.                 cout << "Enter a string: ";
  24.                 cin >> item;
  25.                 insert(start, item);
  26.                 printList(start);
  27.                 break;
  28.             case 2:
  29.                 if (!isEmpty(start)) {
  30.                     cout << "Enter string to be deleted: ";
  31.                     cin >> item;
  32.                     if (del(start, item)) {
  33.                         cout << item << " deleted." << endl;
  34.                         printList(start);
  35.                     } else {
  36.                         cout << item << " not found." << endl << endl;
  37.                     }
  38.                 } else {
  39.                     cout << "List is empty." << endl;
  40.                 }
  41.                 break;
  42.             default:
  43.                 cout << "Invalid choice." << endl;
  44.                 instructions();
  45.                 break;
  46.         }
  47.         cout << "? ";
  48.         cin >> choice;
  49.     }
  50.     cout << "End of run." << endl;
  51. }
  52. void instructions(void) {
  53.     cout << "Enter your choice:\n"
  54.          << " 1 to insert an element into the list.\n"
  55.          << " 2 to delete an element from the list.\n"
  56.          << " 3 to end." << endl;
  57. }
  58. void insert(ListNode& node, string value) {
  59.     if (node.next) {
  60.         if (value < node.next->data) {
  61.             ListNode* next = node.next;
  62.             ListNode* newNode = new ListNode({value, next});
  63.             node.next = newNode;
  64.         } else {
  65.             insert(*(node.next), value);
  66.         }
  67.         return;
  68.     }
  69.     ListNode* newNode = new ListNode({value, nullptr});
  70.     node.next = newNode;
  71. }
  72. bool del(ListNode& node, string value) {
  73.     if (node.next != nullptr) {
  74.         if (node.next->data == value) {
  75.             ListNode* tmp = node.next;
  76.             node.next = node.next->next;
  77.             free(tmp);
  78.             return true;
  79.         } else {
  80.             return del(*(node.next), value);
  81.         }
  82.     }
  83.     return false;
  84. }
  85. int isEmpty(const ListNode& node) {
  86.     return node.next == nullptr;
  87. }
  88. void printList(const ListNode& start) {
  89.     if (isEmpty(start)) {
  90.         cout << "List is empty." << endl;
  91.         return;
  92.     }
  93.     cout << "The list is:" << endl;
  94.     ListNode tmp = start;
  95.     while (tmp.next != nullptr) {
  96.         cout << tmp.next->data << " --> ";
  97.         tmp = *(tmp.next);
  98.     }
  99.     cout << "NULL" << endl;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement