0% found this document useful (0 votes)
14 views10 pages

Lab Assignments - 03 DS - 240919 - 215212

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)
14 views10 pages

Lab Assignments - 03 DS - 240919 - 215212

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/ 10

Name:-Chanchlesh Ravikiran

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");

printf("Enter your choice: ");


scanf("%d", &choice);
switch (choice) {
case 1:
push();
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 elements in the stack:-

2. Display elements of stack:-

3. Pop out elements from stack:-


Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
Que2) Write a program to implement stack (LIFO) using link list.

● 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 push(int value, int x){


if(x==MAX_SIZE){
printf("Stack overflow");
}
else{
Node* newnode = (Node*)malloc(sizeof(Node));
newnode->data = value;
if(top==NULL){
newnode->next = NULL;
top = newnode;
}
else{
newnode->next = top;
top = newnode;
}
}
}

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");

printf("Enter your choice: ");


scanf("%d", &choice);

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

2. Stack overflow when max size reached:-

3. Display stack:-
Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
4. Pop out elements from the stack:-

5. Stack underflow when it has no elements:-


Name:-Chanchlesh Ravikiran
Enrollment no:-0801IT231042
B.Tech IInd year, Batch A2
Que3) Write a program to convert infix notation to postfix notation using
stack.

● Program:-

You might also like