0% found this document useful (0 votes)
7 views

CPDSweek5,6Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CPDSweek5,6Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Ex. No.

5
Array Implementation of Stack
Date: 01-10-2024

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.
OUTPUT:
1.PUSH
2.POP
3.DISPLAY
4.LENGTH OF STACK
5.EXIT1
Enter the Number to be inserted : 23

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:

• 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
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

Write a C program for array implementation of queue using an array and


provides operations to enqueue, dequeue, display elements.
OUTPUT
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
1
Element to be enqueued: 23

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:

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.
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
#define size 4
int front=-1;
int rear=-1;
int queue[size];
int enq();
int deq();
int display();
int main() {
int c;
while(1){
printf("\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
scanf("%d",&c);
switch(c){
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("enter a VALID choice");
break;
}
}
}
void enq(){
int n;
if(rear==size-1)
printf("OverFlow");
else{
if(front==-1)
front=0;
printf("Element to be enqueued: ");
scanf("%d",&n);
rear++;
queue[rear]=n;}
}
void deq(){
if (front==-1||front>rear)
printf("UNDERFLOW");
else
{
printf("Element deleted: %d",queue[front]);
front=front+1;
}
}
void display(){
int i;
if(front==-1)
printf("Empty");
else{
printf("Queue Elements :");
for (i=front;i<=rear;i++)
printf("\n%d",queue[i]);
printf("\n");
}
}

RESULT
The C program to implement array implementation of queue using an array is verified
and executed successfully

You might also like