Skip to content

Instantly share code, notes, and snippets.

@tanvir002700
Last active September 21, 2015 13:28
Show Gist options
  • Save tanvir002700/ccbfed186d193a7166ea to your computer and use it in GitHub Desktop.
Save tanvir002700/ccbfed186d193a7166ea to your computer and use it in GitHub Desktop.
Stack
#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