fueanta

LINKED List BASICS

Oct 30th, 2016
153
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // AUTHOR : FUEANTA
  2. // DATE : 31 OCT, 2016
  3.  
  4. #include "iostream"
  5. using namespace std;
  6.  
  7. struct node {
  8.     int number;
  9.     node *next;
  10. };
  11.  
  12. bool IsEmpty(node *);
  13. int menu();
  14. void insertAsFirstElement(node *&, node *&, int);
  15. void insert(node *&, node *&, int);
  16. void remove(node *&, node *&);
  17. void showList(node *);
  18.  
  19. int main() {
  20.     node *head = NULL;
  21.     node *tail = NULL;
  22.     int choice;
  23.     int num;
  24.     do {
  25.         choice = menu();
  26.         switch(choice) {
  27.         case 1 :    cout << "Please enter a number: ";
  28.                     cin >> num;
  29.                     insert(head, tail, num);
  30.                     break;
  31.         case 2 :    remove(head, tail);
  32.                     break;
  33.         case 3 :    showList(head);
  34.                     break;
  35.         default:    cout << "\nChoose 4 to exit." << endl;
  36.                     break;
  37.         }
  38.  
  39.     } while (choice != '4');
  40.     return 0;
  41. }
  42.  
  43. bool IsEmpty(node *head) {
  44.     if (head == NULL)
  45.         return true;
  46.     else return false;
  47. }
  48.  
  49. int menu() {
  50.     int choice;
  51.     cout << "\n**MENU**\n" << endl;
  52.     cout << "1. Insert an item." << endl;
  53.     cout << "2. Delete an item." << endl;
  54.     cout << "3. Show the list." << endl;
  55.     cout << "4. Exit." << endl;
  56.     cout << "\nCHOICE : ";
  57.     cin >> choice; return choice;
  58. }
  59.  
  60. void insertAsFirstElement(node *&head, node *&tail, int num) {
  61.     node *temp = new node;
  62.     temp->number = num;
  63.     temp->next = NULL;
  64.     head = temp;
  65.     tail = temp;
  66. }
  67.  
  68. void insert(node *&head, node *&tail, int num) {
  69.     if (IsEmpty(head))
  70.         insertAsFirstElement(head, tail, num);
  71.     else {
  72.         node *temp = new node;
  73.         temp->number = num;
  74.         temp->next = NULL;
  75.         tail->next = temp;
  76.         tail = temp;
  77.     }
  78. }
  79.  
  80. void remove(node *&head, node *&tail) {
  81.     if (IsEmpty(head))
  82.         cout << "\nThe list is already empty." << endl;
  83.     else if (head == tail) {
  84.         delete head;
  85.         cout << "\nNode removed." << endl;
  86.         head = tail = NULL;
  87.     }
  88.     else {
  89.         node *temp = head;
  90.         head = head->next;
  91.         delete temp;
  92.         cout << "\nNode removed." << endl;
  93.     }
  94. }
  95.  
  96. void showList(node *current) {
  97.     if (IsEmpty(current))
  98.         cout << "\nNothing to show. The list is empty." << endl;
  99.     else {
  100.         cout << "\nThe list contains : ";
  101.         while (current != NULL) {
  102.             cout << current->number << " ";
  103.             current = current->next;
  104.         }
  105.     }
  106. }
RAW Paste Data