Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

Linked List Data structure

Dr. Pavithra L K, VIT


Chennai Campus

Dr. Pavithra L K, VIT, Chiennai Campus


Array
• Linear Data structure, elements are stored in contiguous memory location
int a[7]; int b[5]= {11,12,13,14,15};

a 0 1 2 3 4 5 6 index
4000 4004 4008 4012 4016 4020 4024 memory address

11 12 13 14 15 elements
Base Address b 0 1 2 3 4 index
5000 5004 5008 5012 5016 memory address

Base Address Dr. Pavithra L K, VIT, Chennai Campus


Array
• Arrays can be used to store linear data of similar types.
• Limitations:
– Size of the arrays is fixed:
Maximum size of the array is known in advance.
Allocated memory with the maximum size irrespective of
the usage.
– Insertion is expensive
creating a space for new elements by shifting of existing
elements in an array

Dr. Pavithra L K, VIT, Chennai Campus


Need for Linked List
int a[5] ;
In array, elements are not stored in contiguous manner.

a 4000 4004 4008 4012 4016 4020 4024 4027 memory address
5 6 13 10 12 Data/value/element

Not possible

int b[5]= {11,12,13}; Wastage of memory space

11 12 13
0 1 2 3 4
5000 5004 5008 5012 5016
Dr. Pavithra L K, VIT, Chennai Campus
List
• Linear Data Structure
• Size of the linked list data structure is modifiable
during the run time
• Sequential access
• Use memory space effectively (doesnot waste
memory space)
• It contains value/data/element and address to next
element
Node
Element/ Data/ Address to next
Value element
Dr. Pavithra L K, VIT, Chennai Campus
Linked List
• Elements are not stored at contiguous memory locations
• Elements in a linked list are linked using pointers as
shown in the below image
• Last element points to NULL
4000 4012 4024 memory address
4000 5 4012 6 4024 10 Null

Head Head Tail


Pointer Node Node

4000 4004 4008 4012 4016 4020 4024 4028 memory address
5 6 10 Data/value/element
Dr. Pavithra L K, VIT, Chennai Campus
Array Vs Linked List

• Arrays
• Inserting/deleting an element in ordered manner
• Supports random access
• Number of elements in the array are not
predefined
• Linked lists
• Inserting/deleting an element in Non-ordered
manner
• Sequential access
• Number of elements in the list are not predefined

Dr. Pavithra L K, VIT, Chennai Campus


Basic Operations on a List
• Creating a list
• Traversing the list
• Inserting an element in the list
• Deleting an element from the list
• Find an element in the list
• Modify an element from the list

Dr. Pavithra L K, VIT, Chennai Campus


Types of linked list
• Singly Linked List (SLL)
• Doubly Linked List (DLL)
• Circular Linked List (CLL)
• Circular Doubly Linked List (C-DLL)

Dr. Pavithra L K, VIT, Chennai Campus


Singly Linked List (SLL)
4000 4012 4024
4000 5 4012 6 4024 10 Null

Head Head
pointer Node

• Most commonly used LL


• Each node has two parts: Data and pointer
• It has single link to its next node
• Forward sequential movement is allowed

Dr. Pavithra L K, VIT, Chennai Campus


Circular Linked List (CLL)
4000 4012 4024
4000 5 4012 6 4024 10 4000

Head Head
pointer Node

• Each node has two parts: Data and pointer


• Pointer of the last node in the list points back to the first node.

Dr. Pavithra L K, VIT, Chennai Campus


Doubly linked list
• Each node has 3 parts
• Forward/backward movement are possible
• Each node contains 2 pointers and 1 data field
• 2 pointers
Pointer to previous node Pointer to next node

node
Previous Data Next_node_addr
_node_address ess
4000 4018 4030
4000 null 12 4018 4000 67 4030 4018 67 null

Head Head
Dr. Pavithra L K, VIT, Chennai Campus
pointer node
Circular Doubly Linked List (C-DLL)
4000 4018 4030
4000 4030 12 4018 4000 67 4030 4018 67 4000

Head
pointer

Each node has 3 parts


