0% found this document useful (0 votes)
13 views

Code For Impelementing Stack by Using Linked List

This program implements a stack using a linked list. It defines push, pop, peek and display functions to add and remove elements from the stack and view its contents. The main function uses a menu to allow the user to test the stack operations and view the results.

Uploaded by

Tasneem Kubra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Code For Impelementing Stack by Using Linked List

This program implements a stack using a linked list. It defines push, pop, peek and display functions to add and remove elements from the stack and view its contents. The main function uses a menu to allow the user to test the stack operations and view the results.

Uploaded by

Tasneem Kubra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a program to implement stack by using linked list

#include <bits/stdc++.h> //free fuction works under this file


#include<iostream>
void line();
void star();
using namespace std;
struct Node
{
int data;
struct Node* link;
};
struct Node* top;
void push(int data)
{
struct Node* temp;
temp = new Node();
if (!temp)
{
cout << "\nHeap Overflow";
exit(1);
}
temp->data = data;
temp->link = top;
top = temp;
}
int isEmpty()
{
return top == NULL;
}
int peek()
{
if (!isEmpty())
return top->data;
else
exit(1);
}
void pop()
{
struct Node* temp;
if (top == NULL)
{
cout << "\nStack Underflow" << endl;
exit(1);
}
else
{
temp = top;
cout<<top->data<<" has been poped.\n";
top = top->link;
temp->link = NULL;
free(temp);
}
}
void display()
{
struct Node* temp;
if (top == NULL)
{
cout << "\nStack Underflow";
exit(1);
}
else
{
temp = top;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->link;
}
}
}
int main()
{
star();
cout<<"Program objective : This program implements Stack by using linked list.\n";
star();
/*cout<<"Enter the value to put in stack : ";
int num;
push(11);
push(22);
push(33);
push(44);
display();
cout << "\nTop element is "
<< peek() << endl;
pop();
pop();
display();
cout << "\nTop element is "
<< peek() << endl;
*/
cout<<"______________Enter 1 To push\n";
cout<<"______________Enter 2 To Pop\n";
cout<<"______________Enter 3 To display\n";
cout<<"______________Enter 0 To exit\n\n";
cout<<"Enter choice : ";
int chk;
cin>>chk;
cout<<endl;
while(chk!=0)
{
if(chk==1)
{
cout<<"---------------Pushing the number--------------\n\n";
cout<<"Enter the number to push in stack : ";
int num;
cin>>num;
push(num);
}
else if(chk==2)
{
cout<<"---------------Poping the number--------------\n\n";
pop();//<<" has been poped.\n";
}
else if(chk==3)
{
cout<<"---------------Displaying the numbers--------------\n\n";
display();
}
else
{
cout<<"\a\nWrong input.";
}
cout<<"\n______________Enter 1 To push\n";
cout<<"______________Enter 2 To Pop\n";
cout<<"______________Enter 3 To display\n";
cout<<"______________Enter 0 To exit\n\n";
cout<<"Enter choice : ";
cin>>chk;
cout<<endl;
}
return 0;
}
void line()
{
for(int i=1; i<45; i++)
{
cout<<"-";
}
cout<<endl;
}
void star()
{
cout<<endl;
for(int i=1; i<37;i++)
{
cout<<"**";
}
cout<<endl;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy