Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. //Stack
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define MAX 1000
  7.  
  8. class Stack
  9. {
  10.     int top;
  11. public:
  12.     int a[MAX];
  13.  
  14.     Stack() { top = -1; }
  15.     bool push(int x);
  16.     int pop();
  17.     bool isEmpty();
  18. };
  19.  
  20. bool Stack::push(int x)
  21. {
  22.     if (top >= (MAX-1))
  23.     {
  24.         cout << "Stack Overflow";
  25.         return false;
  26.     }
  27.     else
  28.     {
  29.         a[++top] = x;
  30.         cout<<x <<" pushed into stack\n";
  31.         return true;
  32.     }
  33. }
  34.  
  35. int Stack::pop()
  36. {
  37.     if (top < 0)
  38.     {
  39.         cout << "Stack Underflow";
  40.         return 0;
  41.     }
  42.     else
  43.     {
  44.         int x = a[top--];
  45.         return x;
  46.     }
  47. }
  48.  
  49. bool Stack::isEmpty()
  50. {
  51.     return (top < 0);
  52. }
  53.  
  54. int main()
  55. {
  56.     class Stack s;
  57.     s.push(10);
  58.     s.push(20);
  59.     s.push(30);
  60.     cout<< "-------------------\n";
  61.     cout<<s.pop() << " Popped from stack\n";
  62.     cout<<s.pop() << " Popped from stack\n";
  63.     cout<<s.pop() << " Popped from stack\n";
  64.     cout<<s.pop() << " Popped from stack\n";
  65.  
  66.     return 0;
  67. }
  68.  
  69. QUEUE:
  70.  
  71. #include <iostream>
  72. #define SIZE 5
  73.  
  74. using namespace std;
  75.  
  76. class Queue {
  77.     int items[SIZE], front, rear;
  78.  
  79. public:
  80.     Queue(){
  81.         front = -1;
  82.         rear = -1;
  83.     }
  84.  
  85.     bool isFull();
  86.     bool isEmpty();
  87.     void enQueue(int element);
  88.     int deQueue();
  89.     void display();
  90.  
  91. };
  92.  
  93. bool Queue::isFull(){
  94.         if(front == 0 && rear == SIZE - 1){
  95.             return true;
  96.         }
  97.         if(front == rear + 1) {
  98.             return true;
  99.         }
  100.         return false;
  101.     }
  102.  
  103. bool Queue::isEmpty(){
  104.         if(front == -1)
  105.             return true;
  106.         else
  107.             return false;
  108. }
  109. void Queue::enQueue(int element){
  110.         if(isFull()){
  111.             cout << "\nQueue is full\n---------------" << endl;
  112.         } else {
  113.             if(front == -1) front = 0;
  114.             rear = (rear + 1) % SIZE;
  115.             items[rear] = element;
  116.             cout << endl << "-------------\nInserted " << element << endl;
  117.         }
  118. }
  119.  
  120.  
  121. int Queue::deQueue(){
  122.         int element;
  123.         if(isEmpty()){
  124.             cout << "Queue is empty" << endl;
  125.             return(-1);
  126.         } else {
  127.             element = items[front];
  128.             if(front == rear){
  129.                 front = -1;
  130.                 rear = -1;
  131.             } /* Q has only one element, so we reset the queue after deleting it. */
  132.             else {
  133.                 front=(front+1) % SIZE;
  134.             }
  135.             return(element);
  136.         }
  137.     }
  138.  
  139. void Queue::display(){
  140.         int i;
  141.         if(isEmpty()) {
  142.             cout << endl << "Empty Queue" << endl;
  143.         }
  144.         else
  145.         {
  146.             cout << "\nFront -> " << front;
  147.             cout << endl << "Items -> ";
  148.             for(i=front; i!=rear;i=(i+1)%SIZE)
  149.                 cout << items[i] <<" ";
  150.             cout << items[i];
  151.             cout << endl << "Rear -> " << rear;
  152.         }
  153.     }
  154.  
  155.  
  156.  
  157. int main()
  158. {
  159.     Queue q;
  160.  
  161.     q.deQueue();
  162.  
  163.     q.enQueue(1);
  164.     q.display();
  165.     q.enQueue(2);
  166.     q.display();
  167.     q.enQueue(3);
  168.     q.display();
  169.     q.enQueue(4);
  170.     q.display();
  171.     q.enQueue(5);
  172.     q.display();
  173.  
  174.     q.enQueue(6);
  175.     q.display();
  176.  
  177.     int elem = q.deQueue();
  178.     if( elem != -1)
  179.        cout << endl << "Deleted Element is " << elem;
  180.  
  181.     q.display();
  182.     q.enQueue(7);
  183.     q.display();
  184.     q.enQueue(8);
  185.  
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement