CPDSweek5,6Final
CPDSweek5,6Final
5
Array Implementation of Stack
Date: 01-10-2024
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT1
Enter the Number to be inserted : 34
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT1
Enter the Number to be inserted : 56
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT3
Elements in the Stack :
56
34
23
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT4
Length of the Stack is 3
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT2
The Popped number is 56
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:
Step 3: Declare function prototypes for push, pop, display, and a function for
checking the length of the stack.
Step 5: Implement the pop function to remove an element from the stack:
Step 6: Implement the display function to show all elements in the stack:
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
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT2
The Popped number is 34
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT5
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#define size 4
int top=-1;
int push();
int pop();
int display();
char stack[100];
int main(){
int choice;
while(1){
printf("\n 1.PUSH \n 2.POP \n 3.DISPLAY \n 4.LENGTH OF STACK \n 5.EXIT");
scanf("%d",&choice);
switch(choice){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Length of the Stack is %d",top+1);
break;
case 5:
exit(0);
break;
default:
printf("Enter a VALID choice");
break;
}
}
}
int push(){
int n;
if (top==size-1)
printf("Stack OverFlow");
else{
printf("Enter the Number to be inserted : ");
scanf("%d",&n);
top++;
stack[top]=n;
}
int pop(){
if (top==-1)
printf("Stack UnderFlow");
else{
int n;
n=stack[top];
printf("The Popped number is %d",n);
top--;
}
}
int display(){
int i;
if(top==-1)
printf("Stack is Empty");
else{
printf("Elements in the Stack :\n");
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
RESULT:
The C program to implement array implementation of stack using an array is verified
and executed successfully
Ex. No. 6
Array Implementation of Queue
Date: 01-10-2024
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
1
Element to be enqueued: 34
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
1
Element to be enqueued: 45
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
2
Element deleted: 23
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
2
Element deleted: 34
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
3
Queue Elements :
45
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
4
AIM:
To write a program for array implementation of queue in C.
ALGORITHM:
RESULT
The C program to implement array implementation of queue using an array is verified
and executed successfully