Unit IV_Linked List
Unit IV_Linked List
• Circular Linked List, Doubly Linked List , Generalized Linked List (GLL)
DATA STRUCTURE
LINEAR
NONLINEAR
ARRAY
LINKLIST STACK
QUEUE TREES & GRAPH
Linked List ADT
structure Linked List (item)
declare CREATE() -> Linked list
insert(item, linked list) -> linked list
delete(linked list) -> linked list
ISEMPS(linked list) -> boolean;
for all L ∈ Linked list, i ∈ item let
ISEMPS(CREATE) ::= true
ISEMPS(insert(i,L)) ::= false
delete(CREATE) ::= error
delete(insert(I,L)) ::= L
end Linked List
Introduction to linked list
• Representation of simple data structures using an array and a sequential mapping
are studied.
• These representations had the property that successive nodes of the data object
were stored fixed distance apart.
• If the element aij of a table was stored at location Lij, then a i, j+1 was at the location
L ij + c for some constant c;
(BAT, CAT, EAT, FAT, HAT, JAT, LAT, MAT, OAT, PAT, RAT, SAT, TAT, VAT,
WAT)
Pointer to the
first node
myList
a b c d
• Let L ={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}
• L is an ordered set
• Syntax of malloc() in C
• ptr = (cast-type*) malloc(byte-size)
Pointers in C
P 10
*P = 10;
Notes:
1. The new allocated memory is anonymous ( i.e. does not
have a name of its own
2. The only way we can access this location is via P.
Pointers in C – Dynamic Memory Allocation
head = NULL;
head
Singly Linked List
A linked list in which each node contains only one link
field pointing to the next node in the list
--- 05 60 99
--- 05 60 99
--- 05 60 99
--- 05 60 99
Algorithm create(*H)
{
temp=H;
repeat untill choice =‘y’
{
allocate memory to curr;
accept curr->data;
curr->next=NULL;
temp->next=curr;
temp=curr; //temp=temp->next
Read choice;
}
}
Display Link List Finding length of Link List
Algorithm display(*H)
Algorithm len(*H)
{
if H->next ==NULL {
Print “ list is empty “ curr=H->next;
else while(curr!=NULL)
{ {
//print head node values
i++;
curr=H->next;
while(curr!=NULL) curr=curr->next;
{ }
Print curr,curr->data,curr->next; return(i);
curr=curr->next; }
}
}
Insertion in a linked list
a b
… …
4. 3.
curr
2.Accept
nnode Data
1.Allocate memory
28
Insert a new node by position
Algorithm Insertbypos(*H)
{
i=1 ; curr=H;
//allocate memory for nnode node;
read nnode->data and pos; //accept data & position to be inserted:
k=len();
If(pos>k+1)
//Print “Data can't be inserted”;
else
{
while(curr!=NULL && i<pos)
{
i++;
curr=curr->next;
}
nnode->next=curr->next;
curr->next=nnode;
}
Deletion from a linked list
… a x b
…
deletedNode
current
Algorithm reverse(*H)
{
prev=NULL;
curr=head->next;
while(curr!=NULL)
{
future=curr->next;
curr->next=prev;
prev=curr;
curr=future;
}
head->next=prev;
}
Sorting of SLL by using pointers
Algorithm sort(*H) if(curr->data > temp->data
{ {
len=len(H); prev->next=temp;
curr->next=temp->next;
for i=1 to len-1
temp->next=curr;
{
prev=temp;
prev=H; }
curr=H->next; else
for j=0 to <l-i {
{ prev=curr;
temp=curr->next; curr=curr->next;
}
} //end for inner for
} //end for outer for
} //end Algorithm
Merging of SLL by using pointers
Algorithm merge(*H1,*H2) else
{ {
curr1=H1->next;
temp->next=curr2;
curr2=H2->next;
if(curr1->data<curr2->data) temp=curr2;
{ curr2=curr2->next;
temp=head1; }
flag=1;
} }
else if(curr1==NULL)
{ temp->next=curr2;
temp=head2;
flag=0;
if(curr2==NULL)
} temp->next=curr1;
while(curr1!=NULL && curr2!=NULL) if(flag==1)
{
display(head1);
if(curr1->data<curr2->data)
{ else
temp->next=curr1; display(head2);
temp=curr1; }
curr1=curr1->next;
Arrays Vs Linked Lists
Arrays Linked list
Insertions and Deletions are inefficient: Elements Insertions and Deletions are efficient: No shifting
are usually shifted
No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to
full; otherwise may result in much memory our need) there is no waste of memory.
waste.
Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]
Doubly-Linked Lists
▪ It is a way of going both directions in a linked list, forward and reverse.
▪ Many applications require a quick access to the predecessor node of
some node in list.
A node in a doubly-linked list store two references:
•A next link; that points to the next node in the list, and
•A prev link; that points to the previous node in the list.
Advantages:
Convenient to traverse the list backwards.
Simplifies insertion and deletion because you no longer have to refer to the
previous node.
Disadvantage:
Increase in space requirements.
pre nex
v t
ele nod
m e
Doubly Link List creation
Algorithm Create(*H)
{
struct node temp=H;
{ repeat till choice =y
char data[20]; {
node *next,*prev; //allocate memory for new
}; node //in curr pointer
temp->next=curr;
curr->prev=temp;
curr->next=NULL;
temp=curr;
}
Read choice;
}
Insertion
head current
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
delete oldNode;
current = head;
Circular Linked Lists
Allocation of memory for head node using constructor
p1(x) p2(x)
6 2 3 8 -3 18 0 0 23
0 2 0 2 4
Index
represents
exponents
• Polynomial (continued)
6 2 0 0 -3 0 ………… 0 16
WASTE OF SPACE!
• Polynomial (continued)
P1 23 9 18 7 41 6 163 4 3 0
P2 4 6 10 4 12 1 8 0
coeff exp
a.first 3 1 2 8 1 0 0
4
1 1 1
b.first 8 -3 6 **
4 0 0
Node structure of Polynomial
struct polyNode
{
// All members in “struct” are public
int coef; // coefficient
int exp; // exponent
struct polyNode *next;
};
Addition of Two Polynomials (1)
• It is an easy way to represent a polynomial by
a linked list.
• Example of adding two polynomials a and b
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 1 10 6 0
0
q
c.first 1 1 0 (i) p->exp == q->exp
1 4
Addition of Two Polynomials (2)
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 1 10 6 0
0
q
c.first 1 1 -3 1 0
1 4 0
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 1 10 6 0
0
q
c.first 1 1 -3 1 2 8 0
1 4 0
4-52
• Polynomial (continued)
Algorithm add(*H1,*H2)
{
Allocate a memory for H3;
head3->exp=-1;
t3=H3;
t1=H1->next;
t2=H2->next;
while(t1->exp!=-1||t2->exp!=-1)
{
if(t1->exp==t2->exp)
{
Allocate the memory for temp;
Add t1 coeff and t2 coeff in t3 coeff
copy one of the exponent in t3 exp
t3->next=temp;
temp->next=head3;
t3=temp;
Move t1 to next node ;
Move t2 to next node
}
Addition of Polynomial
else
if exponent of p1 < exponent of p2
Copy node of p2 to end of p3.
else //exponent of p1 > exponent of p2
Copy node of p1 to end of p3
}//end of while
} //end algorithm
Evaluation of Polynomial
Algorithm eval(*H, x)
{
cur=H->next;
while not end of list
{
using value of x find the sum of terms;
move cur to next node ;
}
print sum;
}
Doubly Circular Linked
List
A Doubly linked list where the last node points to the
header node and Header node Points to last node is called
a circular Doubly linked list.
There are no NULL links.
--- 05 60 99
• a0 is the head of list A and the rest (a1, …, an-1) is the tail
of list A.
Examples of Generalized Lists
• A = ( ): the null, or empty, list; its length is zero.
• B = (a, (b, c)): a list of length two; its first element is the atom
a, and its second element is the linear list (b, c).
0 b 0 c 0
C 1 1 1 0 0 C=(B, B,
( ))
D 0 a 1 0 D=(a, D)
**
GLL Examples
• L1=((A, B, C), ((D, E), F), G).
• P(x, y, z)=
• It is invisible to programmer.
Example
Obj Obj2 Obj3
hea 1
d
nex nex nex
t t t
• Sweep Phase
As the name suggests it “sweeps” the unreachable objects
it clears the heap memory for all the unreachable objects.
• All those objects whose marked value is set to false are cleared from the heap
memory, for all other objects (reachable objects) the marked bit is set to false
• Since we will run the algorithm (if required) and again we will go through the
mark phase to mark all the reachable objects.
77
Example:
a) All the objects have their marked bits set to false.
78
b) Reachable objects are marked true
79
c) Non reachable objects are cleared from the heap.
80
Mark-and-Sweep
• Basic idea
– go through all memory and mark every chunk
that is referenced
– make a second pass through memory and
remove all chunks not marked
OS 0 1 2 3
p2 = p2 =
650 360
0 10 35 45 60
0 0 0 0