Created
January 23, 2020 13:01
-
-
Save susanhsrestha/9969726563d42d44e83615205a3cd2b5 to your computer and use it in GitHub Desktop.
Dynamic Stack ADT operations using C++
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<conio.h> | |
using namespace std; | |
struct Node{ | |
int data; | |
Node *next; | |
}; | |
struct Node* head = NULL; // empty list creation | |
void push(int data){ | |
Node* temp = new Node; | |
temp->data = data; | |
temp->next = NULL; | |
if(head == NULL){ | |
head = temp; | |
return; | |
} | |
temp->next = head; | |
head = temp; | |
} | |
void pop(){ | |
if(head == NULL){ | |
cout<<"EMPTY STACK"<<endl; | |
return; | |
} | |
Node *temp = head; | |
cout<<"Deleted Element: "<<temp->data; | |
head = temp->next; | |
delete temp; | |
} | |
void front(){ | |
cout<<"The top element of the stack is "<<head->data; | |
} | |
void display(){ | |
cout<<"Stack is: "; | |
if(head == NULL){ | |
cout<<"Empty"<<endl; | |
return; | |
} | |
Node* temp = head; | |
while(temp!=NULL){ | |
cout<<temp->data<<" "; | |
temp = temp->next; | |
} | |
cout<<endl; | |
} | |
int main(){ | |
pop(); | |
display(); | |
push(3); | |
display(); | |
push(7); | |
display(); | |
front(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment