Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Stack ADT using linked list Program

#include<stdio.h> #include<stdlib.h> #define null 0 struct stacknode { int data; struct stacknode *next; }*start=null; typedef struct stacknode snode; void push(); int pop(); void display(); int main() { char ch; int choice,item; do { printf("\n1.push"); printf("\n2.pop"); printf("\n3.display"); printf("\n4.exit"); printf("\nenter your choice"); scanf("%d",&choice); switch(choice) { case 1: push (); break; case 2: pop(); printf("\nthe element is deleted"); break; case 3: display(); break; } }while(choice!=4); } void push() { snode *node; node=(snode*)malloc (sizeof (snode)); printf("\nenterthe number to be inserted:"); scanf("%d",&node->data); node->next=start; start=node; } int pop() {

snode*temp; temp=start; if(start==null) { printf("stack is alraedy empty"); exit(0); } else { temp=start; start=start->next; free(temp); } return(temp->data); } void display() { snode*temp; temp=start; while(temp->next!=null) { printf("\n%d",temp->data); temp=temp->next; } printf("\n%d",temp->data); }

You might also like