2. LINKED LIST
• Like arrays, Linked List is a linear data structure. Unlike
arrays, linked list elements are not stored at a contiguous
location; the elements are linked using pointers
3. Why Linked List?
Arrays can be used to store linear data of similar types, but
arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper
limit on the number of elements in advance. Also, generally,
the allocated memory is equal to the upper limit irrespective of
the usage.
2) Inserting a new element in an array of elements is expensive
because the room has to be created for the new elements and
to create room existing elements have to be shifted.
4. Cont…
• For example, in a system, if we maintain a sorted list of IDs
in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
• And if we want to insert a new ID 1005, then to maintain the
sorted order, we have to move all the elements after 1000
(excluding 1000).
Deletion is also expensive with arrays until unless some
special techniques are used. For example, to delete 1010 in
id[], everything after 1010 has to be moved.
5. Cont…
• Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
• Drawbacks:
1) Random access is not allowed. We have to access elements
sequentially starting from the first node. So we cannot do binary
search with linked lists efficiently with its default implementation.
2) Extra memory space for a pointer is required with each element
of the list.
3) Not cache friendly. Since array elements are contiguous
locations, there is locality of reference which is not there in case of
linked lists.
6. Representation:
A linked list is represented by a pointer to the first node of the linked list. The
first node is called the head. If the linked list is empty, then the value of the
head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
In C, we can represent a node using structures. Below is an example of a linked
list node with integer data.
In Java or C#, LinkedList can be represented as a class and a Node as a
separate class. The LinkedList class contains a reference of Node class type.
9. TYPES OF LINKED LIST
• Single linked list
• Doubly linked list
• Circular linked list
• Circular Doubly linked list
10. Linked List Traversal
The idea here is to step through the list from beginning to end. For example, we may
want to print the list or search for a specific node in the list.
The algorithm for traversing a list Start with the head of the list. Access the content of the
head node if it is not null.
Then go to the next node(if exists) and access the node information
Continue until no more nodes (that is, you have reached the null node)
void traverseLL(Node head)
{
while(head != NULL)
{
printf(head->data) ;
head = head->next;
}
}
11. Single linked list
• A singly linked list is a type of linked list that is unidirectional,
that is, it can be traversed in only one direction from head to
the last node (tail).
• Each element in a linked list is called a node. A single node
contains data and a pointer to the next node which helps in
maintaining the structure of the list.
• The first node is called the head; it points to the first node of
the list and helps us access every other element in the list.
The last node, also sometimes called the tail, points
to NULL which helps us in determining when the list ends.
12. Creation of single linked list
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
void create(int);
void display(void);
struct node *start = NULL, *curr, *prev;
13. Cont…
void main()
{
int ch,item;
clrscr();
do
{
printf("n 1-> create n 2->display n 3-> exit");
printf("Enter your choice");
scanf("%d ",&ch);
switch(ch)
{
case 1:
printf("Enter the item to create");
scanf("%d ",&item);
create(item);
break;
case 2:
display();
break;
case 3:
exit(0);
17. Insertion
• The insertion into a singly linked list can be performed at different
positions. Based on the position of the new node being inserted, the
insertion is categorized into the following categories.
SN Operation Description
1 Insertion at beginning It involves inserting any element at the front of the list. We just need to a few
link adjustments to make the new node as the head of the list.
2 Insertion at end of the
list
It involves insertion at the last of the linked list. The new node can be inserted
as the only node in the list or it can be inserted as the last one. Different logics
are implemented in each scenario.
3 Insertion after specifie
d node
It involves insertion after the specified node of the linked list. We need to skip
the desired number of nodes in order to reach the node after which the new
node will be inserted. .
18. Insertion in singly linked list at beginning
• Inserting a new element into a singly linked list at beginning is quite simple. We just
need to make a few adjustments in the node links. There are the following steps
which need to be followed in order to insert a new node in the list at beginning.
• Allocate the space for the new node and store data into the data part of the node.
This will be done by the following statements.
ptr = (struct node *) malloc(sizeof(struct node *));
ptr → data = item
• Make the link part of the new node pointing to the existing first node of the list.
This will be done by using the following statement.
ptr->next = head;
• At the last, we need to make the new node as the first node of the list this will be
done by using the following statement.
head = ptr;
19. Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR NEXT
→
Step 4: SET NEW_NODE DATA = VAL
→
Step 5: SET NEW_NODE NEXT = HEAD
→
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
21. Insertion in singly linked list at the end
• In order to insert a node at the last, there are two following scenarios
which need to be mentioned.
1.
The node is being added to an empty list
2.
The node is being added to the end of the linked list
in the first case,
• The condition (head == NULL) gets satisfied. Hence, we just need to
allocate the space for the node by using malloc statement in C. Data and
the link part of the node are set up by using the following statements.
ptr->data = item;
ptr -> next = NULL;
• Since, ptr is the only node that will be inserted in the list hence, we
need to make this node pointed by the head pointer of the list. This will
be done by using the following Statements.
Head = ptr
22. Cont…
• In the second case,
• The condition Head = NULL would fail, since Head is not null. Now, we need to declare a
temporary pointer temp in order to traverse through the list. temp is made to point the
first node of the list.
Temp = head
• Then, traverse through the entire linked list using the statements:
while (temp next != NULL)
→
temp = temp next;
→
• At the end of the loop, the temp will be pointing to the last node of the list. Now, allocate
the space for the new node, and assign the item to its data part. Since, the new node is
going to be the last node of the list hence, the next part of this node needs to be pointing
to the null. We need to make the next part of the temp node (which is currently the last
node of the list) point to the new node (ptr) .
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
23. Algorithm
Step 1: IF PTR = NULL Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL
Step 5: SET NEW_NODE - > NEXT = NULL
Step 6: SET PTR = HEAD
Step 7: Repeat Step 8 while PTR - > NEXT != NULL
Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
Step 9: SET PTR - > NEXT = NEW_NODE
Step 10: EXIT
25. Insertion in singly linked list after specified Node
• In order to insert an element after the specified number of nodes into the linked list, we
need to skip the desired number of elements in the list to move the pointer at the
position after which the node will be inserted. This will be done by using the following
statements.
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
return;
}
}
• Allocate the space for the new node and add the item to the data part of it. This will be
done by using the following statements.
ptr = (struct node *) malloc (sizeof(struct node));
ptr->data = item;
26. Cont…
• Now, we just need to make a few more link adjustments and our node
at will be inserted at the specified position. Since, at the end of the
loop, the loop pointer temp would be pointing to the node after which
the new node will be inserted. Therefore, the next part of the new node
ptr must contain the address of the next part of the temp (since, ptr
will be in between temp and the next of the temp). This will be done by
using the following statements.
ptr next = temp next
→ →
• now, we just need to make the next part of the temp, point to the new
node ptr. This will insert the new node ptr, at the specified position.
temp ->next = ptr;
27. Algorithm
STEP 1: IF PTR = NULL
WRITE OVERFLOW
GOTO STEP 12
END OF IF
STEP 2: SET NEW_NODE = PTR
STEP 3: NEW_NODE DATA = VAL
→
STEP 4: SET TEMP = HEAD
STEP 5: SET I = 0
STEP 6: REPEAT STEP 5 AND 6 UNTIL I<loc< li=""></loc<>
STEP 7: TEMP = TEMP NEXT
→
STEP 8: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
END OF LOOP
STEP 9: PTR NEXT = TEMP NEXT
→ →
STEP 10: TEMP NEXT = PTR
→
STEP 11: SET PTR = NEW_NODE
STEP 12: EXIT
29. Assignment / lab experiment
Program:
WAP to insert a new element in to the single linked list at the following
position.
1. At the beginning of the list
2. At the end of the list
3. After a specified position of the list
30. Deletion
• The Deletion of a node from a singly linked list can be performed at different
positions. Based on the position of the node being deleted, the operation is
categorized into the following categories.
SN Operation Description
1 Deletion at beginning It involves deletion of a node from the beginning of the list. This is the
simplest operation among all. It just need a few adjustments in the
node pointers.
2 Deletion at the end of the list It involves deleting the last node of the list. The list can either be
empty or full. Different logic is implemented for the different scenarios.
3 Deletion after specified node It involves deleting the node after the specified node in the list. we
need to skip the desired number of nodes to reach the node after
which the node will be deleted. This requires traversing through the
list.
31. Deletion in singly linked list at beginning
• Deleting a node from the beginning of the list is the simplest operation of all. It just
need a few adjustments in the node pointers. Since the first node of the list is to be
deleted, therefore, we just need to make the head, point to the next of the head. This
will be done by using the following statements.
ptr = head;
head = ptr->next;
• Now, free the pointer ptr which was pointing to the
head node of the list. This will be done by using the
following statement.
free(ptr)
32. Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT
34. Deletion in singly linked list at the end
• There are two scenarios in which, a node is deleted from the
end of the linked list.
1.There is only one node in the list and that needs to be deleted.
2.There are more than one node in the list and the last node of
the list will be deleted.
In the first scenario,
• the condition head next = NULL will survive and therefore,
→
the only node head of the list will be assigned to null. This will
be done by using the following statements.
ptr = head
head = NULL
free(ptr)
35. Cont…
In the second scenario,
• The condition head next = NULL would fail and therefore, we have to traverse the node in order to reach the last node
→
of the list.
• For this purpose, just declare a temporary pointer temp and assign it to head of the
list. We also need to keep track of the second last node of the list. For this purpose,
two pointers ptr and ptr1 will be used where ptr will point to the last node and ptr1
will point to the second last node of the list.
• this all will be done by using the following statements.
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
• Now, we just need to make the pointer ptr1 point to the NULL and the last node of the
list that is pointed by ptr will become free. It will be done by using the following
statements.
ptr1->next = NULL;
free(ptr);
36. Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
38. Deletion in singly linked list after the specified node :
• In order to delete the node, which is present after the specified node, we need to skip
the desired number of nodes to reach the node after which the node will be deleted. We
need to keep track of the two nodes. The one which is to be deleted the other one if the
node which is present before that node. For this purpose, two pointers are used: ptr
and ptr1.
• Use the following statements to do so.
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("nThere are less than %d elements in the list..",loc);
return;
}
}
39. Cont…
• Now, our task is almost done, we just need to make a
few pointer adjustments. Make the next of ptr1 (points
to the specified node) point to the next of ptr (the node
which is to be deleted).
• This will be done by using the following statements.
ptr1 ->next = ptr ->next;
free(ptr);
40. Algorithm
• STEP 1: IF HEAD = NULL
• WRITE UNDERFLOW
GOTO STEP 10
END OF IF
• STEP 2: SET TEMP = HEAD
• STEP 3: SET I = 0
• STEP 4: REPEAT STEP 5 TO 8 UNTIL I<loc< li=""></loc<>
• STEP 5: TEMP1 = TEMP
• STEP 6: TEMP = TEMP NEXT
→
• STEP 7: IF TEMP = NULL
• WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
• STEP 8: I = I+1
• END OF LOOP
• STEP 9: TEMP1 NEXT = TEMP NEXT
→ →
• STEP 10: FREE TEMP
• STEP 11: EXIT
43. Assignment / lab experiment
Program :
WAP to delete an element from a single linked list at the following position.
1. From the beginning of the list
2. From the end of the list
3. After a specified position of the list
Program:
4. WAP to Implement stack using linked list
5. WAP to Implement queue using linked list
44. Doubly linked list
• Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence. Therefore, in
a doubly linked list, a node consists of three parts: node data, pointer to the
next node in sequence (next pointer) , pointer to the previous node (previous
pointer). A sample node in a doubly linked list is shown in the figure.
• A doubly linked list containing three nodes having numbers from 1 to 3 in their data
part, is shown in the following image.
45. Cont.…
• In C, structure of a node in doubly linked list can be given as :
struct node
{
struct node *prev;
int data;
struct node *next;
};
• The prev part of the first node and the next part of the last node will always
contain null indicating end in each direction.
• In a singly linked list, we could traverse only in one direction, because each
node contains address of the next node and it doesn't have any record of its
previous nodes. However, doubly linked list overcome this limitation of singly
linked list. Due to the fact that, each node of the list contains the address of its
previous node, we can find all the details about the previous node as well by
using the previous address stored inside the previous part of each node.
47. Operations on doubly linked list
SN Operation Description
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after specified node Adding the node into the linked list after the specified node.
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node having g
iven data
Removing the node which is present just after the node containing the given
data.
7 Searching Comparing each node data with the item to be searched and return the location
of the item in the list if the item found else return null.
8 Traversing Visiting each node of the list at least once in order to perform some specific
operation like searching, sorting, display, etc.
48. Insertion in doubly linked list at beginning
• As in doubly linked list, each node of the list contain double pointers
therefore we have to maintain more number of pointers in doubly linked
list as compare to singly linked list.
• There are two scenarios of inserting any element into doubly linked list.
Either the list is empty or it contains at least one element. Perform the
following steps to insert a node in doubly linked list at beginning.
• Allocate the space for the new node in the memory. This will be done by
using the following statement.
ptr = (struct node *)malloc(sizeof(struct node));
• Check whether the list is empty or not. The list is empty if the condition head ==
NULL holds. In that case, the node will be inserted as the only node of the list
and therefore the prev and the next pointer of the node will point to NULL and
the head pointer will point to this node.
49. Cont…
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
• In the second scenario, the condition head == NULL become false and the
node will be inserted in beginning. The next pointer of the node will point to
the existing head pointer of the node. The prev pointer of the existing head
will point to the new node being inserted.
• This will be done by using the following statements.
ptr->next = head;
head prev=ptr;
→
• Since, the node being inserted is the first node of the list and therefore it
must contain NULL in its prev pointer. Hence assign null to its previous part
and make the head point to this node.
ptr prev =NULL
→
head = ptr
50. Algorithm :
Step 1: IF ptr = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Step 2: SET NEW_NODE = ptr
Step 3: SET ptr = ptr -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> PREV = NULL
Step 6: SET NEW_NODE -> NEXT = START
Step 7: SET head -> PREV = NEW_NODE
Step 8: SET head = NEW_NODE
Step 9: EXIT
52. Insertion in doubly linked list at the
end
• In order to insert a node in doubly linked list at the end, we must make
sure whether the list is empty or it contains any element. Use the
following steps in order to insert the node in doubly linked list at the end.
• Allocate the memory for the new node. Make the pointer ptr point to the
new node being inserted.
ptr = (struct node *) malloc(sizeof(struct node));
• Check whether the list is empty or not. The list is empty if the
condition head == NULL holds. In that case, the node will be inserted as
the only node of the list and therefore the prev and the next pointer of
the node will point to NULL and the head pointer will point to this node.
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
53. Cont…
• In the second scenario, the condition head == NULL become false. The new node will
be inserted as the last node of the list. For this purpose, we have to traverse the
whole list in order to reach the last node of the list. Initialize the pointer temp to head
and traverse the list by using this pointer.
Temp = head;
while (temp != NULL)
{
temp = temp next;
→
}
• the pointer temp point to the last node at the end of this while loop. Now, we just
need to make a few pointer adjustments to insert the new node ptr to the list. First,
make the next pointer of temp point to the new node being inserted i.e. ptr.
temp next =ptr;
→
• make the previous pointer of the node ptr point to the existing last node of the list i.e.
temp.
ptr prev = temp;
→
• make the next pointer of the node ptr point to the null as it will be the new last node
of the list.
ptr next = NULL
→
54. Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET TEMP = START
Step 7: Repeat Step 8 while TEMP -> NEXT != NULL
Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 9: SET TEMP -> NEXT = NEW_NODE
Step 10C: SET NEW_NODE -> PREV = TEMP
Step 11: EXIT
56. Insertion in doubly linked list after Specified node
• In order to insert a node after the specified node in the list, we need to skip the
required number of nodes in order to reach the mentioned node and then make the
pointer adjustments as required.
• Use the following steps for this purpose.
• Allocate the memory for the new node. Use the following statements for this.
ptr = (struct node *)malloc(sizeof(struct node));
• Traverse the list by using the pointer temp to skip the required number of nodes in
order to reach the specified node.
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL) // the temp will be //null if the list doesn't last long //
up to mentioned location
{
return;
}
}
58. Algorithm
•Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 15
[END OF IF]
•Step 2: SET NEW_NODE = PTR
•Step 3: SET PTR = PTR -> NEXT
•Step 4: SET NEW_NODE -> DATA = VAL
•Step 5: SET TEMP = START
•Step 6: SET I = 0
•Step 7: REPEAT 8 to 10 until I<="" li="">
•Step 8: SET TEMP = TEMP -> NEXT
•STEP 9: IF TEMP = NULL
•STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
GOTO STEP 15
[END OF IF]
[END OF LOOP]
•Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT
•Step 12: SET NEW_NODE -> PREV = TEMP
•Step 13 : SET TEMP -> NEXT = NEW_NODE
•Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE
•Step 15: EXIT
60. Algorithm
•STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
•STEP 2: SET PTR = HEAD
•STEP 3: SET HEAD = HEAD NEXT
→
•STEP 4: SET HEAD PREV = NULL
→
•STEP 5: FREE PTR
•STEP 6: EXIT
62. ALGORITHM
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET TEMP = HEAD
Step 3: REPEAT STEP 4 WHILE TEMP->NEXT !=
NULL
Step 4: SET TEMP = TEMP->NEXT
[END OF LOOP]
Step 5: SET TEMP ->PREV-> NEXT = NULL
Step 6: FREE TEMP
Step 7: EXIT
64. Algorithm
•Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
•Step 2: SET TEMP = HEAD
•Step 3: Repeat Step 4 while TEMP -> DATA != ITEM
•Step 4: SET TEMP = TEMP -> NEXT
[END OF LOOP]
•Step 5: SET PTR = TEMP -> NEXT
•Step 6: SET TEMP -> NEXT = PTR -> NEXT
•Step 7: SET PTR -> NEXT -> PREV = TEMP
•Step 8: FREE PTR
•Step 9: EXIT
65. Circular Singly Linked List
In a circular Singly linked list, the last node of the list contains a pointer to the first node of the
list. We can have circular singly linked list as well as circular doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started. The
circular singly liked list has no beginning and no ending. There is no null value present in the
next part of any of the nodes.
66. Insertion into circular singly linked list at beginning
There are two scenario in which a node can be inserted in circular singly linked list at beginning. Either
the node will be inserted in an empty list or the node is to be inserted in an already filled list.
67. Algorithm
•Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
•Step 2: SET NEW_NODE = PTR
•Step 3: SET PTR = PTR -> NEXT
•Step 4: SET NEW_NODE -> DATA = VAL
•Step 5: SET TEMP = HEAD
•Step 6: Repeat Step 8 while TEMP -> NEXT != HEAD
•Step 7: SET TEMP = TEMP -> NEXT
[END OF LOOP]
•Step 8: SET NEW_NODE -> NEXT = HEAD
•Step 9: SET TEMP NEXT = NEW_NODE
→
•Step 10: SET HEAD = NEW_NODE
•Step 11: EXIT
69. Algorithm
•Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]
•Step 2: SET NEW_NODE = PTR
•Step 3: SET PTR = PTR -> NEXT
•Step 4: SET NEW_NODE -> DATA = VAL
•Step 5: SET NEW_NODE -> NEXT = HEAD
•Step 6: SET TEMP = HEAD
•Step 7: Repeat Step 8 while TEMP -> NEXT !=
HEAD
•Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
•Step 9: SET TEMP -> NEXT = NEW_NODE
•Step 10: EXIT
71. Algorithm
•Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
•Step 2: SET PTR = HEAD
•Step 3: Repeat Step 4 while PTR NEXT !=
→
HEAD
•Step 4: SET PTR = PTR next
→
[END OF LOOP]
•Step 5: SET PTR NEXT = HEAD NEXT
→ →
•Step 6: FREE HEAD
•Step 7: SET HEAD = PTR NEXT
→
•Step 8: EXIT
73. Algorithm
•Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
•Step 2: SET PTR = HEAD
•Step 3: Repeat Steps 4 and 5 while PTR ->
NEXT != HEAD
•Step 4: SET PREPTR = PTR
•Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
•Step 6: SET PREPTR -> NEXT = HEAD
•Step 7: FREE PTR
•Step 8: EXIT
74. Circular Doubly Linked List
Circular doubly linked list is a more complexed type of data structure in which a node contain
pointers to its previous node as well as the next node. Circular doubly linked list doesn't contain
NULL in any of the node. The last node of the list contains the address of the first node of the list.
The first node of the list also contain address of the last node in its previous pointer.
76. Algorithm
•Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 13
[END OF IF]
•Step 2: SET NEW_NODE = PTR
•Step 3: SET PTR = PTR -> NEXT
•Step 4: SET NEW_NODE -> DATA = VAL
•Step 5: SET TEMP = HEAD
•Step 6: Repeat Step 7 while TEMP -> NEXT !=
HEAD
•Step 7: SET TEMP = TEMP -> NEXT
[END OF LOOP]
•Step 8: SET TEMP -> NEXT = NEW_NODE
•Step 9: SET NEW_NODE -> PREV = TEMP
•Step 1 : SET NEW_NODE -> NEXT = HEAD
•Step 11: SET HEAD -> PREV = NEW_NODE
•Step 12: SET HEAD = NEW_NODE
•Step 13: EXIT
78. Algorithm
•Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 12
[END OF IF]
•Step 2: SET NEW_NODE = PTR
•Step 3: SET PTR = PTR -> NEXT
•Step 4: SET NEW_NODE -> DATA = VAL
•Step 5: SET NEW_NODE -> NEXT = HEAD
•Step 6: SET TEMP = HEAD
•Step 7: Repeat Step 8 while TEMP -> NEXT !=
HEAD
•Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
•Step 9: SET TEMP -> NEXT = NEW_NODE
•Step 10: SET NEW_NODE -> PREV = TEMP
•Step 11: SET HEAD -> PREV = NEW_NODE
•Step 12: EXIT
79. Polynomial Representation using Linked List
A Polynomial has mainly two fields. exponent and
coefficient.
Node of a Polynomial:
For example 3x^2 + 5x + 7 will represent as follows.
In each node the exponent field will store the corresponding exponent and the coefficient
field will store the corresponding coefficient. Link field points to the next item in the
polynomial.
81. Adding two polynomials using
Linked List
Given two polynomial numbers represented by a linked list. Write a function that
add these lists means add the coefficients who have same variable powers.
Example:
Input:
1st number = 5x^2 + 4x^1 + 2x^0
2nd number = 5x^1 + 5x^0
Output: 5x^2 + 9x^1 + 7x^0
Input:
1st number = 5x^3 + 4x^2 + 2x^0
2nd number = 5x^1 + 5x^0
Output: 5x^3 + 4x^2 + 5x^1 + 7x^0