Skip to content

Instantly share code, notes, and snippets.

@susanhsrestha
Created January 23, 2020 13:01
Show Gist options
  • Save susanhsrestha/9969726563d42d44e83615205a3cd2b5 to your computer and use it in GitHub Desktop.
Save susanhsrestha/9969726563d42d44e83615205a3cd2b5 to your computer and use it in GitHub Desktop.
Dynamic Stack ADT operations using C++
#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