UNIT 4
QUESTIONS
Q1 What is link list and write its advantages and disadvantages
The linked allocation method of storage may result in both efficient use of memory and computer time.
Linked List
Operations on linked list
Advantages of Linked List
Disadvantages of Linked List
Q2What are types of linked list
Types of linked list
1. Singly Linked List
2. Circular Linked List
3. Doubly Linked list
4. Doubly circular link list
Q3 Write basic operations in linked list
1. Singly linked list
1. Insertion at start first position
Function: INSERT(X, First)
IF AVAIL = NULL
Then Write (“Availability Stack Underflow”)
Return(FIRST)
AVAIL←NEW
LINK(AVAIL)←AVAIL
X←INFO (NEW)
FIRST←LINK (NEW)
Return (NEW)
When INSERT is invoked it returns a pointer value to the variable FIRST
INSERT (X, FIRST)←FIRST
2. Insertion at end
Function: INSEND(X, First) (Insert at end)
INFO and LINK fields.
variable.
IF AVAIL = NULL
Then Write (“Availability Stack Underflow”)
Return(FIRST)
AVAIL←NEW
LINK(AVAIL)←AVAIL
X←INFO (NEW)
NULL←LINK (NEW)
If FIRST = NULL
then Return (NEW)
FIRST←SAVE
Repeat while LINK (SAVE) ≠ NULL
LINK (SAVE)←SAVE
NEW←LINK (SAVE)
Return (FIRST)
When INSERTEND is invoked it returns a pointer value to the variable FIRST
INSERTEND (X, FIRST)←FIRST
3. Deletion a node from Linked List
Function: DELETE(X, FIRST)
If FIRST = NULL
then write (‘Underflow’)
return
FIRST←SAVE
Repeat thru step-5 while SAVE ≠ X and LINK (SAVE) ≠ NULL
SAVE←PRED
LINK (SAVE)←SAVE
If SAVE ≠ X
then write (‘Node not found’)
return
If X = FIRST (if X is first node?)
LINK (FIRST)←then FIRST
LINK (X)←else LINK (PRED)
[Free Deleted Node]
Free (X)
Q4 Write basic operations in doubly linked list
Doubly linked list
1. Insertion in first
Function: DOUBINS (L, R, M, X)
NEW NODE
2. [Copy information field]
INFO (NEW) ← X
3. [Insert into an empty list]
If R = NULL
Then LPTR (NEW) ←RPTR (NULL) ← NULL
L ← R ← NEW
Return
4. [Is left most insertion?]
If M = L
Then LPTR (NEW)←NULL
RPTR (NEW) ← M
LPTR (M)← NEW
L ← NEW
Return
5. [Insert in middle]
LPTR (NEW)←LPTR (M)
RPTR (NEW) ← M
LPTR (M) ← NEW
RPTR (LPTR (NEW)) ← NEW
6. Return
2. Insertion in middle
Function: DOUBINS_ORD (L, R, M, X)
NEW NODE
2. [ Copy information field]
INFO (NEW) ← X
3. [Insert into an empty list]
If R = NULL
Then LPTR (NEW) ←RPTR (NULL) ← NULL
L ← R ← NEW
Return
4. [Does the new node precedes all other nodes in List?]
If INFO(NEW) ≤ INFO(L)
Then RPTR (NEW) ← L
LPTR(NEW)← NULL
LPTR (L) ← NEW
L ← NEW
Return
5. [Initialize temporary Pointer]
SAVE ← L
6. [Search for predecessor of latest node]
Repeat while RPTR(SAVE) ≠ NULL and INFO(NEW) ≥ INFO(RPTR(SAVE))
SAVE ←RPTR (SAVE)
7. [Set link field of latest node and its predecessor]
RPTR (NEW) ←RPTR(SAVE)
LPTR (RPTR(SAVE))←NEW
RPTR (SAVE) ← NEW
LPTR (NEW) ← SAVE
If SAVE = R
Then RPTR(SAVE)←NEW
3. Deletion in doubly link list
Function: DOUBDEL (L, R, OLD)
If R=NULL
Then write (‘UNDERFLOW’)
return
2. 2. [Delete node]
If L = R (single node in list)
Then L ← R ← NULL
else If OLD = L (left most node)
Then L ←RPTR(L)
LPTR (L) ← NULL
else if OLD = R (right most)
Then R←LPTR (R)
RPTR (R) ← NULL
elseRPTR (LPTR (OLD)) ←RPTR (OLD)
LPTR (RPTR (OLD)) ←LPTR (OLD)
3. 3. [ FREE deleted node]
FREE (OLD)
Q5 Write basic operations in circular linked list
Circular link list
1. Insertion at start
PROCEDURE: CIRCULAR_LINK_INSERT_FIRST (X, FIRST, LAST)
NEW ← NODE
2. [Initialize fields of latest node and its link to the list]
INFO (NEW) ←X
If FIRST = NULL
Then LINK (NEW) ←NEW
FIRST ← LAST ←NEW
else LINK (NEW) ← FIRST
LINK (LAST)←NEW
FIRST ←NEW
Return
2. Insertion at end
PROCEDURE: CIR_LINK_INSERT_END (X, FIRST, LAST)
NEW ←NODE
2. [Initialize fields of latest node and its link to the list]
If FIRST = NULL
Then LINK (NEW) ←NEW
FIRST ←LAST ← NEW
else LINK(NEW) ← FIRST
LINK(LAST) ← NEW
LAST ← NEW
Return
3. Insertion in between
PROCEDURE: CIR_LINK_INSERT_ORDER (X, FIRST, LAST)
NEW ← NODE
2. [Copy information content into new node
INFO (NEW) ← X
3. [Is Linked List is empty?]
If FIRST = NULL
Then LINK (NEW) ← NEW
FIRST ← LAST ← NEW
Return
4. Does new node precedes all other nodes in List?]
If INFO (NEW) ≤ INFO (FIRST)
Then LINK (NEW) ← FIRST
LINK (LAST) ← NEW
FIRST ← NEW
Return
5. [Initialize Temporary Pointer]
SAVE ← FIRST
6. [Search for Predecessor of latest node]
Repeat while SAVE ≠ LAST and INFO(NEW) ≥ INFO(LINK(SAVE))
SAVE ←LINK(SAVE)
7. [Set link field of latest node and its Predecessor]
LINK(NEW) ← LINK(SAVE)
LINK(SAVE) ← NEW
If SAVE = LAST
Then LAST ← NEW
8. [Finish]
Return
4. Deletion in circular link list
PROCEDURE: CIR_LINK_DELETE (X, FIRST, LAST)
If FIRST = NULL
Then write (‘Linked List is Empty’)
Return
2. [Initialize look for X]
SAVE ← FIRST
3. [Find X]
Repeat thru step 5 while SAVE ≠ X and SAVE ≠ LAST
4. [Update predecessor marker]
PRED← SAVE
5. [Move to next node]
SAVE ← LINK (SAVE)
6. [End of Linked List]
If SAVE ≠ X
Then write(‘Node not found’)
return
7. [Delete X]
If X = FIRST
Then FIRST ← LINK (FIRST)
LINK (LAST) ← FIRST
else LINK (PRED) ← LINK(X)
If X = LAST
Then LAST ←PRED
8. [Free Deleted Node]
Free (X)
Q6 How dynamically a link list can be represented?
Basic allocator functionality
1. malloc(n) –
2. free(p) –
To malloc a block,
When a block is freed,
node* new_node(long value, node* next)
{
node* n = (node*) malloc(sizeof(node));
n->value = value;
n->next = next;
return n;
}
A list will be identified by a node*. Hence, new_node above is also a kind of push_front, adding a new node to the beginning of a list.
Inserting a value
To insert a value, we pass a pointer to the node before the location to insert at, as well as the value to be inserted. In C, we would do
node* insert(node* p, long value)
{
node* n = new_node(value, p->next);
p->next = n;
return n;
}
Removing a node
Similarly, to remove a node, we pass a pointer to the node before it.
void remove(node* p)
{
node* n = p->next;
if(p->next)
p->next = p->next->next;
free(n);
}
Q7 Explain insertion operation in circular link list, singly link list, and doubly linked list?
Insertion at Singly linked list
Function: INSERT(X, First)
6. [Underflow?]
IF AVAIL = NULL
Then Write (“Availability Stack Underflow”)
Return(FIRST)
7. [Obtain address of next free Node]
AVAIL←NEW
8. [Remove free node from Availability Stack]
LINK(AVAIL)←AVAIL
9. [Initialize fields of latest node and its link to the list]
X←INFO (NEW)
FIRST←LINK (NEW)
10. [Return address of latest node]
Return (NEW)
When INSERT is invoked it returns a pointer value to the variable FIRST
INSERT (X, FIRST)←FIRST
Insertion in doubly link list
Function: DOUBINS (L, R, M, X)
7. [Create New Empty Node]
NEW NODE
8. [Copy information field]
INFO (NEW) ← X
9. [Insert into an empty list]
If R = NULL
Then LPTR (NEW) ←RPTR (NULL) ← NULL
L ← R ← NEW
Return
10. [Is left most insertion?]
If M = L
Then LPTR (NEW)←NULL
RPTR (NEW) ← M
LPTR (M)← NEW
L ← NEW
Return
11. [Insert in middle]
LPTR (NEW)←LPTR (M)
RPTR (NEW) ← M
LPTR (M) ← NEW
RPTR (LPTR (NEW)) ← NEW
12. Return
Insertion at Circular link list
PROCEDURE: CIRCULAR_LINK_INSERT_FIRST (X, FIRST, LAST)
3. [Create New Empty Node]
NEW ← NODE
4. [Initialize fields of latest node and its link to the list]
INFO (NEW) ←X
If FIRST = NULL
Then LINK (NEW) ←NEW
FIRST ← LAST ←NEW
else LINK (NEW) ← FIRST
LINK (LAST)←NEW
FIRST ←NEW
Return
Q8 How polynomial is represented in a link list?
In the above example the top node is of variable x. The temp node shows the primary field as 2 means coefficient and exponent are present.
Since temp node is attached to go node and head node has variable x, temp node having coefficient = 9 and exponent = 5. The above two nodes are often read as 9x5.
Similarly, within the above figure, the node temp1 are often read as x4.
Q9 How items can be deleted in circular, singly and doubly link list?
Deletion a node from singly Linked List
Return the node into availability area.
Function: DELETE(X, FIRST)
8. [Is Empty list?]
If FIRST = NULL
then write (‘Underflow’)
return
9. [Initialize look for X]
FIRST←SAVE
10. [Find X]
Repeat thru step-5 while SAVE ≠ X and LINK (SAVE) ≠ NULL
11. [Update predecessor marker]
SAVE←PRED
12. [Move to next node]
LINK (SAVE)←SAVE
13. [End of the list]
If SAVE ≠ X
then write (‘Node not found’)
return
14. [Delete X]
If X = FIRST (if X is first node?)
LINK (FIRST)←then FIRST
LINK (X)←else LINK (PRED)
15. [Free Deleted Node]
Free (X)
Deletion in doubly link list
Function: DOUBDEL (L, R, OLD)
4. [Is underflow?]
If R=NULL
Then write (‘UNDERFLOW’)
return
5. 2. [Delete node]
If L = R (single node in list)
Then L ← R ← NULL
else If OLD = L (left most node)
Then L ←RPTR(L)
LPTR (L) ← NULL
else if OLD = R (right most)
Then R←LPTR (R)
RPTR (R) ← NULL
elseRPTR (LPTR (OLD)) ←RPTR (OLD)
LPTR (RPTR (OLD)) ←LPTR (OLD)
6. 3. [ FREE deleted node]
FREE (OLD
Deletion in circular link list
PROCEDURE: CIR_LINK_DELETE (X, FIRST, LAST)
9. [Is Empty List?]
If FIRST = NULL
Then write (‘Linked List is Empty’)
Return
10. [Initialize look for X]
SAVE ← FIRST
11. [Find X]
Repeat thru step 5 while SAVE ≠ X and SAVE ≠ LAST
12. [Update predecessor marker]
PRED← SAVE
13. [Move to next node]
SAVE ← LINK (SAVE)
14. [End of Linked List]
If SAVE ≠ X
Then write(‘Node not found’)
return
15. [Delete X]
If X = FIRST
Then FIRST ← LINK (FIRST)
LINK (LAST) ← FIRST
else LINK (PRED) ← LINK(X)
If X = LAST
Then LAST ←PRED
16. [Free Deleted Node]
Free (X)
Q10 How polynomial is added using a link list?
A linked list that's wont to store Polynomial seems like –
For all power, we'll check for the coefficients of the exponents that have an equivalent value of exponents and add them.
Input - Polynomial P1 and P2 represented as a linked list.
Polynomial
P1= 13x8 +7x5 + 32x2 +54
P2=3x12 +17x5 + 3x3 +98
Algorithm
Resultant polynomial
P =3x12 +13x8 +24x5 +3x3 +32x2 +152
Resultant polynomial after adding 2 polynomials p1 and p2