Unit 2 Linked List
Unit 2 Linked List
malloc()
calloc()
realloc()
free()
malloc()
• The name "malloc" stands for memory allocation.
Syntax of calloc()
ptr = (cast-type*)calloc(n, element-size);
Example:
ptr = (float*) calloc(25, sizeof(float));
Syntax of free()
free(ptr);
Syntax of realloc()
ptr = realloc(ptr, x);
• Find VAL = 4
1 7 3 4 5 2 X
PTR
(Here, PTR -> DATA != 4, so move to next node)
1 7 3 4 5 2 X
1 7 3 4 5 2 X
1 7 3 4 5 2 X
PTR
(Here, PTR -> DATA = 4, so POS = PTR)
Searching an unsorted Linked List
1 7 3 4 5 2 X
START
9
9 1 7 3 4 5 2 X
START
9 1 7 3 4 5 2 X
START
Case 1: New node is inserted at the beginning
1 7 3 4 5 2 X
START
9 X
1 7 3 4 5 2 9 X
START
Case 2: New node is inserted at the end
Step 1: IF AVAIL = NULL then
Write OVERFLOW
GO TO Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET New_Node -> DATA = VAL
Step 5: SET New_Node -> NEXT = NULL
Step 6: IF START = NULL
START = New_Node
GO TO Step 11
[END OF IF]
Step 7: SET PTR = START
Step 8: Repeat Step 9 while PTR -> NEXT != NULL
Step 9: SET PTR = PTR -> NEXT
[END of LOOP]
Step 10: SET PTR -> NEXT = New_Node
Step 11: EXIT
Deleting node from LL
Case 1: First node is deleted
Case 2: Last node is deleted
Case 3: Node after a given node is deleted
Case 4: Node is deleted from a sorted linked list
Case 1: First node is deleted
1 7 3 4 5 2 X
START
7 3 4 5 2 X
START
Case 1: First node is deleted
Step 1: IF START = NULL then
Write UNDERFLOW
GO TO Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START -> NEXT
Step 4: FREE PTR
Step 5: EXIT
Case 2: Last node is deleted
1 7 3 4 5 2 X
START
1 7 3 4 5 X
START
Case 2: Last node is deleted
Step 1: IF START = NULL then
Write UNDERFLOW
GO TO Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR -> NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: IF PTR = START then
START = NULL
ELSE
SET PREPTR -> NEXT = NULL
[END OF IF]
Step 7: FREE PTR
Step 8: EXIT
Circular Linked List
Advantages
1. In linear linked list it is not possible to go to previous
node but within Circular LL possible.
Disadvantages
1. If proper care is not taken, then the problem of
infinite loop can occur.
struct node {
struct node *prev;
int info ;
struct node *next;
};
Inserting a new node in DLL
a.first 3 14 2 8 1 0 X
a 3 x14 2 x 8 1
b.first 8 14 -3 10 10 6 X
b 8 x14 3x10 10 x 6
Operating On Polynomials
• Adding Polynomials
• To add two polynomials, we examine their terms pointed
to by p and q.
• If the exponents of the two terms are equal
1. add the two coefficients
2. create a new term for the result.
• If the exponent of the current term in p is less than q
1. create a duplicate term of q
2. attach this term to the result
3. advance the pointer to the next term in q.
• We take a similar action on a if p->exp > q->exp.
Operating On Polynomials
• E.g., adding two polynomials a and b
a.first 3 14 2 8 1 0 X
p
b.first 8 14 -3 10 10 6 X
q
c.first 11 14 X
(i) p->exp == q->exp
Operating On Polynomials
a.first 3 14 2 8 1 0 X
b.first 8 14 -3 10 10 6 X
q
c.first 11 14 -3 10 X
a.first 3 14 2 8 1 0 X
b.first 8 14 -3 10 10 6 X
q
c.first 11 14 -3 10 2 8 X