0% found this document useful (0 votes)
25 views7 pages

Dsa Lab Assingnment

The document contains code for two programs that implement stack and queue data structures using arrays in C++. Program 1 implements push, pop and display functions for a stack. Program 2 implements enqueue, dequeue and display functions for a queue.

Uploaded by

Muhammad Ahad
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)
25 views7 pages

Dsa Lab Assingnment

The document contains code for two programs that implement stack and queue data structures using arrays in C++. Program 1 implements push, pop and display functions for a stack. Program 2 implements enqueue, dequeue and display functions for a queue.

Uploaded by

Muhammad Ahad
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/ 7

Subject:

Data Structures and Algorithm


Submitted By:
Muhammad Ahad
Registration No:
FA22-BCS-287.
Section:
3-C.
Submitted To:
Sir Ghias ul din
ASSIGNMENT # 01 (LAB)

Program No. 1:
#include<iostream>

using namespace std;

int const maxsize=5;

int arr[maxsize];

int top=-1;

int count=0;

int element;

int choice;

void push(int stack[],int item)

{if(top==(maxsize-1))

cout<<"stack overflow "<<endl;

else

{top=top+1;

stack[top]=item;

count++;

}}

int pop(int stack[])

{int itemtop;

if(top==-1)

cout<<"stack underflow"<<endl;
else

{itemtop=stack[top];

top=top-1;

count--;

cout<<"\npop item is "<<itemtop<<endl;

void display(int stack[])

{ if (top==-1)

cout<<"stack is empty "<<endl;

else

{ cout<<"\ndisplaying stack elements"<<endl;

for(int i=top; i>=0; i--)

{cout<<stack[i]<<endl;}}

cout<<endl;

int main()

{cout<<"Program menu"<<endl<<endl;

cout<<"1. push an element"<<endl;

cout<<"2.pop an element"<<endl;

cout<<"3. display stack"<<endl;

cout<<"4. exit"<<endl;

do

cout<<"enter your choice= ";


cin>>choice;

switch (choice){

case 1:

cout<<"enter an element= ";

cin>>element;

push(arr,element);

break;

case 2:

element=pop(arr);

cout<<"element poped"<<element<<endl;

break;

case 3:

display(arr);

break;

case 4:

exit(0);

default:

cout<<"Invalid choice"<<endl;

} }while(choice!=4);

return 0;}

Program No. 2:
#include<iostream>

using namespace std;

const int maxsize=5;

int arr[maxsize];

int rear= -1;

int front= -1;

int count=0;

int choice;

int element;

void enqueue(int queue[],int data)

{if(rear==(maxsize-1))

cout<<"queue overflow "<<endl;

else

{rear=rear+1;

queue[rear]=data;

count++;

int dequeue(int queue[])

{ int item;

if((front==-1)|| (front>rear))

cout<<"queue underflow."<<endl;

else{

front=front+1;

item=queue[front];
count++;

return item;

void display(int queue[])

{ if (front==-1)

cout<<"queue is empty"<<endl;

else

cout<<"Displaying queue elements: "<<endl;

for(int i=rear; i>=0; i--)

{cout<<queue[i]<<endl;

int main()

{cout<<"Program menu"<<endl<<endl;

cout<<"1. enqueue an element"<<endl;

cout<<"2. dequeue an element"<<endl;

cout<<"3. display queue"<<endl;

cout<<"4. exit"<<endl;

do

cout<<"enter your choice= ";

cin>>choice;

switch (choice){

case 1:
cout<<"enter an element= ";

cin>>element;

enqueue(arr,element);

break;

case 2:

element=dequeue(arr);

cout<<"element dequeued"<<element<<endl;

break;

case 3:

display(arr);

break;

case 4:

exit(0);

default:

cout<<"Invalid choice"<<endl;

} }while(choice!=4);

return 0; }

You might also like