Experiment N
Aim:
Implement Stack / Linear Queue ADT using Linked List
Input specification :
‘Accept the element for the stack
Output specification
Display the output of various fundamental operation
Theory:
itack Link it
To represent a stack through linked list we create a structure data type i.e. node.
+ Each node in the list contains data and a pointer to next node,
+ The node structure is defined as follows
struct node
{ 1
int data; ‘er
, node * next; PLRECLHEEOperation of Stack:
The primitive operations on stack as follows
= push : the process of adding a new element to the top of stack is called push operation. After every push
operation the top is incremented by one.
If stack is full i.e. there is no more space to add new element and PUSH operation is performed
then such condition is called overflow of stack.
rion to delete element from stack
void push(int value)
if(top = = max-1) // max is size of array & it is global
variable printf(“Stack Overflow”);
else
stack[++top]=value; _// stack & top is global variable, initial top= - 1
}
= pop : the process of delete an element from the top of stack is called pop operation. After every pop
operation the top is decremented by one.
If stack is empty i.e. there is no element in stack and POP operation is performed then such
condition is called underflow of stack.
function to delete element from stack
void pop() {
if(top == -1)
printf(“Stack Underflow”)
else
printf(“% element is deleted”, stack{top—);isEmpty : this function is used to check whether a stack is empty or not. This function retums boolean
value true (1) if stack is empty otherwise false (0).
int isEmpty ¢
(top==-1) // stack empty
condition return 1;
else
return 0;
isFull :: this function is used to check whether a stack is full or not. This function returns boolean value
true if stack is full otherwise false.
int isEullO {
if(top = = max-
1) return 1;
else
return 0;
splay : this function is used to displaying the elements of stack.
void display)
{int
if(top= = - 1)
printf("\nStack is
Empty!!"); else {
printf(“in Stack element (top to first element) are: \n")
for(i=top; i>=0;
printf("\t%d" stack{i));Algorithm:-
Stack Operations using Linked List
To implement a stack using a linked list, we ne
implementing actual operations.
‘Step 1 - Include all the header files which are used in the program. And declare all
the user defined functions.
+Step 2 - Define a ‘Node’ structure with two members data and next.
*Step 3 - Define a Node pointer ‘top’ and set it to NULL.
«Step 4 - Implement the main method by displaying Menu with list of operations and make
suitable function calls in the main method.
to set the following things before
push(value) - Inserting an element into the Stack
We can use the following steps to insert a new node into the stack.
*Step 1 - Create a newNode with given value.
‘Step 2 - Check whether stack is Empty (top = NULL)
«Step 3 - If it is Empty, then set mewNode — next = NULL.
«Step 4 - If it is Not Empty, then set newNode — next = top.
«Step 5 - Finally, set top = newNode.
pop() - Deleting an Element from a Stack
We can use the following steps to delete a node from the stack.
‘Step 1 - Check whether stack is Empty (top == NULL).
«Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!
terminate the function
Step 3 - If it is Not Empty, then define
‘Step 4 - Then set ‘top = top — next’
«Step 5 - Finally, delete ‘temp’. (free(temp)).
and
Node pointer ‘temp! and set it to ‘top’.
display() - Displaying stack of elements
We can use the following steps to display the elements (nodes) of a stack...
«Step 1 - Check whether stack is Empty (top = NULL).
«Step 2 - If it is Empty, then display 'Stack is Empty!!!" and terminate the function.
«Step 3 - If it is Not Empty, then define a Node pointer ‘temp’ and initialize with top.
«Step 4 - Display 'temp —> data —->' and move it to the next node. Repeat the same
until temp reaches to the first node in the stack. (temp —> next != NULL).
«Step 5 - Finally! Display 'temp — data —> NULL’Program Code :-
Hinclude
Hinclude
struct Node
{
int data;
struct Node *next;
y*top = NULL;
void push(int);
void pop();
‘void display();
void main()
{
int choice, val
lrser();
printf("\n:: Stack using Linked List :
while(1){
print{("\n#***** MENU *###8\n");
printf("I. Push\n2. Pop\n3. Display\nd. Exit\n");
printf{"Enter your choice: ");
scanfi("%d" Schoice);
switch(choice){
se 1: printf(" Enter the value to be insert: ");
scanf("'%d", Svalue);
push(value);
break;
case 2: pop); break;
case 3: display(); break;
ease 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
t
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc sizeof struct Node));
");
newNode->data = value;
if(top = NULL)
newNode->next = NULL;
else
newNode->next = top:
top = newNode;
printf("\nlnsertion is Success!!!\n");
}
void pop()
1
if(top = NULL)printf("\nStack is Empty!!!\n");
else
struct Node *temp = top;
printf("\nDeleted element:
top = temp->next;
free(temp);
}
}
void display)
t
if(top = NULL)
printf("\nStack is Empty!!!\n");
elses
struct Node *temp = top;
while(temp->next != NULL){
printf(" "temp->data);
temp = temp > next;
}
printf("%d-—->NULL" temp>data);
}
fod", temp->data);
Output :-
ene rac
Po ee met Mere
Doo errr
fo Fl
the value to he insert: 18
your choice: 1
the value to be insert: 20_eee ee
pases
cer your choice: 2
Deleted element: 20
os ieee
Person
pees
Corres eces
San
ose’
ren
co
DEE
Prem eer