Forward/backward movement is possible
Each node contains 2 pointers and 1 data field
Previous pointer of the first node in the list points back to the last node in the list.
Next pointer of the last node in the list points back to the first node in the list.

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at beginning SLL
• 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 new Node→next=NULL and
head=new Node
• Step 4 -If it is Not Empty then, set newNode→ next=head
and head=newNode.

Dr. Pavithra L K, VIT, Chennai Campus


Insertion in SLL
• At beginning
• At end
• At particular location
At beginning
• Create a empty node (new node) and assign value to data field of the node
• Assign the new node to head
Node creation /SLL
struct node
{
int data;
struct node *next_adr; Insertion at beginning if
}; list is empty
struct node *head, *newnNode;
Allocate memory for the node 4000
newNode=(struct node*)malloc (size of (struct node)); newNode
Assign Value
newNode->data=30; newNode 30 null
newNode->next_adr=null;
head=newNode; Dr. Pavithra L K, VIT, Chennai Campus
head 4000 30 null
Insertion at beginning SLL
4032 newNode created at location 4032, and data 32 is inserted
32 4000

4032
Update the address field of the newNode
32 4000
1

4000 5 4012 6 4024 10 Null

4000 4012 4024

4032
Update the head pointer
32 4000
2 1

4032 5 4012 6 4024 10 Null

4000 4012 4024


Remove the link Dr. Pavithra L K, VIT, Chennai Campus
Insertion at beginning SLL
After insertion at beginning

4032 32 4000 5 4012 6 4024 10 Null

4032 4000 4012 4024

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at beginning SLL
struct node
{
int a;
struct node *next_adr;
};
struct node *head, *newnode, *temp;
newnode= (struct node*)malloc (size of (struct node));
newnode->next_adr=0;
if (head==0)
{
head =temp=new_node;
}
else
{
temp->next_adr=newnode;
temp=newnode;
}

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at End SLL

Step 1 -Create a newNode with given value and newNode→ next_adr as


NULL.
Step 2 -Check whether list is Empty(head==NULL).
Step 3 -If it is Empty then, set head=new Node.
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_adr is equal to NULL).
Step 6 –Set temp →next_adr=new Node.

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at End SLL
4032
32 null newNode created at location 4032, and data 32 is inserted

1. Check whether the head node second field is empty or not.


2. Create a node pointer as “temp”. Assign head node to that and check second field is
empty or not. (second field is not empty)
temp

4000 5 4012 6 4024 10 Null


3. if not move temp to next node and check second field is empty or not. (second field
is not empty) temp

4000 5 4012 6 4024 10 Null

4. if not move temp to next node and check second field is empty or not. (second field
is empty) temp

4000 5 4012 6 4024 10 Null


Dr. Pavithra L K, VIT, Chennai Campus
Insertion at End SLL
5. Second field of the temp pointer pointing node is not empty. (second
field is empty). Now insert the “newNode” in location next to this

temp 4032

4000 5 4012 6 4024 10 4032 32 null

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at end SLL
struct node
{
int a;
struct node *next_adr;
};
struct node *head, *newnode, *temp;
newnode= (struct node*)malloc (size of (struct node));
newnode->next_adr=0;
temp=head;
while (temp->next_adr!=0)
{
temp=temp->next_adr;
}
temp->next_adr=newnode;

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at particular location SLL
Step1 - Create a newNode with given value.
Step 2 -Check whether list is Empty(head==NULL)
Step 3 -If it is Empty then, set newNode→next_adr=NULL and head
=newNode.
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 node
after which we want to insert the newNode(until temp→data is equal to
location, here location is the node value after which we want to insert the
newNode).
Step 6 -Check whether temp is reached to last node or not. If it is reached to
last node then display Error message and terminate the function. Otherwise
move the temp to next node.
Step 7 -Finally, Set newNode→ next_adr=temp

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at particular location SLL
4032 newNode created at location 4032, and data 32 is inserted
32

1. Get the position detail where we want to insert the newNode.(position=2)


2. Check whether the head node second field is null or not. If not null go to step 3.
3. Create a node pointer as “temp”. Assign head node to that and check whether the
position of the temp pointing node is equal to the position detail given by the user.

