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

stack using linked list

This document contains a C program that implements a stack using a linked list. It provides functions to push, pop, peek, and display elements in the stack. The main function allows users to interact with the stack through a menu-driven interface.

Uploaded by

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

stack using linked list

This document contains a C program that implements a stack using a linked list. It provides functions to push, pop, peek, and display elements in the stack. The main function allows users to interact with the stack through a menu-driven interface.

Uploaded by

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

#include<stdio.

h>
#include<stdlib.h>
struct node{
struct node *next;
int n;
};
struct node *first=NULL;
struct node *last;
struct node *current;
int push();
int pop();
int peek();
int display();
int main();
int push()
{
int n;
current=(struct node*)malloc(sizeof(struct node));
printf("Please enter n value");
scanf("%d",&current->n);
if(first==NULL)
{
first=last=current;
last->next=NULL;
}
else
{
current->next=last;
last=current;
first=last;
}
}
int pop()
{
if(first==NULL)
printf("Stack is empty");
else
first=first->next;
}
int peek()
{
if(first==NULL)
printf("Stack is empty");
else
printf("The top element is %d",first->n);
}
int display()
{
if(first==NULL)
printf("Stack is empty");
else
{
current=first;
printf("The elements in the stack are \n");
while(current!=NULL)
{
printf("%d\t",current->n);
current=current->next;
}
}
}
int main()
{
int option,n;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit");
printf("\n\nPlease enter you choice of operation : ");
scanf("%d",&option);
switch(option)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
break;
default:
printf("Invalid choice entered");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy