0% found this document useful (0 votes)
29 views

Module 4 DSA

The document discusses linked lists, a data structure where each node contains a data field and a pointer to the next node. It describes the basic components of linked lists like the head node and null pointers. It then covers the advantages and disadvantages of linked lists compared to arrays. Various types of linked lists are defined like singly, doubly, and circular linked lists. Functions for creating linked lists like allocating memory and connecting nodes are demonstrated. Insertion operations at the beginning and end of singly linked lists are also explained.

Uploaded by

barotshreedhar66
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Module 4 DSA

The document discusses linked lists, a data structure where each node contains a data field and a pointer to the next node. It describes the basic components of linked lists like the head node and null pointers. It then covers the advantages and disadvantages of linked lists compared to arrays. Various types of linked lists are defined like singly, doubly, and circular linked lists. Functions for creating linked lists like allocating memory and connecting nodes are demonstrated. Insertion operations at the beginning and end of singly linked lists are also explained.

Uploaded by

barotshreedhar66
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Module 4 data structure

Linked list
17/12/2023 2
Linked List
• Collection of nodes that are randomly stored in the memory.
• Each node stores the data and the address of the next node.
• We have to start somewhere, so we give the address of the first node a special name
called HEAD
• The last node of the list contains pointer to the null.

• Why use linked list over array?


• It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
• Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available memory
space.
Advantages and Disadvantages of Linked List

• Advantages:
• Linked lists are dynamic data structures: That is, they can grow or shrink during
execution of a program.
• Efficient memory utilization: Here memory is not pre-allocated.
• Insertion and deletions are easier and efficient: Linked list provide flexibility in inserting a
data item at a specified position and deletion of a data item from the given position.
• Disadvantages :
• Random access is not allowed. We have to access elements sequentially starting from
the first node. So it is difficult to perform binary search with linked lists.
• It cannot be easily sorted.
• More complex to create than an array.
• Extra memory space for a pointer is required with each element of the list.
Operations on Linked list

• Creation
• Insertion
• Deletion
• Traversing
• Searching
• Concatenation
• Display
• Types of Linked List
Following are the various types of linked list.
• Singly Linked List − Item navigation is forward only.
• Doubly Linked List − Items can be navigated forward and backward.
• contains a link to the previous node

• Circular Linked List − Last item contains link of the first element as next and the
first element has a link to the last element as previous.
Structure in C (linked representation)
• Collection of variables (can be of
different types) under a single Example:
name.
• To define a structure, the struct struct Person {
keyword is used.
• Syntax of structure
char name[50];
struct structureName int citNo;
{ float salary;
dataType member1;
dataType member2; };
...
};
Pointers in C
• The pointer in C language is a variable which stores the address of another variable.
• This variable can be of type int, char, array, function, or any other pointer. For
example
• int a = 10;
• int* ptr = &a; // Variable p of type pointer is pointing to the address of the variab
le n of type integer.
Self Referential structure
• Self Referential structure is a structure which contains a pointer to a
structure of the same type. Example

Struct abc {
int a;
char b;
struct abc *self;
};
• Self Referential structure is used to create a node of the linked list.
Creating a node of a linked list

• Each node consists: • Creating single node


• A data item struct node
• An address of next node
{
• So a node can be created using
structures int data;
• Each struct node has a data item struct node *next;
and a pointer to another struct };
node
Steps to create the linked list
• Step 1-Creating node structure
• Step 2- Initialize nodes
• Step 3- Allocate memory using malloc function
• Step 4-Assign data values
• Step 5-Connect nodes
Creating a node

• Step 1-Creating node structure