temp
4000 5 4012 6 4024 10 Null
4. if it is not matched, move the temp to next location and check whether the position of
the temp pointing node is equal to the position detail given by the user.
temp
4000 5 4012 6 4024 10 Null
4032 Update newNode node pointer value to
32 4012 temp pointer pointing node address
temp
4000 5 4012 6 4024
Dr. Pavithra L K, VIT, Chennai Campus
10 Null
Insertion at particular location SLL
Update newNode node address to the second field of the node which is present
before the position given by the user
4032
32 4012
temp
4000 5 4032 6 4024 10 Null

Remove the existing link


4032
32 4012
temp
4000 5 4032 6 4024 10 Null

Linked list after insertion

4000 5 4032 32 4012 6 4024 10 Null

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at particular location SLL
Get position from the user and store it in the variable pos
int i, pos;
if (pos>count)
{printf(“error message”);
}
else
{
temp=head;
while(i<pos)
{
temp=temp->next_adr;
i++;}
newnode->next_adr=temp->next_adr;
temp->next_adr=newnode;

Dr. Pavithra L K, VIT, Chennai Campus


Deletion in SLL
• Deletion operation removes a specified node from the list

• Deletion taken place in the following places of the list.

• Beginning of the list

• Between two nodes in the list

• End of the list


Deletion in SLL- Beginning of the list
temp Assign head node to the temp pointer
4000 5 4032 32 4012 6 4024 10 Null

Update the
head pointer free(temp)
4032 5 4032 32 4012 6 4024 10 Null

List after to deletion

4032 32 4012 6 4024 10 Null

Dr. Pavithra L K, VIT, Chennai Campus


Deletion in SLL (at beginning)
struct node
{
int a;
struct node *next_adr;
};
struct node *head, *temp;
newnode= (struct node*) malloc(size of (struct node));
if( head==0)
{
list is empty;
}
else
{
temp=head;
head=head->next_adr;
free(temp);
}

Dr. Pavithra L K, VIT, Chennai Campus


Deletion
temp
in SLL- Beginning of the list
Prev node Assign head node to the temp and Prevnode pointer

4000 5 4032 32 4012 6 4024 10 Null

Prev node temp update temp pointer position

4000 5 4032 32 4012 6 4024 10 Null

Prev node temp update temp pointer position


4000 5 4032 32 4012 6 4024 10 Null

update prev_node pointing node second field as Prev node free(temp)


null
4000 5 4032 32 4012 6 null 10 Null

4000 5 4032 32 4012 6 null


Dr. Pavithra L K, VIT, Chennai Campus
Deletion in SLL (at end)
struct node
{
int a;
struct node *next_adr;
};
struct node *head, *prevnode, *temp;
temp=head;
while (temp->next_adr!=0)
{
prevnode=temp;
temp=temp->next_adr;
}
if (temp==head)
{head=0; free(temp);}
else
{
prenode->next_adr=0;
}
free(temp);
}
Dr. Pavithra L K, VIT, Chennai Campus
Deletion in SLL- End of the list
temp Prevnode Assign head node to the temp and Prevnode pointer

4000 5 4032 32 4012 6 4024 10 Null

Prevnode temp update temp pointer position

4000 5 4032 32 4012 6 4024 10 Null

Prevnode temp update temp pointer position

4000 5 4032 32 4012 6 4024 10 Null

update prev_node pointing node second field as null free(temp)


Prevnode

4000 5 4032 32 4012 6 null 10 Null

4000 5 4032 32 4012 6 null


Dr. Pavithra L K, VIT, Chennai Campus
Deletion in SLL (at particular
location)
struct node
{
int a;
struct node *next_adr;
};
struct node *head, *nextnode, *temp;
int pos; i=1;
temp=head;
while (i<pos-1)
{
temp=temp->next_adr;
i++;
}
nextnode=temp->next_adr;
temp->next_adr=nextnode->next_adr;
free(nextnode);

Dr. Pavithra L K, VIT, Chennai Campus


