top of page

Stack Using Linked List



#include <stdio.h>

#include<stdlib.h>

#include<math.h>


#define size 5


int top=-1;


void push(void);

void pop(void);

void peek_top(void);

void isEmpty(void);

void isFull(void);

void display(void);


struct stack{

int data;

struct node *next;

};


struct stack *head=0;


void main() {

int c,e;

do{

switch(c)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek_top();

break;

case 4:

isEmpty();

break;

case 5:

isFull();

break;

case 6:

display();

break;

case 7:

printf("\n----Exit from program successfully----");

printf("\n--------------Thank you--------------");

exit(0);

}

printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display\n7 : Exit");

printf("\nEnter your choice : ");

scanf("%d",&c);

}while(c);

}


void push()

{

struct stack *newnode,*temp;

if(top<size)

{

newnode=(struct stack *)malloc(sizeof(struct stack));

printf("\nEnter data : ");

scanf("%d",&newnode->data);

newnode->next=0;

top++;

if(head==0)

{

head=temp=newnode;

}

else

{

temp=head;

while(temp->next!=0)

{

temp=temp->next;

}

temp->next=newnode;

}

}

else

{

printf("\nStack is full");

}

}


void display()

{

struct stack *temp;

temp=head;

while(temp!=0)

{

printf("--%d",temp->data);

temp=temp->next;

}

}


void pop()

{

struct stack *temp,*prevnode;

printf("\n\n----Deletion From End----");

temp=head;

top--;

while(temp->next!=0)

{

prevnode=temp;

temp=temp->next;

}

if(temp==head)

{

head=0;

}

else

{

prevnode->next=0;

}

free(temp);

printf("\nTop : %d",top);

}


void peek_top()

{

struct stack *temp;

temp=head;

while(temp->next!=0)

{

temp=temp->next;

}

printf("\nTop : %d",top);

printf("\nPeek : %d",temp->data);

}


void isEmpty()

{

if(top==-1)

{

printf("\nStack is empty");

}

else

{

printf("\nStack is not empty");

}

}


void isFull()

{

if(top==size-1)

{

printf("\nStack is full");

}

else

{

printf("\nStack is not full");

}

}

2 views0 comments

Recent Posts

See All

Stack Using Queue

A stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue operations. When an element is pushed onto the stack, it is enqueued

bottom of page