Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

7.

Array implementation of Stack, Queue and Circular Queue ADTs

Ex. No. 1a Array Implementation of Stack


Aim
To implement stack operations using array.

Algorithm

1. Start
2. Define a array stack of size max = 5
3. Initialize top = -1
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
If top < max -1
Increment top
Store element at current position of top
Else
Print Stack overflow
Else If choice = 2 then
If top < 0 then
Print Stack underflow
Else
Display current top element
Decrement top
Else If choice = 3 then
Display stack elements starting from top
7. Stop

Program
/* Stack Operation using Arrays */

#include <stdio.h>
#include <conio.h>
#define max 5
static int stack[max];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return (stack[top--]);
}
void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \n");
else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
{
printf("%4d", stack[i]);
}
printf("\n");
}
}
main()
{
int ch=0, val;
clrscr();
while(ch != 4)
{
printf("\n STACK OPERATION \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("4.QUIT \n");
printf("Enter Choice : ");

scanf("%d", &ch);
switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n");
break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n");
else
{
val = pop();
printf("\n Popped element is %d\n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
}

Output
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 34
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 45 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element is 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 4

Result
Thus push and pop operations of a stack was demonstrated using arrays.

Ex.No 1b. Array Implementation Queue


Aim
To implement queue operations using array.

Algorithm

1. Start
2. Define a array queue of size max = 5
3. Initialize front = rear = –1
4. Display a menu listing queue operations
5. Accept choice
6. If choice = 1 then
If rear < max -1
Increment rear
Store element at current position of rear
Else
Print Queue Full
Else If choice = 2 then
If front = –1 then
Print Queue empty
Else
Display current front element
Increment front
Else If choice = 3 then
Display queue elements starting from front to rear.

Program

/* Queue Operation using Arrays */


#include <stdio.h>
#include <conio.h>
#define max 5
static int queue[max];
int front = -1;
int rear = -1;
void insert(int x)
{
queue[++rear] = x;
if (front == -1)
front = 0;
}
int remove()
{
int val;
val = queue[front];
if (front==rear && rear==max-1)
front = rear = -1;
else
front ++;
return (val);
}
void view()
{
int i;
if (front == -1)
printf("\n Queue Empty \n");
else
{
printf("\n Front-->");
for(i=front; i<=rear; i++)
printf("%4d", queue[i]);
printf(" <--Rear\n");
}
}
main()
{
int ch= 0,val;
clrscr();

while(ch != 4)
{
printf("\n QUEUE OPERATION \n");
printf("1.INSERT ");
printf("2.DELETE ");
printf("3.VIEW ");
printf("4.QUIT\n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(rear < max-1)
{
printf("\n Enter element to be inserted : ");
scanf("%d", &val);
insert(val);
}
else
printf("\n Queue Full \n");
break;
case 2:
if(front == -1)
printf("\n Queue Empty \n");
else
{
val = remove();
printf("\n Element deleted : %d \n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
}

Output
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 12
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 23
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 34
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 45
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 56
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Queue Full
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 3
Front--> 12 23 34 45 56 <--Rear
Result

Thus insert and delete operations of a queue was demonstrated using arrays.

Ex.No.1c Array Implementation of Circular Queue ADTs

Aim
To implement Circular Queue ADTs using array implementation.

Algorithm:

1. Enqueue(x) Operation

You should follow the following steps to insert (enqueue) a data element into a circular queue -

 Step 1: Check if the queue is full (Rear + 1 % Maxsize = Front)

 Step 2: If the queue is full, there will be an Overflow error

 Step 3: Check if the queue is empty, and set both Front and Rear to 0

 Step 4: If Rear = Maxsize - 1 & Front != 0 (rear pointer is at the end of the queue and front
is not at 0th index), then set Rear = 0

 Step 5: Otherwise, set Rear = (Rear + 1) % Maxsize

 Step 6: Insert the element into the queue (Queue[Rear] = x)

 Step 7: Exit

2. Dequeue() Operation

Obtaining data from the queue comprises two subtasks: access the data where the front is
pointing and remove the data after access. You should take the following steps to remove data
from a circular queue - 

 Step 1: Check if the queue is empty (Front = -1 & Rear = -1)

 Step 2: If the queue is empty, Underflow error

 Step 3: Set Element = Queue[Front]


 Step 4: If there is only one element in a queue, set both Front and Rear to -1 (IF Front =
Rear, set Front = Rear = -1)

 Step 5: And if Front = Maxsize -1 set Front = 0

 Step 6: Otherwise, set Front = Front + 1

 Step 7: Exit

#include <stdio.h>

#include <Windows.h>

#define MAX_SIZE 5

int a[MAX_SIZE];

int front = -1;

int rear = -1;

void enqueue(int x)

if (front == -1 && rear == -1)

front = rear = 0;

else if ((rear + 1) % MAX_SIZE == front)

printf("queue is full\n");

return;
}

else

rear = (rear + 1) % MAX_SIZE;

a[rear] = x;

void dequeue()

if (front == -1 && rear == -1)

printf("queue is empty \n");

return;

else if (front == rear)

front = rear = -1;

else

front = (front + 1) % MAX_SIZE;

int Peek()
{

if (a[front] == -1)

return -1;

return a[front];

void Print()

int count = ((rear + MAX_SIZE - front) % MAX_SIZE)+1;

int i;

for (i = 0; i < count; i++)

printf("%d ", a[(front+i)%MAX_SIZE]);

printf("\n");

int main(void)

enqueue(5);

printf("\nFirst insertion in circular Queue\n");

Print();
printf("\n Second insertion in circular Queue\n");

enqueue(7);

Print();

printf("\n Third insertion in circular Queue\n");

enqueue(-3);

Print();

printf("\n Fourth insertion in circular Queue\n");

enqueue(9);

Print();

printf("\n Circular Queue after first deletion\n");

dequeue();

Print();

printf("\n Circular Queue after 2nd deletion\n");

dequeue();

Print();

printf("\n Insertion in circular Queue\n");

enqueue(14);

system("pause");

return 0;

}
Output:

You might also like