Advertisement
Shahd_Elmeniawy

Stack and Queue

Feb 7th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class node{
  6.     public:
  7.     int data;
  8.     node* Next;
  9.     node(int num = 0){
  10.         data = num;
  11.         Next = nullptr;
  12.     }
  13. };
  14.  
  15.  
  16. class Stack{
  17.     public:
  18.     node* top;
  19.     int size;
  20.     Stack(){
  21.         top = nullptr;
  22.         size = 0;
  23.     }
  24.  
  25.     void push(int num){
  26.         node* newnode = new node(num);
  27.         newnode ->Next = top;
  28.         top = newnode;
  29.         size++;
  30.     }
  31.  
  32.     int printSt(){
  33.         if(top == nullptr) return -1;
  34.         else return top->data;
  35.     }
  36.  
  37.     void pop(){
  38.         if(top){
  39.         node* deletenode = top;
  40.         top = top->Next;
  41.         delete deletenode;
  42.         size--;}
  43.     }
  44.  
  45.     int Size(){
  46.         return size;
  47.     }
  48.  
  49.     bool is_empty(){
  50.         return (top == nullptr);
  51.     }
  52.  
  53.  
  54.     ~Stack(){
  55.         while(top){
  56.             node* newnode = top;
  57.             top = top -> Next;
  58.             delete newnode;
  59.         }
  60.     }
  61.  
  62. };
  63.  
  64.  
  65. class Queue{
  66.     public:
  67.     node* front;
  68.     node* back;
  69.     int size;
  70.  
  71.     Queue(){
  72.         front = nullptr;
  73.         back = front;
  74.         size = 0;
  75.     }
  76.  
  77.     int Size(){
  78.         return size;
  79.     }
  80.  
  81.     void push(int num){
  82.        if(!front){
  83.         back = front = new node(num);
  84.         }else{
  85.             back->Next = new node(num);
  86.             back = back->Next;
  87.         }
  88.         size++;
  89.        
  90.     }
  91.  
  92.     int Front(){
  93.         if(front){
  94.             return front->data;
  95.         }
  96.         return -1;
  97.     }
  98.  
  99.     int Back(){
  100.         if(back){
  101.             return back->data;
  102.         }
  103.         return -1;
  104.     }
  105.  
  106.     void Pop(){
  107.         if(front){
  108.             node* deletenode = front;
  109.             front = front->Next;
  110.             delete deletenode;
  111.             size--;
  112.         }
  113.     }
  114.  
  115.     bool is_empty(){
  116.         return (front == nullptr);
  117.     }
  118.  
  119.     ~Queue(){
  120.         while(front){
  121.             node* newnode = front;
  122.             front = front -> Next;
  123.             delete newnode;
  124.         }
  125.     }
  126.  
  127. };
  128.  
  129.  
  130.  
  131.  
  132. int main(){
  133.     Stack st;
  134.     st.push(9);
  135.     st.push(10);
  136.     st.push(8);
  137.     cout << st.printSt() << endl;
  138.     st.pop();
  139.     cout << st.printSt() << endl;
  140.     st.pop();
  141.     st.pop();
  142.     cout << "size : " << st.Size() << endl;
  143.     cout << st.printSt() << endl;
  144.  
  145.  
  146.     /////////////////////////////////////////////////////
  147.     Queue q;
  148.     cout << q.is_empty() << endl;
  149.     q.push(10);
  150.     q.push(8);
  151.     q.push(11);
  152.     q.push(13);
  153.     cout << q.Front() << " " << q.Back() << endl;
  154.     q.Pop();
  155.     cout << q.Front() << " " << q.Back() << endl;
  156.     cout << q.is_empty() << " " << q.Size();
  157.  
  158.    
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement