0% found this document useful (0 votes)
18 views8 pages

9842 DSA Exp4

Uploaded by

Alan Michael Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

9842 DSA Exp4

Uploaded by

Alan Michael Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FR.

CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
Computer Science

Class: S.E. ECS(Semester III) Subject Name: Data Structures and Algorithms
(ECC 303)

Name of the Student: Wasee Momin

Experiment No 3 Roll Number 9842


Date of Date of Submission 14-10-23
Performance
Experiment Title Dynamic Implementation of stack
CO Mapping ECC303.2

Problem Statement:
Write C Program to implement stack using linked list.

Objective of the Experiment:


Understand basic operations like push and pop in 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.

For third node.


FR. CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
Computer Science

• POP:
Pop the node data pointed by top and set top to top->next.

• createStack() - initialize top


• push(ele) - insert item onto stack
• pop() – delete item from stack
• isEmpty() – check whether a stack is empty.
• peek() - get item at top
isFull() operation is not needed since elements can be inserted into stack without
limitation to the stack size.
Source Code:

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;

//Required functions for stack operations


int isEmpty(struct node*);
struct node *push(struct node*);
struct node *pop(struct node*);
void peek(struct node*);
void display(struct node*);

//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);
}

//now write all the functions individually


struct node *push(struct node *start)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
int data;
printf("\nEnter data in the new node: ");
scanf("%d",&data);
newnode->info=data;

if(start=NULL)
{
start=newnode;
newnode->next=NULL;
}
else
{
newnode->next=start;
start=newnode;
}
return start;
}

int isEmpty(struct node *start)


{
if(start=NULL)
return 1;
else
return 0;
}

struct node *pop(struct node *start)


{
if(isEmpty(start))
{
printf("STACK UNDERFLOW");
}
else
{
FR. CONCEICAO RODRIGUES
COLLEGE OFENGINEERING
Department of Electronics and
struct node *temp=start;
Computer Science
start=temp->next;
free(temp);
}
return start;
}

void peek(struct node *start)


{
if(isEmpty(start))
{
printf("STACK UNDERFLOW");
}
else
{
printf("\nElement at the top of the stack is:%d",start->info);
}
}

void display(struct node *start)


{
if(isEmpty(start))
{
printf("STACK UNDERFLOW");
}
else
{
struct node *temp=start;
while(temp!=NULL)
{
printf("\n%d",temp->info);
temp=temp->next;
}
}
}

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)

Date and Signature of teacher:

You might also like