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

Stack and Queue C++ Code

Uploaded by

F&B House
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Stack and Queue C++ Code

Uploaded by

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

Stack and queue Implementation

Queue using linked list


#include<iostream> void showfront() {
#include<conio.h> if(isempty())
using namespace std; cout<<"Queue is empty";
else
struct student_queue
cout<<front->data; }
{
void dsplay_queue()
int data; {
student_queue *next; if(isempty())
}; cout<<"Queue is empty";
student_queue *front=NULL; else {
student_queue *rear=NULL; student_queue *ptr=front;
while(ptr!=NULL) {
bool isempty() cout<<ptr->data<<" ";
ptr=ptr->next;
{
}}}
if(front==NULL and rear==NULL)
int main()
return true; {
else while(1)
return false; {
} cout<<"\n press 1 insert the data to the queue \n";
cout<<"\n press 2 remove the data from the queue \n";
void enqueue(int value) cout<<"\n press 3 display the top from the queue \n";
{ cout<<"\n press 4 display all data from the queue \n";
cout<<"\n press 5 exit \n";
student_queue *ptr = new student_queue();
cout<<"\n enter youe choice: ->";
ptr->data=value;
int ch;
ptr->next=NULL; cin>>ch;
if(front==NULL) system("cls");
{ switch(ch)
front=ptr; {
rear=ptr; case 1:
} cout<<"enter the data";
else int x;
cin>>x;
{
enqueue(x);
rear->next=ptr;
break;
rear=ptr; case 2:
}} dequeue();
void dequeue() break;
{ case 3:
if(isempty()) showfront();
cout<<"Queue is empty"; break;
else case 4:
dsplay_queue();
if(front==rear)
break;
{
case 5:
delete(front); exit(1);
front=rear=NULL; break;
} default:
else cout<<"Pls insert from 1-5";
{ break;
student_queue *ptr= front;
front=front->next; }}}
delete(ptr);
}}
Stack and queue Implementation

Queue using Array


#include <iostream> void Display() {
using namespace std; if (front == - 1)
int queue[100], n = 100, front = - 1, rear = - 1; cout<<"Queue is empty"<<endl;
void Insert() { else {
int val; cout<<"Queue elements are : ";
if (rear == n - 1) for (int i = front; i <= rear; i++)
cout<<"Queue Overflow"<<endl; cout<<queue[i]<<" ";
else { cout<<endl;
if (front == - 1) }
front = 0; }
cout<<"Insert the element in queue : "<<endl; int main() {
cin>>val; while(1)
rear++; {
queue[rear] = val; int ch;
} cout<<"1) Insert element to queue"<<endl;
} cout<<"2) Delete element from queue"<<endl;
void Delete() { cout<<"3) Display all the elements of queue"<<endl;
if (front == - 1 || front > rear) { cout<<"4) Exit"<<endl;
cout<<"Queue Underflow ";
return ; cout<<"Enter your choice : "<<endl;
} else { cin>>ch;
cout<<"Element deleted from queue is : "<< switch (ch) {
queue[front] <<endl; case 1: Insert();
front++;; break;
} case 2: Delete();
} break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
exit(1);
break;
default: cout<<"Invalid choice"<<endl;
}}
return 0;
}
Stack and queue Implementation

Stack using array


#include <iostream> void showtop() {
if(isempty())
using namespace std; cout<<"stack is empty";
else
#define size 90 cout<<"Top data is: "<<a[top]; }
void dsplay_queue() {
int a[size];
if(isempty())
int top=-1; cout<<"stack is empty";
else {
int count =-1; for(int i=0 ;i<=top;i++) {
cout<<a[i];
void push(int value) { }} }
int main()
if(top== size-1 ) {
while(1)
cout<<"stack is full \n"; {
cout<<"\n press 1 insert the data to the stack \n";
else
cout<<"\n press 2 remove the data from the stack \n";
top++; cout<<"\n press 3 display the top from the stack \n";
cout<<"\n press 4 display all data from the stack \n";
a[top]= value; cout<<"\n press 5 exit \n";
cout<<"\n enter youe choice: ->";
} int ch;
cin>>ch;
bool isempty() system("cls");
switch(ch)
{
{
if(top==-1) case 1:
cout<<"enter the data";
return true; int x;
cin>>x;
else push(x);
break;
return false; case 2:
pop();
} break;
case 3:
void pop()
showtop();
{ break;
case 4:
if(isempty()) dsplay_stack();
break;
cout<<"stack is empty"; case 5:
exit(1);
else break;
default:
{
cout<<"Pls insert from 1-5";
cout<<a[top]<<endl; break;

top--; }}}

}}
Stack and queue Implementation

Stack using linked list


#include <iostream> int main()
using namespace std; {
struct node while(1)
{ {
int data; cout<<"\n press 1 insert the data to the stack \n";
node *link; cout<<"\n press 2 remove the data from the stack \n";
}; cout<<"\n press 3 display the top from the stack \n";
node *top=NULL; cout<<"\n press 4 display all data from the stack \n";
bool isempty() cout<<"\n press 5 exit \n";
{ cout<<"\n enter youe choice: ->";
if(top==NULL) int ch;
return true; cin>>ch;
else system("cls");
return false; switch(ch)
} {
case 1:
void push(int value) { cout<<"enter the data";
node *ptr = new node(); int x;
ptr->data=value; cin>>x;
ptr->link=top; push(x);
top=ptr; break;
cout<<"yes \n"; case 2:
} pop();
void pop() break;
{ case 3:
if(isempty()) showtop();
cout<<"stack is empty"; break;
else case 4:
{ dsplay_stack();
node *ptr=top; break;
top=top->link; case 5:
delete(ptr); exit(1);
}} break;
void showtop() default:
{ cout<<"Pls insert from 1-5";
if(isempty()) break;
cout<<"stack is empty"; }
else
}
cout<<"Top data is: "<<top->data; }
}
void dsplay_stack() {
if(isempty())
cout<<"Queue is empty";
else {
node *ptr=top;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->link;
}}}

You might also like