Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Debre Markos University: Department of Information Technology

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Debre Markos University

Department of Information Technology


Data Structure and Algorithm Lab Manual

Lab 4: Array implementation of Stack and Queue operations


Tools used: Quincy 2005v.1.3 editor
Objective:
Students should be able to know:
- What push and pop operations of stack are, and their implementation using array
- What enqueue and dequeue operations of queue are, and their implementation using array
Source code for array implementation of Stack operations (PUSH and POP )
#include<iostream.h>
#define size 10
int Stack[size]; int main()
int top = -1; {
void push(int x) int s;
{ int a[10];
if(top = = size) cout<<"How many elements do u want to store in
cout<<"\n Stack is full."<<endl; a stack (<10):";
else cin>>s;
{ cout<<"\n\t Enter:\n";
top++; for(int i=0;i<s; i ++)
Stack[top] = x; {
}} cout<<"\t\t element "<<(i+1)<<" :";
int pop() cin>>a[i];
{ push(a[i]);
if(top = = -1) }
{ cout<<"\n The stack is:"<<endl;
cout<<"\n Stack is empty."<<endl; display();
return -1; int t;
} for(int k=1;k<=s; k ++)
int t = Stack[top]; {
top--; t=pop();
return t; if(k= =1)
} cout<<"1st popped element is : "<<t<<endl;
void display() else if(k==2)
{ cout<<"2nd popped element is : "<<t<<endl;
int t=top; else if(k==3)
while(t != -1) cout<<"3rd popped element is : "<<t<<endl;
{ else
cout<<Stack[t]<<endl; cout<<k<<"th popped element is : "<<t<<endl;
t--; }
} }
}

Page - 1
Source code for array implementation of Queue operations (ENQUEUE and DEQUEUE)

#include<iostream.h> {
#define size 10 int a[size];
int queue[size]; int s;
int front = -1, rear = -1; cout<<"How many elements do you have for
int isempty() queue?(<10):";
{ cin>>s;
return front = = -1; cout<<"\n Enter...\n";
} for(int i=0;i<s; i++)
int isfull() {
{return ((rear = = size-1)); cout<<"\t element "<<(i+1)<<" :";
} cin>>a[i];
void enqueue(int item) enqueue(a[i]);
{ }
if(isfull()) cout<<"\n The queue looks like:\n";
cout<<"Overflow, Queue is full.\n"; display();
else cout<<endl;
{ int t;
queue[++rear]=item; for(int k = 1;k <= s; k++)
if(front = = -1) {
front++; t=dequeue();
} if(k = = 1)
} cout<<"1st deleted item is: "<<t<<endl;
void display() else if(k = = 2)
{int r, f; cout<<"2nd deleted item is: "<<t<<endl;
r =rear; else if(k = = 3)
f=front; cout<<"3rd deleted item is: "<<t<<endl;
while(f != r+1) else
{cout<<queue[f]<<" "; cout<<k<<"th deleted item is: "<<t<<endl;
f=f+1; }
} }
cout<<endl;
}
int dequeue()
{
if(isempty())
{
cout<<"Underflow, Queue is empty.\n";
return NULL;
}
else
return queue[front++];
}

int main()
Page - 2

You might also like