Deletion in SLL- End of
Assign head node to the temp; Check whether it reached pos-1;
the list
temp Pos=3
i=1

4000 5 4032 32 4012 6 4024 10 Null

i=2 temp update temp pointer position; Check


whether it reached pos-1;
4000 5 4032 32 4012 6 4024 10 Null

Check whether it reached pos-1; if reached pos-1 assign the second field of pos-1 node
to nextnode.(i.e: nextnode= temp->next_adr ).
temp nextnode
4000 5 4032 32 4012 6 4024 10 Null
update temp pointing node’s second field with nextnode’s second field and free the
nextnode. temp free(nextnode)
4000 5 4032 32 4024 6 4024 10 Null

4000 5 4032 32 4024 10 null


Dr. Pavithra L K, VIT, Chennai Campus
Doubly linked list
struct node
{
int data;
struct node *next;
struct node *prev;
};*head; *tail;
head=null; tail=null;

4000 null 12 4018 4000 67 null


4000 4018

Head Head Dr. Pavithra L K, VIT, Chennai Campus


pointer node
Basic operations
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Insert Last − Adds an element at the end of the list.
Delete Last − Deletes an element from the end of the list.
Insert After − Adds an element after an item of the list.
Delete − Deletes an element from the list using the key.
Display forward − Displays the complete list in a forward
manner.
Display backward − Displays the complete list in a backward
manner.

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at Doubly linked list
node *temp; node *temp;
temp = (struct node*) malloc (size of temp = (struct node*) malloc (size of
(struct node)); (struct node));
temp->data = d; temp->data = d;
temp->prev = NULL; temp->next = pos;
temp->next = head; temp->prev = pos->prev;
// List is empty pos->prev = temp;
if(head == NULL) //if node is to be inserted before first
tail = temp; node
else if(pos->prev == NULL)
head->prev = temp; head = temp;
head = temp;

Insert Data in the beginning Insert Data before a Node

Dr. Pavithra L K, VIT, Chennai Campus


Insertion at Doubly linked list
node *temp; node *temp;
temp = (struct node*)malloc (size of temp = (struct node*)malloc (size of (struct
(struct node)); node));
temp->data = d; temp->data = d;
temp->prev = pos; temp->prev = tail;
temp->next = pos->next; temp->next = NULL;
pos->next = temp; // if list is empty
//if node is to be inserted after last node if(tail == NULL)
if(pos->next == NULL) head = temp;
tail = temp; else
tail->next = temp;
Insert Data after a Node tail = temp;

Insert Data in the end

Dr. Pavithra L K, VIT, Chennai Campus


Deletion at Doubly linked list
if(pos->prev == NULL)//if node to be deleted is first node of list
{
head = pos->next; //the next node will be front of list
head->prev = NULL;
}
else if(pos->next == NULL) //if node to be deleted is last node of list
{
tail = pos->prev; // the previous node will be last of list
tail->next = NULL;
}
else
{ //previous node's next will point to current node's next
pos->prev->next = pos->next;
//next node's prev will point to current node's prev
pos->next->prev = pos->prev;
}
free(n); //delete node

Dr. Pavithra L K, VIT, Chennai Campus


Basic operations of DLL
node *trav; node *trav;
trav = front; trav = end;
while(trav != NULL) while(trav != NULL)
{ {
printf(“%d”,trav->data); printf(“%d”trav->data);
trav = trav->next; trav = trav->prev;
} }
Display forward Display backward

Dr. Pavithra L K, VIT, Chennai Campus


Doubly linked list Insertion
temp 4044
Insert Data before and after a Node
48

4000 null 12 4018 4000 67 null


4000 4018

4044
Head Update temp node’s prev pointer 4000 48
pointer

4000 null 12 4018 4000 67 null

Head 4000 4018


pointer
Dr. Pavithra L K, VIT, Chennai Campus
Doubly linked list Insertion
4044
Update temp node’s next pointer
4000 48 4018

4000 null 12 4018 4000 67 null

Head 4000 4018


pointer
4044
Update pos node’s next pointer
4000 48 4018

4000 null 12 4044 4000 67 null


Head 4000 4018
pointer

