Advertisement
apl-mhd

StackWithLinkedList

Apr 1st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. struct StackFarame{
  6.    
  7.     int data;
  8.     struct StackFarame *next;
  9.    
  10.     };
  11.    
  12. typedef struct StackFarame node;
  13.  
  14.     node *top;
  15.    
  16.    
  17.    
  18.    
  19. void Push(int x){
  20.    
  21.     node * temp;
  22.    
  23.     temp = new node();
  24.    
  25.     temp->data =x;
  26.     temp->next = top;
  27.     top= temp;
  28.    
  29.    
  30. }
  31.  
  32. void Print(){
  33.        
  34.     node *temp;
  35.     temp = top;
  36.     while(temp !=NULL){
  37.         cout<<temp->data<<" ";
  38.         temp=temp->next;
  39.      
  40.     }
  41.     cout<<endl;
  42. }
  43.  
  44. void Pop(){
  45.    
  46.     node *temp;
  47.     if(top == NULL)
  48.         cout<<"underflow"<<endl;
  49.     else{
  50.        
  51.         temp = top;
  52.         top= temp->next;
  53.         delete temp;
  54.        
  55.     }
  56.  
  57.    
  58.  
  59. }
  60.  
  61.  void Top(){
  62.    
  63.     if(top !=NULL)
  64.     cout<<top->data<<endl;
  65.    
  66. }
  67.  
  68.  
  69. int main(int argc, char **argv)
  70. {
  71.     top = NULL;
  72.    
  73.     Push(10);
  74.     Push(20);
  75.     Push(30);
  76.     Top();
  77.    
  78.    
  79.    
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement