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

Stacks Using Array

This document contains C++ code to implement stacks and queues using arrays. The stack code defines a MAX size of 10, integer array stack of size 10, and variables to track the top index and elements. Functions are defined to push, pop, and display elements by manipulating the top index and array. Similarly, the queue code defines a MAX size of 20, integer array queue of size 20, and variables to track the front, rear, and elements. Functions are defined to insert, delete, and display elements by manipulating the front, rear indexes and array. Both implementations use do-while loops to get user input to call the push/pop, insert/delete, and display functions until the user

Uploaded by

ugopi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

Stacks Using Array

This document contains C++ code to implement stacks and queues using arrays. The stack code defines a MAX size of 10, integer array stack of size 10, and variables to track the top index and elements. Functions are defined to push, pop, and display elements by manipulating the top index and array. Similarly, the queue code defines a MAX size of 20, integer array queue of size 20, and variables to track the front, rear, and elements. Functions are defined to insert, delete, and display elements by manipulating the front, rear indexes and array. Both implementations use do-while loops to get user input to call the push/pop, insert/delete, and display functions until the user

Uploaded by

ugopi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

//stacks using array #include<iostream.

h>#define MAX 10int stack[10],i,top=-1,x,ch;void


push(){if(top==MAX-1)cout<< "\n Stack Overflow \n";else{cout<< "\n enter any element "; cin>>x;
stack[++top]=x;}}void pop(){if(top==-1)cout<< "/n Stack Underflow";else{ x=stack[top--];cout<<"\n
Poped element is \n"<<x; }}void display(){ if(top==-1) cout<<"\n Stack Underflow"; else { cout<<"\t
Stack elements are ";for(i=0;i<=top;i++)cout<<"\t" <<stack[i];}}void main(){do{cout<< "\n
1.Push";cout<< "\n 2.pop";cout<< "\n 3.display";cout<< "\n 4.exit";cout<< "\n enter your choice[1..4]
\n";cin>>ch;switch(ch){case 1:push();break;

case 2:pop(); break;case 3:display();break;}}while(ch!=4);}

//queues using arrays#include<iostream.h>#define MAX 10int queue[20],i,front=-1,rear=-1,x,ch;void insertion()

{if(rear==MAX-1)cout<<" \n Queue Overflow";else{cout<<"\n Enter any element \n";cin>>x;

queue[++rear]=x;if(front==-1)front++;}}void deletion(){if (front==-1 || front==MAX)cout<<"\n Queue


Underflow";else{x=queue[front++];cout<<"\n Deleted Element is:"<<x;}}void display(){if(front==-1 ||
front==MAX)\cout<<"\n Queue Underflow";else{cout<<"\n Queue Elements are:";for(i=front;i<=rear;i++)

cout<<"\t"<<queue[i];}}void main(){do{cout<<"\n 1.Insertion";cout<<"\n 2.Deletion";cout<<"\n 3.Display";

cout<<"\n 4.Exit";cout<<"\n Enter your choice [1..4] \n";cin>>ch;switch(ch){case 1:insertion();break;

case 2:deletion(); break;case 3:display(); break;}}while(ch!=4);}

You might also like