Dr. Pavithra L K, VIT, Chennai Campus


Doubly linked list Insertion
Update (pos+1) node’s prev pointer as temp address 4044
4000 48 4018

4000 null 12 4044 4044 67 null


4000 4018
Head
pointer
4000 null 12 4044 4000 48 4018 4044 67 null

4000 4044 4018

Head Head Tail


pointer

Dr. Pavithra L K, VIT, Chennai Campus


Circular Doubly linked list
Previous_node_ Data Next_node_add
address ress

4000 4018 12 4018 4000 67 4000

Head
pointer

A circular linked list is a sequence of elements in which every element has a link to its
next element in the sequence and the last element has a link to the first element.

Dr. Pavithra L K, VIT, Chennai Campus


Circular linked list- basic operations

Insertion
• At beginning
• At end
• At particular location

Deletion

• Beginning of the list


• Between two nodes in the list
• End of the list

Display

Dr. Pavithra L K, VIT, Chennai Campus


Circular linked list insertion beginning
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
if(head == NULL)
{
head = newNode;
newNode -> next = head;
}
else
{
struct Node *temp = head;
while(temp -> next != head)
temp = temp -> next;
newNode -> next = head;
head = newNode;
temp -> next = head;
}

Dr. Pavithra L K, VIT, Chennai Campus


Circular linked list insertion At end
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
if(head == NULL)
{
head = newNode;
newNode -> next = head;
}
else
{
struct Node *temp = head;
while(temp -> next != head)
temp = temp -> next;
temp -> next = newNode;
newNode -> next = head;
}

Dr. Pavithra L K, VIT, Chennai Campus


Circular linked list insertion At particular
location
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct else
Node)); {
newNode -> data = value; temp = temp -> next;
if(head == NULL) }
{ }
head = newNode; newNode -> next = temp -> next;
newNode -> next = head; temp -> next = newNode;
} printf("\nInsertion success!!!");
else }
{
struct Node *temp = head;
while(temp -> data != location)
{
if(temp -> next == head)
{
printf("Given node is not found in the list!!!");
goto EndFunction;
} Dr. Pavithra L K, VIT, Chennai Campus
Circular linked list deletion At beginning
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct Node *temp = head;
if(temp -> next == head)
{
head = NULL;
free(temp);
}
else{
head = head -> next;
free(temp);
}
printf("\n Deletion success");
}

Dr. Pavithra L K, VIT, Chennai Campus


Circular linked list deletion At End
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct Node *temp1 = head, temp2;
if(temp1 -> next == head)
{
head = NULL;
free(temp1);
}
else{
while(temp1 -> next != head){
temp2 = temp1;
temp1 = temp1 -> next;
}
temp2 -> next = head;
free(temp1);
}
printf("\nDeletion success!!!");
Dr. Pavithra L K, VIT, Chennai Campus
Circular linked list deletion At particular
else{
location if(temp1 == head)
{
if(head == NULL) temp2 = head;
printf("List is Empty!!! Deletion not possible!!!"); while(temp2 -> next != head)
else temp2 = temp2 -> next;
{ head = head -> next;
struct Node *temp1 = head, temp2; temp2 -> next = head;
while(temp1 -> data != delValue) free(temp1);
{ }
if(temp1 -> next == head) else
{ {
printf("\nGiven node is not found in the list!!!"); if(temp1 -> next == head)
goto FuctionEnd; {
} temp2 -> next = head;
else }
{ else
temp2 = temp1; {
temp1 = temp1 -> next; temp2 -> next = temp1 -> next;
} }
} free(temp1);
if(temp1 -> next == head){ }
head = NULL; }
free(temp1); printf("\nDeletion success!!!");
} }

Dr. Pavithra L K, VIT, Chennai Campus


Circular linked list display
if(head == NULL)
printf("\nList is Empty!!!");
else
{
struct Node *temp = head;
printf("\nList elements are: \n");
while(temp -> next != head)
{
printf("%d ---> ",temp -> data);
}
printf("%d ---> %d", temp -> data, head -> data);
}

Dr. Pavithra L K, VIT, Chennai Campus


Implement stack using LL
push(value) -Inserting an element 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 newNode
→next_adr = NULL
• Step 4 -If it is Not Empty, then set newNode
→next_adr = top
• Step 5 –Finally, set top = newNode

Dr. Pavithra L K, VIT, Chennai Campus


pop() -Deleting an Element from a
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!!!" and
terminate the function
• Step 3 -If it is Not Empty, then define a Node
pointer 'temp' and set it to 'top'.
• Step 4 -Then set 'top = top →next'.
• Step 5 -Finally, delete 'temp'. (free(temp)).
Dr. Pavithra L K, VIT, Chennai Campus
display() -Displaying stack of
elements
display() -Displaying stack of elements
• 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'.

Dr. Pavithra L K, VIT, Chennai Campus


push(value) -Inserting an element
into the Stack
void push(int value) //Push data
{
Struct Node *newNode;
newNode = (structNode*) malloc(sizeof(structNode));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
{
newNode->next = top;
}
top = new Node;
printf("\nInsertion is Success!!!\n");
}

Dr. Pavithra L K, VIT, Chennai Campus


pop() -Deleting an Element from a
Stack
void pop() //Pop data
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeletedelement: %d", temp->data);
top = temp->next;
free(temp);
}
}

