Last active
September 21, 2015 13:28
-
-
Save tanvir002700/ccbfed186d193a7166ea to your computer and use it in GitHub Desktop.
Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
class Stack | |
{ | |
private: | |
class node | |
{ | |
public: | |
int data; | |
node *next; | |
node() | |
{ | |
next=NULL; | |
} | |
}*Head; | |
int Size; | |
public: | |
Stack(); | |
void insert(int n); | |
int top(); | |
void pop(); | |
int size(); | |
}; | |
Stack::Stack() | |
{ | |
Head=NULL; | |
Size=0; | |
} | |
void Stack::insert(int n) | |
{ | |
Size++; | |
node *temp; | |
temp =new node; | |
temp->data=n; | |
if(Head==NULL) | |
{ | |
temp->next=NULL; | |
Head=temp; | |
return; | |
} | |
temp->next=Head; | |
Head=temp; | |
} | |
int Stack::top() | |
{ | |
return Head->data; | |
} | |
void Stack::pop() | |
{ | |
Size--; | |
node *temp=Head; | |
Head=Head->next; | |
delete(temp); | |
} | |
int Stack::size() | |
{ | |
return Size; | |
} | |
int main() | |
{ | |
Stack stk; | |
stk.insert(2); | |
stk.insert(19); | |
printf("%d\n",stk.size()); | |
printf("%d\n",stk.top()); | |
stk.pop(); | |
printf("%d\n",stk.top()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment