Más contenido relacionado
La actualidad más candente (20)
Similar a Stack using Linked List (20)
Stack using Linked List
- 2. The Main Module#include<stdio.h>
#include<stdlib.h>
void push(), pop(), display();
struct stack
{
int data;
struct stack *link;
};
typedef struct stack STACK;
STACK *top=NULL;
int main()
{
int choice;
while(1)
{
printf("n----Stack Menu----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: exit(0);
break;
default: printf("Invalid option.");
}
}
}
- 3. Insert An Element Into Stack
void push()
{
STACK *temp;
int num;
temp=(STACK *)malloc(sizeof(STACK));
printf("Enter an integer value: ");
scanf("%d",&num);
temp->data=num;
temp->link=top;
top=temp;
display();
}
- 4. Delete An Element From Stack
void pop()
{
if(top==NULL)
{
printf("Stack Underflow");
}
else
{
STACK *delete_rack;
delete_rack=top;
printf("Item popped is: %dn",delete_rack->data);
top=top->link;
free(delete_rack);
display();
}
}
- 5. Display The Elements In Stack
void display()
{
if(top==NULL)
{
printf("Stack Underflow");
}
else
{
printf("The items in the stack are:");
STACK *head;
head=top;
while(head!=NULL)
{
printf("n%d",head->data);
head=head->link;
}
}
}