Dr. Pavithra L K, VIT, Chennai Campus


display() -Displaying stack of
elements
void display() //Display elements
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}

Dr. Pavithra L K, VIT, Chennai Campus


enQueue(value) -Inserting an
element into the Queue
void insert(int value) //enqueue
{
struct Node *newNode;
newNode = (struct Node*)malloc (sizeof (struct
Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\n Insertion is Success!!!\n");
}
Dr. Pavithra L K, VIT, Chennai Campus
deQueue(value) - deleting an
element from the Queue
void delete() //dequeue
{
if(front == NULL)
printf("\n Queue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}

Dr. Pavithra L K, VIT, Chennai Campus


Display queue elements
void display() //Display queue
{
if(front == NULL)
printf("\n Queue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n", temp->data);
}
}

Dr. Pavithra L K, VIT, Chennai Campus


Polynomial Addition
• Polynomial contains: coefficient and exponent
• Create a polynomial
struct node
{
int cof, exp;
struct node *next;
}*new, *head1, *temp1, *new2, *head2,
*temp2;

Dr. Pavithra L K, VIT, Chennai Campus


Polynomial Addition
• Create a polynomial
• Compare exponent of the two linked list
• If matched add the coefficient in that two
nodes
• Else print coefficient as it is and move the
temp to next node and go to step2 until it reach
end of the linked list

Dr. Pavithra L K, VIT, Chennai Campus


Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1

6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032

head2 temp2

5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1

6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032

5𝑋 5

head2 temp2

5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1

6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032

5𝑋 5 + 6𝑋 4 + 15𝑋 3

head2 temp2

5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1

6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032

5𝑋 5 + 6𝑋 4

head2 temp2

5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1

6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032

5𝑋 5 + 6𝑋 4 + 15𝑋 3 + 2𝑋 2+3𝑋 1 + 8

head2 temp2

5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Linked list intersection
• Consider two linked list L1 and L2
• Initialize result list as NULL. Traverse list1 (L1) and look for its each element in
list2 (L2), if the element is present in list2, then add the element to result.

• L1
4000 temp1 compare the temp1 data and temp2 data. If not
increment temp2 new location
6 4045 7 4027 3 4032 5 null
4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

Dr. Pavithra L K, VIT, Chennai Campus


Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.

• L1
4000 temp1

6 4045 7 4027 3 4032 5 null


4000 4045 4027 4032
• L2
5000 temp2

16 5045 71 5021 3 5050 51 null


5000 5045 5021 5032

• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
Linked list intersection output

5021 3 null

Dr. Pavithra L K, VIT, Chennai Campus


Linked list
• Analyze the time complexity insertion and
deletion operations of the SLL, DLL, CLL,
and C-DLL

Dr. Pavithra L K, VIT, Chennai Campus


Thank You

Dr. Pavithra L K, VIT, Chennai Campus

You might also like