Step 3- /* Allocate memory */
struct node one = malloc(sizeof(struct node));
{ two = malloc(sizeof(struct node));
int data; three = malloc(sizeof(struct node));
struct node *next; Step 4- /* Assign data values */
}; one->data = 1;
• Step 2- /* Initialize nodes */
two->data = 2;
struct node *head=NULL; three->data=3;
struct node *one = NULL; Step 5- /* Connect nodes */
struct node *two = NULL; one->next = two;
struct node *three = NULL; two->next = three;
three->next = NULL;
Step 6- /* Save address of first node in head */
head = one;
int main() {

Main program // Initialize nodes


struct node *head=NULL;
// Linked list implementation in C
struct node *one = NULL;
#include <stdio.h>
struct node *two = NULL;
#include <stdlib.h>
struct node *three = NULL;
// Creating a node
// Allocate memory
struct node { one = malloc(sizeof(struct node));
int value; two = malloc(sizeof(struct node));
struct node *next; three = malloc(sizeof(struct node));
}; // Assign value values
// print the linked list value using a temporary one->value = 1;
pointer temp two->value = 2;
void printLinkedlist (struct node *temp) { three->value = 3;
while (temp != NULL) { // Connect nodes
printf("%d ", p->value); one->next = two;
temp = temp->next; two->next = three;
}} three->next = NULL;
/* Allocate memory */
Doubly linked list one = malloc(sizeof(struct node));
• Creating a node two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
struct node {
/* Assign data values */
int data;
one->data = 1;
struct node *next; two->data = 2;
struct node *prev; three->data = 3;
} /* Connect nodes */
one->next = two;
/* Initialize nodes */
one->prev = NULL;
struct node *head=NULL;
two->next = three;
struct node *one = NULL; two->prev = one;
struct node *two = NULL; three->next = NULL;
struct node *three = NULL three->prev = two;
/* Save address of first node in head */
head = one;
Circular linked list • /* Assign data values */
Creating a node • one->data = 1;
struct node {
• two->data = 2;
int data;
• three->data = 3;
struct node *next;
struct node *prev;
} • /* Connect nodes */
• * Initialize nodes */ • one->next = two;
struct node *head;
struct node *one = NULL;
• two->next = three;
struct node *two = NULL; • three->next = one;
struct node *three = NULL;

• /* Save address of first node in head */


• /* Allocate memory */
• one = malloc(sizeof(struct node)); • head = one;
• two = malloc(sizeof(struct node));
Singly linked list

Inserting a new node in a linked list


• Case 1:The new node is inserted at the beginning
• Case 2:The new node is inserted at the end
• Case 3:The new node is inserted after a given node
• Case 4:The new node is inserted before a given node
Insertion at the beginning

• Void InsertBegin(int value)


• struct node *newNode =null;
• Create and allocate memory for new node
• newNode = malloc(sizeof(struct node))
• Store data
• newNode->data = value
• Check if it is the first node of the linked list
• set next of new node to null value • If (head==null)
• set head to recently created node newnode->next=null;
• If it is not the first node then head=newnode;
• set next of new node to head
• set head to recently created node
• Else
newNode->next = head;
head = newNode;
• Void InsertEnd(int value)
Insertion at the end
• struct node *newNode =null;
• Create and allocate memory for new node • newNode = malloc(sizeof(struct node))
• Store data in newnode and set its next field • newNode->data = value
as null as node is inserted at the end. • newNode->next = NULL
• Check if it is the first node of the linked list, • If(head==null)
then
• head=newnode;
• set next of new node to null
• newNode->next = NULL;
• set head to point to recently created
node • Else
• Else ,Traverse to last node using temp pointer • struct node *temp = head
which initially points to head node while(temp->next != NULL)
• Change next of last(temp) node to recently {
created node
temp = temp->next;
}
temp->next = newNode;
newNode->next = NULL;
Inserting a node after a given node(having value NUM)
• Create and allocate memory for new node Void Insertafter(int value)
• Store data 1) struct node *newNode =NULL;
• Traverse the linked list upto the node using
temp pointer after which the node has to be newNode = malloc(sizeof(struct node))
inserted 2) set newnode->data=value
• Point the next pointer of new node to the
3) set temp =head
next pointer of temp node.
• Point the next pointer of temp node to next 4)Repeat step 5 while temp->data!=NUM
pointer of newnode. 5) Set temp=temp->next
End of loop
6) Set newnode->next=temp->next
7) Set temp->next=newnode
8) exit
Inserting a node before a given node(having value NUM)
• Create and allocate memory for new Void InsertBefore (int value)
node, Store data in newnode 1) struct node *newNode=NULL
newNode = malloc(sizeof(struct node))
• Traverse the linked list using temp
2) set newnode->data=value
pointer upto the node before which the
node has to be inserted. Use a q pointer 3) set temp =head
which points to the node which is just 4) Repeat steps 5 to 6 while temp->data!=NUM
behind of temp pointer. 5) Set q=temp
• Point the next pointer of newnode to the 6) Set temp=temp->next
temp node. End of while loop
• Point the next pointer of q to the 7) newnode->next=temp
newnode node. 8) q->next=newnode
9) exit
Deleting a node from a linked list
• Case 1:The first node is deleted
• Case 2:The last node is deleted
• Case 3:The node for a given position
Deleting the first node from a linked list
1) Check the value of head pointer. If it Void DeleteBegin()
is equal to null then print 1) If head==null
“underflow” write “underflow”
2) Else if check next field of head. If it go to step no 5
is equal to null, it means it is the end of if
last/only node of the link list, then 2) Else if(head->next==null)
print the node value and set head printf (“%d”, head->data)
to null value.
Set head=null
3) Else use a temp pointer .Set its free(head)
value as head pointer, print the 3) Else
value and shift the head to the next
Set temp=head
node. Delete the node where temp
is pointing printf (“%d”, head->data)
Set head=head->next
4) free the memory used by temp
4) Free temp
pointer.
5) exit
5) End
Deleting the last node from a linked list Void DeleteEnd()
1) If head==null
1) Check the value of head pointer. If it is equal to write “underflow”
null then print “underflow”
go to step no 8
2) Else if check next field of head. If it is equal to
end of if
null, it means it is the last/only node of the link
list, then print the node value and set head to 2) Else if(head->next==null)
null value. printf (“%d”, head->data)
3) Else use a temp pointer to reach to the last node Set head=null
of the linked list. Set its initial value as head
pointer. Use a q pointer which points to the node free(head)
which is just behind of temp pointer. Repeat 3) temp=head
steps 4 and 5 till next field of temp becomes null. Repeat steps 4 and 5 while temp->next!= null
4) Point the next pointer q node to the temp node. 4) Set q=temp
5) Set the temp to next pointer of temp. 5) Set temp=temp->next
6) Print the value, Set next field of q as null value as end of loop
now it is the last node of the linked list.
6) printf (“%d”, temp->data)
7) Free the memory of temp using free command.
Set q->next=null
8) exit
7) Free temp
Deleting the node of a specified position
• Check the value of head pointer. If it • Void DeleteSpos()
is equal to null then print 1) If head==null
“underflow”
write “underflow” and exit
• Specify the position of the node to
be deleted 2) Else
• Use a for loop to traverse to the temp=head
node to be deleted For(int i=1;i<pos;i++)
• Use a temp pointer to hold the
q=temp
address of the node to be deleted
• Use a q pointer which points to the temp=temp->next;
node which is just behind of temp q->next=temp->next
pointer
free(temp)
• Print the value, set next pointer of q
as next pointer of temp
• Delete the temp node and free the
memory
Algorithm
printing/displaying a linked list
1) Check the value of head pointer. If it is equal to null then print “Empty list”
2) Else if check next field of head. If it is equal to null, it means it is the last/only
node of the link list, then print the node value and set head to null value.
3) Else use a temp pointer to traverse the linked list .Set its value as head pointer
Display the values of the linked list till temp reaches to last node.
4) End
Doubly linked list
• Variation of Linked list in which
navigation is possible in both
ways, either forward and
backward easily.
• every node has a link to its
previous node and next node.
Insertion at the beginning
C code:
• Step 1 - Create a newNode with given
void insert_begin(int value) {
value ,set newNode →
previous as NULL. struct Node *newnode=NULL;
newnode = malloc(sizeof(struct Node));
• Step 2 - Check whether list
is Empty (head == NULL) newnode -> data = value;
newnode -> previous = NULL;
• Step 3 - If it is Empty then,
assign NULL to newNode → if(head == NULL) {
next and newNode to head. newnode -> next = NULL;
• Step 4 - If it is not Empty then, head = newnode; }
assign head to newNode → else {
next ,head->previous=newNode newnode -> next = head;
and newNode to head.
head->previous=newnode;
head = newnode;
}
printf("\nInsertion success!!!");
void insert_end(int value) {
Insertion at the end
struct Node *newnode=NULL;
newnode = malloc(sizeof(struct Node));
• Algorithm:
newnode -> data = value;
• Step 1 - Create a newNode with given value
newNode -> next = NULL;
and newNode → next as NULL.
if(head == NULL) {
• Step 2 - Check whether list
newNode -> previous = NULL;
is Empty (head == NULL)
head = newNode; }
• Step 3 - If it is Empty, then
else {
assign NULL to newNode →
previous and newNode to head. temp = head;

• Step 4 - If it is not Empty, then, define a node while(temp -> next != NULL) {
pointer temp and initialize with head. temp = temp -> next;

• Step 5 - Keep moving the temp to its next }


node until it reaches to the last node in the temp -> next = newNode;
list (until temp → next is equal to NULL). newNode -> previous = temp;
• Step 6 - Assign newNode to temp → }
next and temp to newNode → previous. printf("\nInsertion success!!!");
}
Code:
Insertion after an element void insert_afterpos(int value) {
struct Node *newnode=NULL;
• Algorithm: newnode = malloc(sizeof(struct Node));
• Allocate memory to the new node newnode -> data = value;
• Store data printf("Enter element after which to insert
• Traverse the Linked list upto the node using temp new element:");
pointer after which the node has to be inserted. scanf("%d",&val);
(temp=temp->next)
temp=head;
• Point the previous pointer of temp->next to
newnode while(temp>data!=val) {
• Point the next pointer of new node to temp-
temp=temp->next;
>next. }
• Point the next pointer of temp to the new node. temp->next->previous=newnode;
• Point the previous pointer of the new node to newnode->next = temp->next;
temp. temp->next = newnode;
newnode->previous=temp;}
void insert_beforepos(int value) {
Insertion before an element struct Node *newnode=NULL;
newnode = malloc(sizeof(struct Node));
newnode -> data = value;
• Algorithm:
printf("Enter element after which to insert new
• Allocate memory to the new node element:");
• Store data scanf("%d",&val);
newnode->data=num;
• Traverse the Linked list upto the node using
temp pointer before which the node has to temp=head;
be inserted. while(temp->data!=val) {
temp=temp->next;
• Point the previous pointer to the new node
to the previous pointer of temp. }
newnode->previous=temp->prev;
• Point the next pointer of temp->previous to
temp->previous->next=newnode;
the new node.
newnode->next=temp;
• Point the next pointer of new node to temp temp->previous=newnode; return;
• Point the previous pointer of temp to the }
new node.
void delete_beg() {

Delete from beginning if(head==NULL) {


printf("The list is empty!!"); }
else if(head->previous==NULL && head-
• Step 1 - Check whether list is Empty (head == NULL) >next==NULL)
• Step 2 - If it is Empty then, display 'List is Empty!!! {
Deletion is not possible' and terminate the function. printf("Deleted element is %d",head->data);
• Step 3 - If it is not Empty then check whether list is head=NULL;
having only one node (head → previous ==NULL &&
free(head); }
head → next==NULL)
else {
• Step 5 - If it is TRUE, then set head to NULL and
delete head (deallocate the memory) temp=head;
• Step 6 - If it is FALSE, then define a Node head=head->next;
pointer 'temp' and initialize with head, assign head → head->previous=NULL;
next to head, NULL to head → previous and printf("Deleted element is %d",temp->data);
delete temp(deallocate the memory).
free(temp);
} }
Deleting node from the end void delete_end() {
• Step 1 - Check whether list if (head==NULL) {
is Empty (head == NULL)
printf("The list is empty!!"); }
• Step 2 - If it is Empty then, display 'List is
else if(head->prev==NULL && head->next==NULL)
Empty!!! Deletion is not possible' and terminate {
the function.
printf("Deleted element is %d",head->data);
• Step 3 - If it is not Empty then check whether list
is having only one node (head → head=NULL;
previous ==NULL && head → next==NULL) free(head);
• Step 5 - If it is TRUE, then set head to NULL and } else {
delete head (deallocate the memory temp=head;
• Step 6 - If it is FALSE, then define a Node while(temp->next!=NULL)
pointer 'temp' and initialize with head ,keep
{temp=temp->next; }
moving temp until it reaches to the last node in
the list. (until temp → next is equal to NULL) printf("Deleted element is %d",temp->data);
• Step 7 - Set next of temp->prev to NULL pointer temp->prev->next=NULL;
q which points to the previous node of temp .Set free(temp); } }
q->next=null and delete temp.
Deleting node from a specific position
• Step 1 - Check whether list is Empty (head == NULL) printf("Enter position to delete:");

• Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not scanf("%d",&pos);


possible' and terminate the function. int delete_pos() {

• Step 3 - If it is not Empty then check whether list is having only one node int pos,i;
(head → previous ==NULL && head → next==NULL) if(head==NULL) {
• Step 4 - If it is TRUE, then set head to NULL and delete head (deallocate printf("List is empty!!");
the memory
return 0; }
• Step 5 - If it is FALSE, then define a Node pointer 'temp' and initialize Else if(head->prev==NULL && head->next==NULL) {
with head ,keep moving temp until it reaches to the exact node which we
want to delete. Use a pointer q which points to the previous node of temp printf("Deleted element is %d",head->data);
head=NULL;
• Step 6 - Set q->next=temp->next and temp->next->prev=temp->next and
delete temp (free(temp)). free(head); }
Else { temp=head;
for(i=1;i<pos;i++) {
q=temp;
temp=temp->next; }
q->next=temp->next;
temp->next->prev=temp->next;
printf("Deleted element is %d",temp->data);
free(temp);
}
Displaying linked list
• Step 1 - Check whether list is Empty (head == NULL) void display()
{
• Step 2 - If it is Empty, then display 'List is
if(head==NULL)
Empty!!!' and terminate the function.
{
• Step 3 - If it is not Empty, then define a Node
printf("List is empty!!");
pointer 'temp' and initialize with head.
}
• Step 4 - Keep displaying temp → data with an arrow else
(<===>) until temp reaches to the last node
{
• Step 6 - Finally, display temp → data with arrow temp=head;
pointing to NULL (temp → data ---> NULL). printf("The linked list is:\n");
while(temp!=NULL)
{
printf("%d< = = >",temp->data);
temp=temp->next;
}
}
circular linked list
• Insertion
• Deletion
• Display
Step 1 - Include all the header files which are used in the program.
Step 2 - Declare all the user defined functions.
Step 3 - Define a Node structure with two members data and next
Step 4 - Define a Node pointer 'head' and set it to NULL.
Step 5 - Implement the main method by displaying operations menu and
make suitable function calls in the main method to perform user selected
operation.
Insertion

• Inserting At Beginning of the list


• Inserting At End of the list
• Inserting At before any element in the list
• Inserting At after an element in the list
Inserting At Beginning of the list
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, set head = newNode and newNode→next = head .
Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with
'head'.
Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node
(until 'temp → next == head').
Step 6 - Set 'newNode → next =head', 'head = newNode' and 'temp →
next = head'.
Inserting At End of the list

Step 1 - Create a newNode with given value.


Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = newNode and newNode → next = head.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in
the list (until temp → next != head).
Step 6 - Set temp → next = newNode and newNode → next = head.
Inserting a node after a given node(having value NUM)
• Create and allocate memory for new node 1) struct node *newNode
• Store data newNode = malloc(sizeof(struct node))
• Traverse the linked list upto the node using
temp pointer after which the node has to be 2) set newnode->data=val
inserted 3) set temp =head
• Point the next pointer of new node to the
4)Repeat step 5 while temp->data!=NUM
next pointer of temp node.
• Point the next pointer of temp node to next 5) Set temp=temp->next
pointer of newnode. End of loop
6) Set newnode->next=temp->next
7) Set temp->next=newnode
8) exit
Inserting a node before a given node(having value NUM)
• Create and allocate memory for new 1) struct node *newNode
node, Store data in newnode newNode = malloc(sizeof(struct node))
• Traverse the linked list using temp 2) set newnode->data=val
pointer upto the node before which the 3) set temp =head
node has to be inserted. Use a q pointer 4) Repeat steps 5 to 6 while temp->data!=NUM
which points to the node which is just 5) Set q=temp
behind of temp pointer.
6) Set temp=temp->next
• Point the next pointer of newnode to the End of while loop
temp node.
7) newnode->next=temp
• Point the next pointer of q to the 8) q->next=newnode
newnode node.
9) exit
Deletion operation
• Deleting from Beginning of the list
• Deleting from End of the list
• Deleting a Specific Node
Deleting from Beginning of the list
Step 1 - Check whether list is Empty (head == NULL)
If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 2 - Else if check next field of head. If it is equal to head, it means it is the
last/only node of the link list, then print the node value, set head to null value
and free the head node.
Step 3 - Else set temp=head, q=head and traverse the linked list till last node
using temp.
Step 4:Set head=head->next, temp->next=head.Delete the q node and free
the memory.
Deleting from End of the list

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function. If it is Not Empty then, define
temp pointer and initialize 'temp' with head.
Step 3 - Else if check next field of head. If it is equal to head, it means
it is the last/only node of the link list, then print the node value and
set head to null value.
Step 4 - Else define a node pointer temp and initialize with head.
Keep moving the temp to its next node until it reaches to the previous
of last node in the list (until temp → next → next != head). Use a q
pointer which points to the node which is just behind of temp pointer.
Step 5 - free temp → next and set temp->next=head
Deleting the node of a specified position

