Lab Assignments - 03 DS - 240919 - 215212
Lab Assignments - 03 DS - 240919 - 215212
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
Lab Assignments_03
Que1) Write a program for stack that performs following operations using array.
(a) PUSH (b)POP (C)DISPLAY.
● Program:-
#include<stdio.h>
#define MAX_SIZE 10
int stack[MAX_SIZE];
int top = -1;
void push();
void pop();
void display();
void push(){
if(top==MAX_SIZE-1){
printf("Stack overflow\n");
}
else{
int n;
printf("Please Enter an element: ");
scanf("%d", &n);
top++;
stack[top] = n;
printf("%d is pushed in the stack", n);
}
}
void pop(){
if(top==-1){
printf("Stack underflow\n");
}
else{
printf("%d is popped out", stack[top]);
--top;
}
}
void display(){
Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
if(top==-1){
printf("Stack Underflow\n");
}
else{
printf("Elements of stack are: \n");
for(int i=top; i>=0; i--){
printf("%d ",stack[i]);
}
}
}
int main(){
printf("Name:-Chanchlesh Ravikiran\nEnrollment No:-0801IT231042\n");
int choice, value;
while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
● Program:-
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
#define MAX_SIZE 10
typedef struct Node Node;
Node* top;
//functions
void push(int value, int x);
void pop();
void display();
int Nodecount();
void pop(){
if(top==NULL){
Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
printf("Stack underflow");
}
else{
Node* t;
t = top;
printf("%d is pop out of the stack",t->data);
top = top->next;
free(t);
}
}
void display(){
if(top==NULL){
printf("stack underflow");
}
else{
Node* temp = (Node*)malloc(sizeof(Node));
temp = top;
while(temp!=NULL){
printf("%d ",temp->data);
temp = temp->next;
}
}
}
int Nodecount(){
int x = 0;
Node* temp;
temp = top;
while(temp!=NULL){
x++;
temp = temp->next;
}
return x;
}
int main(){
printf("Name:-Chanchlesh Ravikiran\nEnrollment No:-0801IT231042\n");
int choice, value;
while (1) {
printf("\nStack Operations:\n");
Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
switch (choice) {
case 1:
int n;
printf("Enter element to push in: ");
scanf("%d", &n);
int m = Nodecount();
push(n, m);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice. Please choose again.\n");
}
}
return 0;
}
Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
● Output:-
1. Pushing operation in stack:-
3. Display stack:-
Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
4. Pop out elements from the stack:-
● Program:-