habiba3azb

Untitled

Dec 23rd, 2022
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. >
  2. using namespace std;
  3. class Node
  4. {
  5. public:
  6.     int data;
  7.     Node* next;
  8.     Node()
  9.     {
  10.         data = 0;
  11.         next = NULL;
  12.     }
  13.  
  14. };
  15.  
  16. class Stack
  17. {
  18.     Node* top;
  19.  
  20. public:
  21.     Stack()
  22.     {
  23.         top = NULL;
  24.     }
  25.     bool isEmpty()
  26.     {
  27.         if (top == NULL)
  28.             return true;
  29.         else
  30.             return false;
  31.     }
  32.     bool isFull()
  33.     {
  34.         Node* ptr = new Node();
  35.         if (ptr == NULL)
  36.             cout << "The Stack is Full , cannot add more items \n";
  37.     }
  38.  
  39.     void push(int item)
  40.     {
  41.         Node* newnode = new Node();
  42.         newnode->data = item;
  43.         if (isEmpty())
  44.         {
  45.             newnode->next = NULL;
  46.             top = newnode;
  47.         }
  48.         else
  49.         {
  50.             newnode->next = top;
  51.             top = newnode;
  52.         }
  53.     }
  54.  
  55.     int pop()
  56.     {
  57.         int value;
  58.         Node* delptr = top;
  59.         value = top->data;
  60.         top = top->next;
  61.         delete delptr;
  62.         return value;
  63.     }
  64.  
  65.     int Peek()
  66.     {
  67.         return top->data;
  68.     }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.     void display()
  76.     {
  77.         Node* temp = top;
  78.         while (temp != NULL)
  79.         {
  80.             cout << temp->data << " ";
  81.             temp = temp->next;
  82.         }
  83.         cout << endl;
  84.     }
  85.  
  86.     int  count()
  87.     {
  88.         int counter = 0;
  89.         Node* temp = top;
  90.         while (temp != NULL)
  91.         {
  92.             counter++;
  93.             temp = temp->next;
  94.         }
  95.         return counter;
  96.     }
  97.  
  98.     bool IsFound(int item)
  99.     {
  100.         bool found = false;
  101.         Node* temp = top;
  102.         while (temp != NULL)
  103.         {
  104.             if (temp->data == item)
  105.                 found = true;
  106.             temp = temp->next;
  107.         }
  108.         return found;
  109.     }
  110.  
  111. };
  112.  
  113.  
  114. int main()
  115. {
  116.     int item;
  117.     Stack s;
  118.     for (int i = 0; i < 3; i++)
  119.     {
  120.         cout << "Enter Item to push \n";
  121.         cin >> item;
  122.         s.push(item);
  123.         s.display();
  124.     }
  125.  
  126.     /*cout << s.pop() << " was deleted from stack \n";
  127.     s.display();
  128.     cout << s.pop() << " was deleted from stack \n";
  129.     s.display();*/
  130.     cout << "Enter Item to search for \n";
  131.     cin >> item;
  132.    
  133.     if (s.IsFound(item))
  134.         cout << "Is Found \n";
  135.     else
  136.         cout << "Not found \n";
  137.     cout << s.count() <<endl;
  138.  
  139. }
  140.  
  141.  
Advertisement
Add Comment
Please, Sign In to add comment