Ex 6 - DSCP Lab
Ex 6 - DSCP Lab
AIM:
To write a C program for array implementation of stack ADTs.
ALGORITHM:
1. Start the program.
2. Initialize stack.
3. Display menu with array implementation operations.
4. If choice = 1, push an element in the stack.
5. If choice = 2, pop an element in the stack.
6. If choice = 3, display the elements in the stack.
7. If choice = 5, exit.
8. Stop the program.
PROGRAM:
/* Array Implementation of Stack ADT */
#include <stdio.h>
#define maxsize 3
int stack [maxsize];
int sp = -1;
void push (int);
int pop ();
void print ();
int main ()
{
int choice = 0, p, x;
while (choice < 4)
{
clrscr ();
printf (“Array Implementation of Stack ADT”);
printf (“\n1. Push \n2. Pop \n3. Display \n4. Exit\n”);
printf (“\nEnter your choice: ”);
scanf (“%d”, &choice);
switch (choice)
{
case 1: printf (“Enter the element: ”);
scanf (“%d”, &x);
push (x);
break;
case 2: x = pop ();
if (x != NULL)
printf (“The popped element is %d.”, x);
break;
case 3: print ();
1
break;
}
getch ();
}
}
void push (int x)
{
if (sp < maxsize - 1)
stack [++sp] = x;
else
{
printf (“Stack Full”);
getch ();
}
}
int pop ()
{
int x;
if (sp != -1)
{
x = stack [sp--];
return x;
}
else
{
printf (“Stack Empty”);
return NULL;
}
}
void print ()
{
int i;
for (i = 0; i <= sp; i++)
printf (“%d\n”, stack [i]);
}
OUTPUT:
Array Implementation of Stack ADT
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the element: 10
Enter your choice: 1
Enter the element: 20
2
Enter your choice: 1
Enter the element: 30
Enter your choice: 3
10
20
30
Enter your choice: 2
The popped element is 30.
Enter your choice: 3
10
20
Enter your choice: 4
RESULT:
Thus, the array implementation of stack ADT was implemented successfully.
3
EX. NO : 6B ARRAY IMPLEMENTATION OF QUEUE ADT
DATE :
AIM:
To write a C program for array implementation of queue ADTs.
ALGORITHM:
1. Start the program.
2. Initialize queue.
3. Display menu with array implementation operations.
4. If choice = 1, enqueue an element in the queue.
5. If choice = 2, dequeue an element in the queue.
6. If choice = 3, display the elements in the queue.
7. If choice = 5, exit.
8. Stop the program.
PROGRAM:
/* Array Implementation of Queue ADT */
#include <stdio.h>
#define maxsize 3
int queue [maxsize];
int front = -1;
int rear = -1;
void enqueue (int);
int dequeue ();
void printqueue ();
int main ()
{
int choice = 0, p, x;
while (choice < 4)
{
clrscr ();
printf (“\n1. Enqueue \n2. Dequeue \n3. Print \n4. Exit\n”);
printf (“\nEnter your choice: ”);
scanf (“%d”, &choice);
switch (choice)
{
case 1: printf (“Enter the value: ”);
scanf (“%d”, &x);
enqueue (x);
break;
case 2: x = dequeue ();
if (x != NULL)
printf (“The dequeued element is %d.”, x);
break;
case 3: printqueue ();
4
break;
}
getch ();
}
}
int dequeue ()
{
int x;
if (front>-1 && front<=rear)
{
x = queue [front];
front = front + 1;
return x;
}
else
{
printf (“Queue Empty”);
return NULL;
}
}
void printqueue ()
{
int i;
for (i = front; i <= rear; i++)
printf (“%d\n”, queue [i]);
}
OUTPUT:
Array Implementation of Queue ADT
1. Enqueue
2. Dequeue
3. Display
4. Exit
5
Enter your choice: 1
Enter the element: 10
Enter your choice: 1
Enter the element: 20
Enter your choice: 1
Enter the element: 30
Enter your choice: 3
10
20
30
Enter your choice: 2
The popped element is 10.
Enter your choice: 3
20
30
Enter your choice: 4
RESULT:
Thus, the array implementation of queue ADT was implemented
successfully.