skb50bd

myQueue[QueueOperations]

Jun 13th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. struct myQueue {
  6.     int data = 0;
  7.     myQueue *next = NULL;
  8. } *Head;
  9.  
  10. int counT() {
  11.     int c = 0;
  12.     for (myQueue *Current = Head; Current != NULL; Current = Current -> next)
  13.         c++;
  14.     return c;
  15. }
  16.  
  17. void display() {
  18.     if (Head == NULL)
  19.         cout << "Queue is Empty, Nothing to Display.";
  20.     else
  21.         for (myQueue *Current = Head; Current != NULL; Current = Current -> next)
  22.             cout << Current -> data << " ";
  23.     cout << endl << endl;
  24. }
  25.  
  26. void enqueue(int num) {
  27.     myQueue *Current = Head;
  28.     myQueue *temp = new myQueue;
  29.     temp -> data = num;
  30.     temp -> next = NULL;
  31.  
  32.     if (Head == NULL)
  33.         Head = temp;
  34.     else {
  35.         while (Current -> next != NULL)
  36.             Current = Current -> next;
  37.         Current -> next = temp;
  38.     }
  39. }
  40.  
  41. void dequeue() {
  42.     if (Head == NULL)
  43.         cout << "Queue is Empty, Nothing to Dequeue." << endl;
  44.     else {
  45.         myQueue *temp = Head;
  46.         Head = Head -> next;
  47.         delete temp;
  48.     }
  49.     cout << endl;
  50. }
  51.  
  52. int main() {
  53.     int choice, data;
  54.  
  55.     while (true) {
  56.         cout << "Queue Operations" << endl;
  57.         cout << "----------------" << endl;
  58.         cout << "1. En-Queue" << endl;
  59.         cout << "2. De-Queue" << endl;
  60.         cout << "3. Display" << endl;
  61.         cout << "4. Size" << endl;
  62.         cout << "5. Exit" << endl;
  63.         cout << " --Choice: ";
  64.         cin >> choice;
  65.  
  66.         switch (choice) {
  67.             case 1: {
  68.                 cout << "Enter the Integer Value: ";
  69.                 cin >> data;
  70.                 cout << endl;
  71.                 enqueue(data);
  72.                 break;
  73.             }
  74.             case 2: {
  75.                 dequeue();
  76.                 break;
  77.             }
  78.             case 3: {
  79.                 display();
  80.                 break;
  81.             }
  82.             case 4: {
  83.                 cout << "Size: " << counT() << endl << endl;
  84.                 break;
  85.             }
  86.             case 5:
  87.                 exit(0);
  88.             default:
  89.                 cout << "Invalid Choice, Try Again." << endl << endl;
  90.         }
  91.  
  92.     }
  93.     return 0;
  94. }
Add Comment
Please, Sign In to add comment