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

stackll

Uploaded by

Anushka Shivade
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)
6 views

stackll

Uploaded by

Anushka Shivade
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/ 3

Assignment 7

Name Anushka Shivade


U no UEC2023156
Problem Program to model a singly linked list as a stack (Dynamic implementation of
Stack) and perform
the following operations on it:
a) Push
b) Pop
c) Display.

Program:
#include<stdio.h>
#include<stdlib.h>
struct stack{
int num;
struct stack *next;

}*p,*q,*top,*temp;

void push();
int pop();
void display();

int main(){
int ch ,x;

do{
printf("**************MENU**************\n");
printf("1.Push\n2.Pop\n3.Display\n");

printf("Enter the choice: ");


scanf("%d",&ch);

switch(ch){

case 1:push();
break;
case 2 :
x =pop();
if(x==-1) printf("Stack is empty , no element can be poped\n");
else printf("The element poped is %d",x);
break;
case 3:display();
break;
default: printf("Enter valid choice\n");
}

}while(ch!=4);

return 0;
}
void display(){
if(top==NULL) printf("Stack is empty \n");
else{
q=top;
while (q!=NULL){
printf("%d\t", q->num);
q=q->next;
}
printf("\n");
}

}
void push(){
char c;
do{
p=(struct stack*) malloc(sizeof(struct stack) );

printf("Enter the data : ");


scanf("%d" ,&p->num);

p->next=NULL;

if(top==NULL) top=p;
else{
p->next=top;
top=p;
}
printf("Do you want more data?(y/n): ");
scanf(" %c",&c);
}while(c=='y'||c=='Y');

}
int pop(){

int pop;
if(top==NULL) return -1;
else{
p=top;
pop=top->num;
top=top->next;
p->next =NULL;
}
free(p);
return pop;
}
Output:
PS D:\Anushka\college> cd "d:\Anushka\college\DSA\" ; if ($?) { gcc stackll.c -o stackll } ; if ($?) {
.\stackll }
**************MENU**************
1.Push
2.Pop
3.Display
Enter the choice: 1
Enter the data : 1
Do you want more data?(y/n): y
Enter the data : 6
Do you want more data?(y/n): y
Enter the data : 99
Do you want more data?(y/n): y
Enter the data : 44
Do you want more data?(y/n): y
Enter the data : 5
Do you want more data?(y/n): n
**************MENU**************
1.Push
2.Pop
3.Display
Enter the choice: 2
The element poped is 5**************MENU**************
1.Push
2.Pop
3.Display
Enter the choice: 3
44 99 6 1
**************MENU**************
1.Push
2.Pop
3.Display
Enter the choice: 4
Enter valid choice

You might also like