0% found this document useful (0 votes)
47 views2 pages

Sta List

This C++ program implements a dynamic stack using pointers. It defines push and pop functions to add and remove elements from the stack. The main function demonstrates pushing multiple values onto the stack, popping values off, and checking the top value.

Uploaded by

Eshaal Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

Sta List

This C++ program implements a dynamic stack using pointers. It defines push and pop functions to add and remove elements from the stack. The main function demonstrates pushing multiple values onto the stack, popping values off, and checking the top value.

Uploaded by

Eshaal Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

//#include<conio.h>

int pop();
void push(int);

//stack declaration
struct stack{
int info;
stack *next;
};

stack *temp = NULL,*top = NULL;

using namespace std;

// Function to push element in a dynamic stack


void push(int x){

temp = new stack();


temp->info = x;
temp->next = NULL;

if(top == NULL)
top = temp;
else{

temp->next = top;
top = temp;
}
cout<<" \n The value pushed into stack is "<<top->info;
}

//Function to delete the element from a queue.


int pop (){

int y;

if(top == NULL){

cout<<"stack underflow.";
return 0 ;
}
y = top->info;
temp = top;
top = top->next;
delete temp;

return y;
}
void topvalue(){
cout<<"\n The value at the top of the stack is "<< top->info;
}

/*Main program to be executed*/

int main(){
//clrscr();

push(10);
push(20);
cout<< "\nPoped value is "<<pop();
topvalue();
push(30);
push(40);
cout<< "\nPoped value is "<<pop();
topvalue();
push(50);
push(60);
cout<< "\nPoped value is "<<pop();
topvalue();
//getch();
return 0;

You might also like