Linked List - DSA
Linked List - DSA
Examination scheme:
Marks-50 [Class Continuous Assessment]
Marks-50 [lab Continuous Assessment] Marks-50[Term End Exam]
UNIT – IV
• Linked List: Linked List as an Abstract Data Type, Representation of Linked List Using Sequential
Organization, Representation of Linked List Using Dynamic Organization, Operations on Linked
List, Polynomial operations using linked list.
• Circular Linked List, Doubly Linked List , Generalized Linked List (GLL)
• Case Study : Garbage Collection
• Circular Linked List, Doubly Linked List , Generalized Linked List (GLL)
DATA STRUCTURE
LINEAR
NONLINEAR
ARRAY
LINKLIST STACK
QUEUE TREES & GRAPH
• 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
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.
DATA STRUCTURES -I WITH C
19
PROGRAMMING
Pointers in C – Dynamic Memory Allocation
head = NULL;
head
--- 05 60 99
--- 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 STRUCTURES -I WITH C
26
PROGRAMMING
Data Structure of Node
struct node
{
int data; data Next
(Address of
struct node *next; next node)
};
struct node *head;
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;
}
}
DATA STRUCTURES -I WITH C
28
PROGRAMMING
Display Link List Finding length of Link List
1.Allocate memory
… a x b
…
deletedNode
current
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
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]
elem node
DATA STRUCTURES -I WITH C
38
PROGRAMMING
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
}; temp->next=curr;
curr->prev=temp;
curr->next=NULL;
temp=curr;
}
Read choice;
}
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
delete oldNode;
current = head;
p1(x) p2(x)
6 2 3 8 -3 18 0 0 23
0 2 0 2 4
Index
represents
exponents
DATA STRUCTURES -I WITH C
46
PROGRAMMING
• Polynomial (continued)
6 2 0 0 -3 0 ………… 0 16
WASTE OF SPACE!
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 14 2 8 1 0 0
a 3 x14 2 x 8 1
b.first 8 14 -3 10 10 6 **
b 8 x14 3 x10 10 x 6
DATA STRUCTURES -I WITH C
50
PROGRAMMING
Node structure of Polynomial
struct polyNode
{
// All members in “struct” are public
int coef; // coefficient
int exp; // exponent
struct polyNode *next;
};
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
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
• 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
}
DATA STRUCTURES -I WITH C
56
PROGRAMMING
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
• a0 is the head of list A and the rest (a1, …, an-1) is the tail
of list A.
• 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)
**
DATA STRUCTURES -I WITH C
62
PROGRAMMING
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.
TOP
Stack implementation using Linked list includes
POP
Inserting Element from top
Deleting Element from top
• It is invisible to programmer.