9842 DSA Exp4
9842 DSA Exp4
CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
Computer Science
Class: S.E. ECS(Semester III) Subject Name: Data Structures and Algorithms
(ECC 303)
Problem Statement:
Write C Program to implement stack using linked list.
Theory:
The disadvantage of a stack implemented using an array is that its size is fixed and
needs to be specified at compile time. This stack implementation is often impractical. To
make the size of the stack to be dynamic and determined at run-time, we need to
implement it using a linked list. By doing this, the size of the stack can shrink or grow on
demand during the program execution. A stack implemented using a linked list is also
known as a linked stack.
• Number of elements in stack is not restricted to certain size.
• Dynamic memory creation, memory will be assigned to stack when a new node
is pushed into stack, and memory will be released when an element being
popped from the stack.
• Stack using linked list implementation can be empty or contains a series of
nodes.
FR. CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
• Each node in a stack must
Computer contain at least 2 attributes: data – to store
Science
information in the stack. pointer next (store address of the next node in the stack
)
• PUSH :
1. Initially top is Null 2. Then pushing first node pointed by top
Pushing second node. Make top point to latest inserted node. And top->next
pointes to previous top.
• POP:
Pop the node data pointed by top and set top to top->next.
SOURCE CODE:
//Name:Wasee Momin
//Rno:9842
//Topic:Dynamic implementation of stack
#include<stdio.h>
FR. CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
#include<stdlib.h>
Computer Science
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
//Main function
void main()
{
int a=1,option;
do
{
printf("\nChoose any operation: \n1:PUSH \n2:POP \n3:PEEK \n4:DISPLAY \n");
scanf("%d",&option);
switch(option)
{
case 1:
start=push(start);
break;
case 2:
start=pop(start);
break;
case 3:
peek(start);
break;
case 4:
display(start);
break;
default:
printf("Incorrect option");
break;
}
FR. CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
printf("\nEnter 0 to exit:");
Computer Science
scanf("%d",&a);
}
while(a!=0);
}
if(start=NULL)
{
start=newnode;
newnode->next=NULL;
}
else
{
newnode->next=start;
start=newnode;
}
return start;
}
OUTPUT:
FR. CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
Computer Science
FR. CONCEICAO RODRIGUES COLLEGE OF
ENGINEERING
Department of Electronics and Computer Science
The program was tested for different sets of inputs.
Program is working is SATISFACTORY NOT SATISFACTORY (Tick appropriate
outcome)
Evaluation: -
On Time Completion Knowledge of Implementation and Total (10)
and Submission (2) the topic (4) Output (4)