Ex. No.
5
Date:3|10|24 Array Implementation of Stack
AIM:
To write a C program that implements a stack using an array and provides operations to push,
pop, display elements, and check the length of the stack.
ALGORITHM:
Step 1: Start
Step 2: Define constants and global variables:
• Define the maximum size of the stack using #define size 4.
• Initialize the top of the stack as -1 to indicate an empty stack.
• Declare an array stack to hold the stack elements.
Step 3: Declare function prototypes for push, pop, display, and a function for checking the
length of the stack.
Step 4: Implement the push function to add an element to the stack:
• Check if the stack is full (i.e., top == size - 1).
• If not full, read an element from the user and push it onto the stack.
Step 5: Implement the pop function to remove an element from the stack:
• Check if the stack is empty (i.e., top == -1).
• If not empty, pop the element from the stack and display it.
Step 6: Implement the display function to show all elements in the stack:
• Check if the stack is empty.
• If not empty, iterate through the stack from the top to bottom and print each element.
Step 7: Implement the main function to provide a menu-driven interface for the stack
operations:
• Use a while loop to continuously display the menu and process user choices.
• Call the appropriate functions based on user input.
Step 8: Stop
RESULT:
1.Push
2.pop
3.display
4.length
5.quit
Enter choice6
Invalid choice
1.Push
2.pop
3.display
4.length
5.quit
Enter choice2
Stack underflow
1.Push
2.pop
3.display
4.length
5.quit
Enter choice1
Enter the element to push:8
1.Push
2.pop
3.display
4.length
5.quit
Enter choice5
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
#define size 4
int top=-1;
int stack [size];
void push();
void pop();
void display();
void main()
{
int choice;
while (1)
{
printf ("\n1.Push\n2.pop\n3.display\n4.length\n5.quit\nEnter choice");
scanf("%d",&choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("length of stack:%d",top);
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice");
}
}
}
void push()
{
int n;
if(top==size-1)
printf("Stack overflow");
else
{
printf("Enter the element to push:");
scanf("%d",&n);
top++;
stack[top]=n;
}
}
void pop()
{
int a;
if(top==-1)
printf ("Stack underflow");
else
{
int n;
n=stack[top];
printf("%d popped from stack",n);
top --;
}
}
void display()
{
int i;
if (top==-1)
printf("Stack empty");
else
{
printf("Elements of stack are:");
for(i=top;i>=0;i--)
{
printf("%d",stack[i]);
}
}
}
INFERENCE:
From this program , the importance and use of stack was known. The stack offers some basic
operations like push, pop. It follows the Last-In-First-Out (LIFO) order of operations
RESULT:
The C program to implement stack using array is compiled and executed using online
compiler
Ex. No. 6
Date:3|10|24 Array Implementation of Queue
AIM:
To write a program for array implementation of queue in C++.
ALGORITHM:
Step1:Define a array which stores queue elements..
Step 2: The operations on the queue are
a)INSERT data into the queue
b)DELETE data out of queue
Step 3: INSERT DATA INTO queue
a.Enter the data to be inserted into queue.
b.If TOP is NULL
the input data is the first node in queue.
the link of the node is NULL.
TOP points to that node.
c.If TOP is NOT NULL
the link of TOP points to the new node.
TOP points to that node.
Step 4: DELETE DATA FROM queue
a.If TOP is NULL
the queue is empty
b.If TOP is NOT NULL
the link of TOP is the current TOP.
the pervious TOP is popped from queue.
Step 5. The queue represented by linked list is traversed to display its content.
RESULT:
1.Enqueue
2.Dequeue
3.display
4.length
5.quit
Enter choice2
Queue underflow
1.Enqueue
2.Dequeue
3.display
4.length
5.quit
Enter choice1
5
1.Enqueue
2.Dequeue
3.display
4.length
5.quit
Enter choice3
Elements of queue are:5
1.Enqueue
2.Dequeue
3.display
4.length
5.quit
Enter choice4
length of stack:-1
1.Enqueue
2.Dequeue
3.display
4.length
5.quit
Enter choice5
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
#define size 4
int top=-1;
int front=-1;
int rear=-1;
int queue[size];
void enqueue();
void dequeue();
void display();
void main()
{
int choice;
while (1)
{
printf ("\n1.Enqueue\n2.Dequeue\n3.display\n4.length\n5.quit\nEnter choice");
scanf("%d",&choice);
switch (choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("length of stack:%d",top++);
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice");
}
}
}
void enqueue()
{
int n;
if(rear==size-1)
printf("Queue overflow");
else
{
if(front==-1)
front=0;
else
printf("Enter the element to enqueue:");
scanf("%d",&n);
rear++;
queue[rear]=n;
}
}
void dequeue()
{
if(front==-1||front>rear)
printf ("Queue underflow");
else
{
printf("%d deleted from queue",queue[front]);
front=front+1;
}
}
void display()
{
int i;
if (front==-1)
printf("Queue empty");
else
{
printf("Elements of queue are:");
for(i=front;i<=rear;i++)
printf("%d",queue[i]);
printf("\n");
}
}
INFERENCE:
From this program , the importance and use of queue was known. The queue offers some
basic operations like enqueue,dequeue. It follows the First-In-First-Out (FIFO) order of
operations
RESULT:
The C program to implement queue using array is compiled and executed using online
compiler.