Stack using Linked List
kaziamir
c_cpp
2 years ago
949 B
14
Indexable
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
int data;
struct node *prev;
}node;
node *top = NULL;
void push(int x){
cout<<"Pushing: "<<x<<endl;
node *newNode = (node *)malloc(sizeof(node));
newNode->data = x;
newNode->prev = top;
top = newNode;
}
void pop(){
node *p;
if(top==NULL){
cout<<"Can't pop. Stack Empty."<<endl;
}
else{
p = top;
top=top->prev;
cout<<"Poped : "<<p->data<<endl;
free(p);
}
}
void display(){
node *start = top;
cout<<"Current Stack: ";
while(start!=NULL){
cout<<start->data<<" ";
start = start->prev;
}
cout<<endl;
}
int main(){
push(2);
push(4);
display();
push(5);
display();
push(6);
display();
pop();
display();
pop();
display();
pop();
display();
pop();
display();
pop();
display();
return 0;
}
Editor is loading...