[go: up one dir, main page]

0% found this document useful (0 votes)
52 views4 pages

Lab 5

The document describes implementing a stack using a linked list in C. It includes functions to push, pop, and display elements in the stack. The main function runs a loop that prompts the user to push, pop, display, or exit. Pushing adds a new node to the top. Popping removes and displays the top node. Display prints all nodes from top to bottom. The stack grows and shrinks dynamically as nodes are added and removed.

Uploaded by

Ram
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)
52 views4 pages

Lab 5

The document describes implementing a stack using a linked list in C. It includes functions to push, pop, and display elements in the stack. The main function runs a loop that prompts the user to push, pop, display, or exit. Pushing adds a new node to the top. Popping removes and displays the top node. Display prints all nodes from top to bottom. The stack grows and shrinks dynamically as nodes are added and removed.

Uploaded by

Ram
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/ 4

LAB-5

IMPLEMENTATION OF STACK USING LINKED LIST

/*
* C Program to Implement a Stack using Linked List
*/
#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*top,*temp,*top1;

void push(int data);


void pop();
void display();
void create();

int main()
{
int no, ch, e;

printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Display");
printf("\n 4 - Exit");
create();

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
exit(0);

default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
getch();
return 0;
}
/* Create empty stack */
void create()
{
top = NULL;
}

/* Push data into stack */


void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
}

/* Display stack elements */


void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)


{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

/* Pop Operation on stack */


void pop()
{
top1 = top;

if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
}

RESULT
-------
OUTPUT

1 - Push
2 - Pop
3 - Display
4 - Exit
Enter choice : 1
Enter data : 5

Enter choice : 1
Enter data : 2

Enter choice : 1
Enter data : 3

Enter choice : 1
Enter data : 4

Enter choice : 3
4 3 2 5
Enter choice : 2

Popped value : 4
Enter choice : 2

Popped value : 3
Enter choice : 2

Popped value : 2
Enter choice : 2

Popped value : 5
Enter choice : 2

Error : Trying to pop from empty stack


Enter choice :

DISCUSSION AND CONCLUSION:


In this lab session,we discussed about the implementation of stack using linked
list.
Unlike,static implementation of stack,linked stack size is not fixed.
It can be grow or shrink during the execution.
During the push opertion we don't need to check the stack full condition because
stack size is not fixed and can be made as desired.
Top of stack is used to represent the first node of the linked stack.
A stack contains a top pointer which is �head� of the stack where pushing and
popping items happens at the head of the list.
First node have null in link field and second node link have first node address in
link field and so on and last node address in �top� pointer.
For pop operation,we checked the stack empty condition. If TOS is NULL, the stack
is empty, otherwise not.
If TOS is not empty, the data is displayed and TOS is shifted to next node of
linked stack and the node which was previously pointed by TOS is discarded.
Thus,stack was implemented using Linked list.

You might also like