0% found this document useful (0 votes)
3 views3 pages

Stack

This document contains a C program that implements a stack data structure using a linked list. It provides functionalities to push, pop, and display elements in the stack through a simple menu-driven interface. The program includes necessary functions for stack operations and handles memory allocation and error checking.

Uploaded by

anshuagro
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)
3 views3 pages

Stack

This document contains a C program that implements a stack data structure using a linked list. It provides functionalities to push, pop, and display elements in the stack through a simple menu-driven interface. The program includes necessary functions for stack operations and handles memory allocation and error checking.

Uploaded by

anshuagro
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

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *stack;
int isempty(stack);
stack push(stack, int);
stack pop(stack);
void display(stack);
void main()
{
stack s=NULL;
int x;
char ch='1';
clrscr();
while(ch!='4')
{
printf("\n1 - Push");
printf("\n2 - Pop");
printf("\n3 - Display");
printf("\n4 - Quit");
printf("\n\n Enter your choice");
fflush(stdin);
ch=getchar();
switch(ch)
{
case '1':
printf("Enter the element to be pushed");
scanf("%d",&x);
s=push(s,x);
break;
case '2':
s=pop(s);
break;
case '3':
display(s);
break;
case '4':
break;
default:
printf("\n Wrong choice!try again");
}
}
}
int isempty(stack s)
{
if(s==NULL)
return 1;
else
return 0;
}
stack push(stack s,int x)
{
stack temp;
temp=(stack)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n out of memory space");
exit(0);
}
temp->info=x;
temp->next=s;
s=temp;
return s;
}
stack pop(stack s)
{
stack temp;
int x;
if(isempty(s))
{
printf("\n stack empty");
exit(0);
}
temp=s;
x=s->info;
printf("\n popped element is %d",x);
s=s->next;;
free(temp);
return s;
}
void display(stack s)
{
stack ptr;
ptr=s;
printf("\n the elements in the stack are");
if(ptr==NULL)
{
printf("\n stack is empty");
return;
}
while(ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->next;
}
}

You might also like