Module 4 DSA
Module 4 DSA
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.
• 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
• 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 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
• 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.