• Check the value of head pointer. If it is equal to null then print


“underflow”
• Specify the position of the node to be deleted
• Use a pointer to traverse to the node to be deleted
• Use a temp pointer to hold the address of the node to be deleted
• Use a q pointer which points to the node which is just behind of temp
pointer
• Set next pointer of q as next pointer of temp
• Delete the temp node and free the memory
Displaying a circular Linked List

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the
function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and
initialize with head.
Step 4 - Keep displaying temp → data with an arrow (--->)
until temp reaches to the head node
Step 5 - Finally display temp → data with arrow pointing to head →
data.
Linked representation of STACK
• push - place an item on the stack
• peek - Look at the item on top of the stack, but do not
remove it
• pop - Look at the item on top of the stack and remove it
Representation of stack using linked list
• Why linked list representation
• Where to insert and delete stack elements in the linked list
• Prefer at the beginning of the stack
Push operation using linked list
• Step 1:create a new code, allocate memory and Void push(int value)
insert data {
struct node *newNode = NULL;
• Step 2:Check if it is the first node (top==NULL).If yes,
Newnode=malloc(sizeof(struct Node));
then set newNode->next=NULL and top=newNode
newNode->data = value;
• Step 3:Else put the address of the top node in the if(top == NULL)
next field of newNode {
• Step 4:Update the top pointer and make it point to newNode->next = NULL;
the new node of the linked list top=newNode;
}
else
{
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
}
Pop operation Void pop()
• Step 1 - Check whether stack is Empty (top == If(top == NULL)
NULL).
printf("\nStack is Empty!!!\n");
• Step 2 - If it is Empty, then display "Stack is
Empty!!! Deletion is not possible!!!" and terminate Else{
the function struct Node *temp = top;
• Step 3 - If it is Not Empty, then define printf("\nDeleted element: %d", temp-
a Node pointer 'temp' and set it to 'top'. >data);
• Step 4 - Then set 'top = top → next'. top = temp->next;
• Step 5 - Finally, delete 'temp'. (free(temp)). free(temp);
}
}
Peek operation Void peek()
• Step 1 - Check whether stack is Empty (top == {
NULL).
If(top == NULL)
• Step 2 - If it is Empty, then display "Stack is
Empty!!! Deletion is not possible!!!" and terminate printf("\nStack is Empty!!!\n");
the function Else{
• Step 3 - If it is Not Empty, then display the top printf("\ntop element is: %d", top->data);
value(top->data)
}
}
display operation
• Step 1 - Check whether stack if(top == NULL)
is Empty (top == NULL).
printf("\nStack is Empty!!!\n");
• Step 2 - If it is Empty, then display 'Stack is
Empty!!!' and terminate the function. else{
• Step 3 - If it is Not Empty, then define a Node struct Node *temp = top;
pointer 'temp' and initialize with top.
• Step 4 - Display 'temp → data' and move it to the
while(temp->next != NULL){
next node. Repeat the same until temp reaches to printf("%d--->",temp->data);
the first node in the stack. (temp → next != NULL).
temp = temp -> next;
• Step 5 - Finally! Display 'temp → data ---> NULL'.
}
printf("%d--->NULL",temp->data);
}
Representation of queue using linked list
• In a linked queue, each node of the queue consists of two parts i.e. data part and
the next part.
• Each element of the queue points to its immediate next element in the memory.
• Operations with queue:
• Enqueue – Insert a node to the linked list from rear end
• Dequeue- - Delete a node from the front end
• Display – Display the data value of the nodes in the linked list
Representation of queue using linked list
• 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 two Node pointers 'front' and 'rear' and set both
to NULL.
• Step 4 - Implement the main method by displaying Menu of list of
operations and make suitable function calls in the main method to
perform user selected operation.
Enqueue operation using linked list
• Create and allocate memory for new node Void enqueue(int value)
{
• Store data in newnode and set its next field as
null as node is inserted at the end. struct node *newNode = NULL;
• Check if it is the first node of the linked list, Newnode=malloc(sizeof(struct Node));
then newNode->data = value;
• set front and rear to point to recently newnode->next=NULL;
created new node
if(front==NULL && rear==NULL) {
• Else ,set next pointer of rear to newnode.
front=newnode;
• Change rear to newnode. rear=newnode; }
else {
rear->next=newnode;
rear=newnode;
}}
dequeue operation using linked list
• Step 1 - Check whether queue is Empty (front == Void dequque()
NULL). {
• Step 2 - If it is Empty, then display "Queue is
if(front == NULL)
Empty!!! Deletion is not possible!!!" and terminate
from the function printf("\nQueue is Empty!!!\n");
• Step 3 - If it is Not Empty then, define a Node else{
pointer 'temp' and set it to 'front'. struct Node *temp = front;
• Step 4 - Then set 'front = front → next' and delete front = front -> next;
'temp' (free(temp)).
printf("\nDeleted element: %d\n",temp->data);
free(temp);
}}
display operation using linked list

