Unit 4 Linked List
Unit 4 Linked List
• Types of Linked List: singly linked, linear and Circular Linked Lists, Doubly
Linked List, Doubly Circular Linked List, Primitive Operations on Linked List-
Create, Traverse, Search, Insert, Delete, Sort, Concatenate. Polynomial
Manipulations- Polynomial addition. Generalized Linked List (GLL) concept,
Representation of Polynomial using GLL.
DATA STRUCTURE
LINEAR NONLINEAR
ARRAY
TREES &
LINKLIST STACK
GRAPH
QUEUE
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
Link Field/
Info/Data
Address Field
field
Anatomy of a linked list
• A linked list consists of:
– A sequence of nodes
myList
a b c d
• L is an ordered set
• This allows for flexibility and efficiency, as the size and location of memory
blocks can be changed according to the program logic and data size.
• It also enables the creation and manipulation of complex and dynamic data
structures, such as linked lists, trees, graphs, and hash tables.
• In which memory resources are allocated whenever they are needed, and
freed whenever they are not necessary anymore
• Linked lists, are defined precisely in such a way as to allow for dynamically
allocating and deallocating memory, depending on the requirements of the
application.
Pointers in C
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
Pointers in C – Dynamic Memory Allocation
int main() {
int a= 10;
int *b = &a;
printf("%d" , *b);
}
Output
10
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int n = 10;
int n = 10; int *p = &n;//stores the address of 'n'
int *p = &n;//stores the address of printf("%d" , *p);
'n' return 0;
printf("%p" , p);// p contains the }
address of the number n
Output:
return 0;
} 10
Output:
0x7ffc4187c0ac
Types of linked lists
• The number of pointers are maintained depending on the
requirements and usage
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
• Creating a List
• List Traversal
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
• Merging two linked lists into a larger list
Data Structure of Node
struct node //self referential structure
{
int data;
struct node *next;
}; data Next
(Address of
struct node *head; next node)
Creation of SLL
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
29
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)
display Data can't be inserted;
else
{
while(curr!=NULL && i<pos)
{
i++;
curr=curr->next;
}
nnode->next=curr->next;
curr->next=nnode;
} //end else
Deletion from a linked list
a x b
… …
deletedNode
current
k=len();
if(k<pos)
display Data can't be deleted;
else
{
while(ctr<pos && curr!=NULL)
{
ctr++;
prev=curr;
curr=curr->next;
}
temp=curr;
prev->next=curr->next;
curr->next=NULL;
free(temp);
}
Sorting of SLL by using pointers
Algorithm sort(*H)
else
{ len=len(H);
{
for i=1 to len-1
prev=curr;
{
curr=curr->next;
prev=H;
}
curr=H->next;
} //end for inner for
for j=0 to <l-i
} //end for outer for
{ temp=curr->next;
} //end Algorithm
if(curr->data > temp-
>data)
{
prev->next=temp;
curr->next=temp-
>next;
temp->next=curr;
prev=temp;
Merging of SLL by using pointers
Algorithm merge(*H1,*H2) else
{ curr1=H1->next; {
curr2=H2->next; temp->next=curr2;
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)
{
if(curr1->data<curr2->data) display(head1);
{ 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.
prev next
elem node
Doubly Link List creation
struct node
{
char data[20];
node *next,*prev;
};
Algorithm Create(*H)
{
temp=H;
repeat till choice =y
{
allocate memory for new node
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
head=(struct node *)malloc(sizeof(struct node));
head->next=head;
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
a 3 x14 2 x 8 1
b.first 8 14 -3 10 10 6 **
b 8 x14 3 x10 10 x 6
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 10 10 6 0
q
c.first 11 14 0 (i) p->exp == q->exp
Addition of Two Polynomials (2)
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 10 10 6 0
c.first 11 14 -3 10 0
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 10 10 6 0
q
c.first 11 14 -3 10 2 8 0
4-53
• Polynomial (continued)
• Adding polynomials using a Linked list representation: (storing the result in p3)
To do this, we have to break the process down to cases:
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;
}
Generalized Lists
• A generalized list, A, is a finite sequence of n ≥ 0
elements, a0, a1, a2, …, an-1, where ai, is either an atom or a
list. The elements ai,0 ≤ i ≤ n – 1, that are not atoms are
said to be the sublists of A.
• 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)
**
(p, q(r, s(u, v), w) (x, y))
General Polynomial
p ( x, y, z ) x10 y 3 z 2 2 x 8 y 3 z 2 3 x 8 y 2 z 2 x 4 y 4 z 6 x 3 y 4 z 2 yz
• P(x, y, z)=
(( x10 2 x 8 ) y 3 3 x 8 y 2 ) z 2 (( x 4 6 x 3 ) y 4 2 y ) z
• Rewritten as Cz2 + Dz, where C and D are
polynomials.
• Again, in C, it is of the form Ey3 + Fy2, where E and
F are polynomials.
• In general, every polynomial consists of a variable
plus coefficient-exponent pairs. Each coefficient
may be a constant or a polynomial.
• Suppose we need to devise a data representation for them and
consider one typical example, the polynomial P(x,y,z) =
PUSH pnode
typedef struct
pnode
{ 23 45 67 78 NULL
pnode * next;
int item; TOP
} Stack implementation using Linked list includes
POP
Inserting Element from top
Deleting Element from top
• It is invisible to programmer.