Created
October 4, 2016 18:08
-
-
Save Parassharmaa/d826fedf1238518e3d2d6cbf2e826ad3 to your computer and use it in GitHub Desktop.
stack implementation via linked_list
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 <iostream> | |
#include <cstring> | |
using namespace std; | |
struct node { | |
int info; | |
node * next; | |
}*start; | |
class ll { | |
int top; | |
public: | |
ll() { | |
start = NULL; | |
} | |
node* create_node(int); | |
void insert(int i); | |
int del(); | |
}; | |
class stack:public ll { | |
public: | |
void push(int n); | |
int pop(); | |
}; | |
int main() { | |
stack s; | |
s.push(30); | |
s.push(32); | |
s.push(23); | |
cout<<s.pop()<<" "<<s.pop()<<" "<<s.pop()<<s.pop(); | |
return 0; | |
} | |
node* ll::create_node(int n) { | |
node* temp = new node; | |
if(temp==NULL) { | |
cout<<"Overflow"; | |
return 0; | |
} | |
else { | |
temp->info = n; | |
temp->next = NULL; | |
return temp; | |
} | |
} | |
void ll::insert(int i) { | |
node *temp, *r; | |
temp = create_node(i); | |
if(start==NULL) { | |
start = temp; | |
} | |
else { | |
r = start; | |
start = temp; | |
start->next = r; | |
} | |
} | |
int ll::del() { | |
if(start==NULL) { | |
cout<<"Underflow"; | |
} | |
else { | |
int s; | |
s = start->info; | |
node *r; | |
r = start; | |
start = r->next; | |
return s; | |
} | |
} | |
void stack::push(int n) { | |
insert(n); | |
} | |
int stack::pop() { | |
int n; | |
n =del(); | |
return n; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment