Complete Module 3 Notes
Complete Module 3 Notes
VASAVI. A
Introduction to Linked List
Array:
Array is a linear data structure where all elements are arranged sequentially.
Limitations of Arrays:
• Arrays have a fixed size, which can lead to memory waste if the allocated
size is larger than the actual data.
• Resizing an array often requires creating a new one and copying elements,
which can be inefficient.
• Since array is static data structure, we should give size at the time of
declaration.
Less flexible:
Arrays can only store elements of same data type and their structure cannot be
easily adapted to different types (e.g. singly linked list, double, circular linked
list) like linked list.
VASAVI. A
Arrays Vs Linked List
An array is a consistent set of fixed number A Linked list is an ordered set of a variable
of data items. no. of data items.
They are stored in contiguous memory They are not stored in contiguous memory
locations. locations.
The memory allocation is done at compile The memory allocation is done at run time.
time.
The insertion and deletion operations The insertion and deletion operations take
require more time to execute. less time to execute.
VASAVI. A
Linked List:
1. Data part
2. Link part (reference to next node) or next part
The link part (next part) contains address of the next node of the list.
VASAVI. A
• head always points to first node of the LL. It will have address of 1st node.
• Last node of LL is NULL.
VASAVI. A
Singly Linked List
• A singly linked list is a fundamental data structure, consisting
of nodes where each node contains a data field and a reference to the next
node in the node.
• The last node points to null, indicating the end of the list.
• This linear data structure allows for efficient insertion and deletion
operations, making it a popular choice for various applications.
Struct node
{
int info;
Struct node *next;
}
Operations on Singly Linked List:
1. Create node
2. Traversal
3. Insertion
4. Deletion
5. Search
VASAVI. A
Creating a node in Singly Linked List:
Struct node
{
int info;
The process of visiting each node of the list once is called traversing.
VASAVI. A
• Now, move ptr forward
VASAVI. A
ptr = ptr → next
ptr != null
VASAVI. A
Implementing traversal:
traverse(struct node * head)
if(head = = NULL)
NULL
VASAVI. A
Insertion into Singly Linked List:
VASAVI. A
Inserting a node at the beginning of linked list:
Steps:
Since we are adding new node at the beginning, add head address in new
node’s address., i.e., 1000 in new node.
VASAVI. A
insertbeginning(struct node * head, int info) // new node’s info
return head;
VASAVI. A
Inserting node at the end of the Single Linked List:
VASAVI. A
insert_end(struct node * head, int info) // data part of new node
{
struct node * ptr, * new; // ptr for traversing, new node to insert
new = (struct node *) malloc (sizeof(struct node));
new→data=info; // 25 will be added in new nodes data
new→next=null;
ptr=head; // 1000 will be added in ptr
if(ptr!=null)
{
while(ptr→next!=null)
{
ptr=ptr→next;
}
ptr→next=new;
}
else
head=new; // if ptr=null 1st node is null. Directly new
will be assigned to head
return head;
VASAVI. A
Inserting new node after a given node in SLL:
VASAVI. A
insertafter(struct node * head, int x, int info) // x is 23 info is 55
Struct node * ptr, *new; // ptr for traversing, new node to insert
ptr=ptr→next;
if(ptr→data==x) // 23 == 23
return head;
VASAVI. A
Deletion from Singly Linked List:
1. From beginning of LL
2. From end of the LL
3. From middle of a LL (After, Before)
• To delete first node of a linked list, (1000), directly point head to second
node (2000).
• Automatically, first node 1000 will be deleted.
VASAVI. A
delfirst(struct node * head)
{
struct node * ptr;
if(head==null)
printf(“List is empty”);
else
{
ptr=head;
head=head→next;
free(ptr);
}
return head;
}
VASAVI. A
Deleting a node from end of the SLL:
VASAVI. A
del_last(struct node * head)
{
struct node * ptr, *prev;
if(head==null)
{
printf(“Linked List is empty”);
}
elseif(head→next==null)
{
free(head);
head=null;
}
else
{
ptr=head;
while(ptr→next!=null)
{
prev=ptr;
ptr=ptr→next;
}
prev→next=null;
free(ptr);
}
return head;
}
VASAVI. A
Deleting a node after a given node in SLL:
VASAVI. A
delete_after(struct node * head, int key) // key is 23 300 should be deleted
{
struct node * ptr1, * ptr2;
ptr1=head;
while(ptr1→next!=null)
{
if(ptr1→data==key)
{
ptr2=ptr1→next;
ptr1→next=ptr2→next;
free(ptr2);
break;
}
ptr1=ptr1→next;
}
return head;
}
VASAVI. A
Search a node in SLL:
Searching in a Singly Linked List refers to the process of looking for a
specific element or value within the linked list.
Steps for searching:
4. If the end of the list is reached without finding a match, return false.
Code:
VASAVI. A
Display (print) elements of Single linked list:
We must traverse from first to last in the singly linked list to display it all.
In contrast to arrays, a linked list’s nodes cannot be accessed arbitrarily.
Therefore, to get to the nth element, we must first walk through all (n-1) elements.
VASAVI. A
Retrieve node of a Single linked list:
VASAVI. A
Module 3
Linear Data Structures – Linked list
• Introduction to Linked List
• Representation of LL
• Linked List Vs Arrays
• Types of Linked List:
❖ Singly Linked List
❖ Doubly Linked List
❖ Circular Linked List
• Operations on Singly Linked List
❖ Create List
❖ Insert Node (Empty list, beginning, middle, end)
❖ Delete Node (First, general case)
❖ Search list
❖ Retrieve node
❖ Print list
• Operations on Doubly Linked List
❖ Create List
❖ Insert Node (Empty list, beginning, middle, end)
❖ Delete Node (First, general case)
❖ Search list
❖ Retrieve node
❖ Print list
• Stack using Singly Linked List
• Queue using Singly Linked List
• Singly Linked List Applications
❖ Polynomial representation and Addition
VASAVI. A
Doubly Linked List
• In a singly linked list, we can traverse only in one direction(forward) i.e.,
each node stores the address of the next node in the linked list.
• It has no knowledge about where the previous node lies in the memory.
• If we are at the 12th node, and if we want to reach 11th node in the linked
list, we must traverse right from the first node.
• Some applications require us to traverse in both forward and backward
directions.
• Here, we can store in each node not only the address of the next node but
also the address of previous node in the linked list. This arrangement is
“Doubly Linked List”
VASAVI. A
Singly Linked List Doubly Linked List
Struct node Struct node
{ {
int data; Struct node * prev;
Struct node * next; int data;
} Struct node * next;
}
1. Create node
2. Traversal
3. Insertion
4. Deletion
5. Search
VASAVI. A
Creating a node of Doubly Linked List:
Struct node
{
Struct node * prev;
int data;
Struct node * next;
}
int main()
{
Struct node * head=malloc(sizeof(struct node));
head→prev=null;
head→data=10;
head→next=null;
}
VASAVI. A
Inserting a node in an empty doubly linked list:
#include<stdio.h>
#include<stdlib.h>
Struct node
{
struct node * prev;
int data;
struct node * next;
}
int main()
{
Struct node * head=NULL; // initially head is NULL
head=addToEmpty(head, 45); // data is 45
printf(“%d”, head→data);
return 0;
}
Struct node * addToEmpty(Struct node * head, int data)
{
Struct node * temp=malloc(sizeof(struct node));
temp→prev=NULL;
temp→data=data; // 45
temp→next=NULL;
head=temp; // 1000
return head;
}
VASAVI. A
Inserting node at beginning of doubly linked list:
temp→next=head;
head→prev=temp;
head=temp;
temp→next=head;
head→prev=temp;
head=temp;
return head;
VASAVI. A
Inserting node at end of doubly linked list:
Step 1:
Traversal (so that we can move till last node and then we can insert new node)
Step 2:
VASAVI. A
Struct node * addAtEnd(Struct node * head, int data)
{
Struct node * new, * ptr;
new=malloc(sizeof(struct node));
new→prev=NULL;
new→data=data;
new→next=NULL;
ptr=head;
while(ptr→next!=NULL)
ptr=ptr→next;
ptr→next=new;
new→prev=ptr;
return head;
}
VASAVI. A
Inserting node at middle of doubly linked list:
while(position!=1)
temp=temp→next;
position--;
VASAVI. A
temp2→prev=newp;
• Now, we should update prev part of new node and next part of new node.
• Prev part should contain address of previous i.e., 2000 and next part of new
node should be 3000.
newp→prev=temp;
newp→next=temp2;
Struct node * addAfterpos(struct node * head, int data, int position) position is 2
{
Struct node * newp=NULL;
Struct node * temp=head;
Struct node * temp2=NULL;
newp= addToEmpty(newp, data)
while(position!=1)
{
temp=temp→next;
position--; position is 2
}
temp2=temp→next;
temp→next=newp;
temp2→prev=newp;
newp→next=temp2;
newp→prev=temp;
return head;
}
VASAVI. A
Adding a node before a specific position (DLL):
VASAVI. A
Code:
Struct node *addBeforepos(struct node * head, int data, int position) position is 3
{
Struct node * newp=NULL;
Struct node * temp=head;
Struct node * temp2=NULL;
newp=addToEmpty(newp, data);
int pos=position; 3
while(pos>2) pos is 3
{
temp=temp→next;
pos--;
}
temp2=temp→next;
temp→next=newp;
temp2→prev=newp;
newp→next=temp2;
newp→prev=temp;
return head;
}
VASAVI. A
Deletion from Doubly Linked List
Method 1:
• Keep one pointer temp pointing to 1000. If head moves forward previous
data will be deleted.
• Now, we will move head forward:
head=head→next;
free(temp);
• Then call free function to delete the 1st node and then we need to assign
NULL to temp and then we should keep NULL to prev part since, 2000
is 1st node now.
head=head→next;
free(temp);
temp=NULL;
head→prev=NULL;
Method 2:
VASAVI. A
Code:
head=head→next;
free(temp);
temp=NULL;
head→prev=NULL;
return head;
VASAVI. A
Deleting the last node of the DLL:
• Keep one pointer pointing to 1st node and then move temp forward.
while(temp→next!=NULL)
{
temp=temp→next;
}
temp2=temp→prev;
temp2→next=NULL;
free(temp);
temp=NULL;
• Keep one more pointer temp2 points to 2000, the node which is before
the node we want to delete.
• Update next part of 2000 so that it will contain NULL because it will be
last node.
• Now free temp i.e., 3000
• 3000 will be deleted. Now place NULL in temp.
VASAVI. A
VASAVI. A
Deleting the intermediate node of the DLL
VASAVI. A
Code:
Struct node * delinter(Struct node * head, int position)
{
Struct node * temp=head;
Struct node * temp1=Null;
if(position==1)
{
head=delfirst(head);
return head;
}
while(position>1)
{
temp=temp→next;
position--;
}
if(temp→next==NULL)
{
head=dellast(head);
}
else
{
temp1=temp→prev;
temp1→next=temp→next;
temp→next→prev=temp1;
free(temp);
temp==null;
} return head;
}
VASAVI. A
Search for a node in doubly linked list
int search(struct node* head, int x)
{
struct node* temp = *head;
int pos = 0;
while (temp->data != x && temp->next != NULL)
{
pos++;
temp = temp->next;
}
if (temp->data != x)
return -1;
return (pos + 1);
}
VASAVI. A
Retrieve node of a Double linked list
VASAVI. A
Display (print) elements of Double linked list
void printList(node* head)
{
node* temp = head;
printf("Linked List elements: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
VASAVI. A
Stack using Singly Linked List
• Since, Linked list is dynamic, we can add n no. of elements.
• Head will be top.
• Insertions and deletions are done from left side.
• If top is NULL, stack is empty (underflow)
Operations on Stack:
Push Operation:
VASAVI. A
push(struct stack * top, int info) // top – top of stack, info of new node
{
new=(struct stack *) malloc(sizeof(struct stack));
if(new==NULL)
Printf(“Stack Overflow”);
if(top==NULL) // if top is NULL, it should be last and first node
{
new→data=info;
new→next=NULL;
top=new;
}
else // if there are already some elements in the list
{
new→data=info;
new→next=top;
top=new;
}
return top;
}
VASAVI. A
Pop Operation:
pop(struct stack * top)
{
struct stack * ptr;
if(top==NULL) // No elements in stack
printf(“List is empty”);
else // If there are elements
{
ptr=top;
top=top→next;
free(ptr);
}
return top;
}
VASAVI. A
Display:
void display()
{
struct node * ptr
if(top==NULL)
{
printf(“Stack is empty”);
}
else
{
ptr=top;
while(ptr!=NULL)
{
printf(“%d\t”, ptr→data);
ptr=ptr→next;
}
}
}
VASAVI. A
Stack using Linked List program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*stack;
void push(int x)
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->next=stack;
stack=new;
return;
VASAVI. A
}
int pop()
{
struct node *ptr;
if(stack==NULL)
{
printf("stack is empty \n");
}
else
{
int x=stack->data;
ptr=stack;
stack=stack->next;
free(ptr);
return x;
}
}
void display()
{
struct node *temp=stack;
if(stack==NULL)
{
printf("stack is empty\n");
}
else
{
VASAVI. A
while(temp!=NULL)
{
printf("%d\t", temp->data);
temp=temp->next;
}
}
}
void main()
{
int ch,stack,x;
while(1)
{
printf("1.push \n");
printf("2.pop \n");
printf("3.display \n");
printf("4. exit \n");
printf("Enter your choice \n");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("enter the element \n");
scanf("%d", &x);
push(x);
break;
case 2: x=pop();
printf("element is popped from the stack is %d \n",x);
VASAVI. A
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
VASAVI. A
Queue using Single Linked List
• Since Linked List is dynamic, we can add n no. of elements.
• Insertions and deletions are done from two ends.
• Data can only be inserted from one end called “rear” end.
• Data can only be deleted from one end called “front” end.
Operations on Queue:
Enqueue():
void insertq(int x)
{
struct node * new;
new=(struct node *)malloc(sizeof(struct node));
new→data=x;
new→next=NULL;
if(front==NULL) // Queue empty
{
front=rear=new;
}
else // There are some elements in queue
{
rear→next=new;
rear=new;
}
}
VASAVI. A
Dequeue():
int deleteq()
{
struct node * ptr;
if(front==NULL)
{
printf(“Queue is empty”);
}
else
{
int x=front→data;
ptr=front;
front=front→next;
free(ptr);
return x;
}
}
VASAVI. A
Display():
void display()
{
struct node * ptr=front;
if(front==NULL)
{
printf(“Queue is empty”);
}
else
{
printf(“The elements of Queue are”);
while(ptr!=NULL)
{
printf(“%d\t”, ptr→data);
ptr=ptr→next;
}
}
}
VASAVI. A
VASAVI. A
Queue using Linked List program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
} * front, * rear;
void insertq(int x)
{
struct node * new;
new=(struct node *)malloc(sizeof(struct node));
new→data=x;
new→next=NULL;
if(front==NULL)
{
front=rear=new;
}
else
{
rear→next=new;
rear=new;
}
}
int deleteq()
{
struct node * temp;
if(front==NULL)
VASAVI. A
{
printf(“Queue is empty”);
}
else
{
int x=front→data;
temp=front;
front=front→next;
free(temp);
return x;
}
}
void display()
{
struct node * temp=front;
if(front==NULL)
{
printf(“Queue is empty”);
}
else
{
printf(“The elements of queue are”);
while(ptr!=NULL)
{
printf(“%d\t”, temp→data);
ptr=ptr→next;
}
}
void main()
VASAVI. A
{
int ch, x;
while(1)
{
printf(“1. Insert \n”);
printf(“2. Delete \n”);
printf(“3. Display \n”);
printf(“4. Exit \n”);
printf(“Enter your choice \n”);
scanf(“%d”, &ch);
switch(ch)
{
case 1: printf(“Enter the element \n”);
scanf(“%d”, &x);
insertq(x);
break;
case 2: x=deleteq();
printf(“The deleted element is %d \n”, x);
break;
case 3: display();
break;
case 4: exit(0);
}
getch();
}
}
VASAVI. A
Circular Linked List
The circular linked list is linked list where all nodes are connected to form a circle.
• In a circular linked list, the first node and the last node are connected to
each other which forms a circle.
• There is no NULL at the end.
VASAVI. A
Circular singly linked list:
The circular singly linked list is like the singly linked list except that the
last node of the circular singly linked list points to the 1st node.
The circular doubly linked list is like the doubly linked list except that the
last node of the circular doubly linked list points to the first node and the first
node of the circular doubly linked list points to the last node.
VASAVI. A
Polynomial representation and Addition
• Polynomial addition in c programming is a fundamental operation used in
many applications such as signal processing, computer graphics, and
scientific simulations.
• We do polynomial addition using linked list in C programming.
• Linked lists provide an elegant solution for efficiently handling
polynomials due to their dynamic memory allocation.
Structure of Node:
VASAVI. A
The linked list node contains three values:
Polynomial Addition:
When we add them together, we can group the like terms and generate the result
VASAVI. A
Q) Add the polynomials
VASAVI. A
Q) Add the polynomials:
VASAVI. A
Algorithm for Performing the Polynomial Addition using Linked List in C
[Initialize segment variables]
[Initialize Counter] Set i=0,j=0,k=0
Repeat while i<t1 and j<t2
IF p1[i].expo=p2[j].expo, THEN
p3[i].coeff=p1[i].coeff+p2[i].coeff
p3[k].expo=p1[i].expo
[Increase counter] Set i=i+1,j=j+1,k=k+1
ELSE IF p1[i].expo > p2[j].expo, THEN
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
[Increase counter] Set i=i+1,k=k+1
ELSE
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
[End of If]
[End of loop]
Repeat while i<t1
p3[k].coeff=p1[i].coeff
p3[k].expo=p1[i].expo
Set i=i+1,k=k+1
[End of loop]
Repeat while j<t2
p3[k].coeff=p2[j].coeff
p3[k].expo=p2[j].expo
Set j=j+1,k=k+1
VASAVI. A
[End of loop]
Return k
EXIT
VASAVI. A
Module 3 Important Questions
1. What is an Array? What are the limitations of an array?
2. Distinguish Array and Linked list.
3. What is a Linked list? Explain how a linked list is represented?
4. Explain how a node is created in a linked list with example code.
5. Explain about types of linked list with example notation.
6. Explain about Singly Linked list. How a node is represented using SLL.
7. Explain about singly linked list traversal with example and code.
8. Explain how a node is inserted in a singly linked list with example and code.
9. Explain how a node is deleted from a singly linked list with example and code.
10. Write a program to search for a given node in singly linked list.
11. Write a program to print elements of a singly linked list.
12. How a node is retrieved from a singly linked list? Explain with example and program.
13. Explain about doubly linked list. How a node is represented using DLL.
14. Explain about doubly linked list traversal with example and code.
15. Explain how a node is inserted in a doubly linked list with example and code.
16. Explain how a node is deleted from a doubly linked list with example and code.
17. Write a program to search for a given node in doubly linked list.
18. Write a program to print elements of a doubly linked list.
19. Distinguish Singly linked list and doubly linked list.
20. Write a program for implementing Stack using Linked list along with example.
21. Write a program for implementing Queue using Linked list along with example
22. Explain in brief about Circular linked list and its types.
23. Write an algorithm for polynomial representation and addition.
24. Numericals on Polynomial representation and addition.
VASAVI.A