• Check Code:
whether queue
is Empty (front == NULL). if(front == NULL)
• If it is Empty then, display 'Queue is printf("\nQueue is Empty!!!\n");
Empty!!!' and terminate the function. else{
• If it is Not Empty then, define a Node struct Node *temp = front;
pointer 'temp' and initialize while(temp != NULL){
with front.
printf("%d--->",temp->data);
• Display 'temp → data --->' and move
it to the next node. Repeat the same temp = temp -> next;
until 'temp' reaches to 'rear' (temp → }
next != NULL). }
Applications of linked list-polynomial addition
• Linked list is a data structure that stores each element as an object in a node of
the list. every node contains two data parts and links to the next node.
• Polynomial is a mathematical expression that consists of variables and
coefficients. for example x^2 - 4x + 7
• In the Polynomial linked list, the coefficients and exponents of the polynomial
are defined as the data node of the list.
• Coefficient Field – The coefficient field holds the value of the
coefficient of a term
• Exponent Field – The Exponent field contains the exponent value of
the term
• Link Field – The linked field contains the address of the next term in
the polynomial
Algorithm
• Include libraries and create structure of the node.
• Initialize three head pointers head1, head2, head3, for the two input polynomials and
resultant polynomial. Then, we create two linked list by inserting coefficients and
exponent values to both the linked lists.
• When polynomials are created in sorted order, next step is to add them according to the
exponent value. Consider pointer temp1 and temp2 for traversing poly1 and poly2.Use
temp3 pointer for the resultant polynomial. We will consider three cases:
Case 1:If exponent value of poly1 is equal to the exponent value of poly 2,then add
their coefficient terms directly and output their addition. Move temp1, temp2 and
temp3 to the next position.
Case 2:If exponent value of poly1 is greater than the exponent value of poly 2,then
output poly1 as it is. Move temp1, temp3 to the next position.
Case 3:If exponent value of poly1 is less than the exponent value of poly 2,then
output poly2 as it as. Move temp2, temp3 to the next position
• Continue to append the remaining nodes(temp1->next==temp2->next==NULL)
from or until we finish the calculation on all nodes
Memory allocation and de allocation in linked list
• malloc() function
• Stands for memory allocation
• Used to allocate a block of memory dynamically.
• ptr = (cast_type *) malloc (byte_size);
• It reserves memory space of specified size and returns the pointer pointing to
the memory location.
• Example: ptr = (int *) malloc (50)
• When this statement is successfully executed, a memory space of 50 bytes is
reserved. The address of the first byte of reserved space is assigned to the
pointer ptr of type int.
• free() function
• release/deallocate memory in C.

You might also like