4 Linked List
4 Linked List
LEARNING OBJECTIVES
temp 5. {
6. printf("List empty");
7. return;
On completion of step 10 – 8. }
temp 9. x = Head → ele;
10. temp = Head;
20 11. if (Head = = Tail)
12. Head = Tail = NULL;
Step 11
13. else
Head
10 15 Tail
14. Head = Head → next;
5
15. printf ("Deleted element "%d", x);
p
16. free(temp);
Step 12, 13 17. }
While (i < pos – 1) Step 4 – Checks for list empty
{ Step 9 – Reads element to delete
P = P → next; Step 10 – Head referred by temp pointer
i++; Step 11 – Checks for last deletion
} Step 14 – Moves the head pointer to next
i < pos element in the list
1 < 2 Step 15 – Displays element to delete
Condition true, so Step 16 – Deallocates memory
Head
5 10 15 Delete tail element
p 1. void del _ tail()
2. {
i becomes 2, 3. int x;
2 < 2 // condition false 4. Node * temp;
Step 14 makes a reference to next of previous element. 5. if (Head = = NULL)
p Tail 6. {
Head 7. printf("\n list empty")
5 10 15
N 8. return ;
9. }
Steps 15 and 16 execute as follows: 10. temp = Head;
p Step 16 N 11. while(temp → next ! = Tail)
Head 12. temp = temp → next;
5 10 15
13. x = Tail → ele;
Step 16 Tail
14. Tail = temp;
20 15. temp = temp → next;
Step 15
temp 16. Tail → next = NULL;
17. printf("\n Deleted element : %d", x)
Now the element 20 becomes the 3rd element in the list.
18. free (temp);
19. }
Deletion Step 4 – Checks for list empty
•• Identify the node Step 10, 11, 12 – Move the temp pointer to last but one
•• Adjust the links, such that deallocation of that node does node of the list
not make the list as unconnected components. Step 13 – Reads tail element to delete
•• Return/display element to delete. Step 14 – Moves tail pointer to last but one
•• Deallocate memory. node
Step 15 – Moves the temp pointer to last node
Delete head element of the list
1. void del _ head() Step 16 – Removes the reference from tail node
2. { to temp node, i.e., tail node becomes
3. int x; the last element
Node * temp; Step 17 – Displays elements to delete
4. if (Head = = NULL) Step 18 – Deallocate memory
3.50 | Unit 3 • Programming and Data Structures
1. START Stack
2. if (P = NULL)
A stack is a last in first out (LIFO) abstract data type and data
1. print (“List is null”);
2. Exit structure. A stack can have any abstract data type as an ele-
3. While (P) ment, but is characterized by only two fundamental operations.
4. R = Q; √ PUSH
5. Q = P; √ POP
6. P = P → next; •• The PUSH operation adds an item to the top of the stack,
7. Q → next = R hiding any items already on the stack or initializing the
8. End While stack if it is empty.
9. Head = Q; •• The POP operation removes an item from the top of the
10. STOP
stack, and returns the poped value to the caller.
•• Elements are removed from the stack in the reverse order to
Double-linked List (DLL)
the order of their insertion. Therefore, the lower elements
Double-linked list is a linked list in which, each node con- are those that have been on the stack for longest period.
tains data part and two link fields.
Node structure:
struct Dnode PUSH POP
{
struct Dnode ∗prev;
int ele;
struct Dnode ∗next;
};
•• prev – points to previous node in list
•• next – points to next node in list Figure 1 Simple representation of a stack
•• The operations which can be performed in SLL can also
be preformed on DLL. Implementation
•• The major difference is that we have to adjust double ref- A stack can be easily implemented either through an array
erence as compared to SLL. or a linked list. The user is only allowed to POP or PUSH
•• We can traverse or display the list elements in forward as items onto the array (or) linked list.
well as in reverse direction.
1. Array Implementation: Array implementation aims
Example: to create an array where the first element inserted is
Tail placed st[0] which will be deleted last.
Head
A B C The program must keep track of position top (last)
element of stack.
Circular-linked List (CLL) Operations
Initially Top = –1;//represents stack empty
Circular-linked list is completely same as SLL, except, in
(i) Push (S, N, TOP, x)
CLL the last (Tail) node points to first (Head) node of list.
{
So, the Insertion and Deletion operation at Head and Tail
if (TOP = = N – 1)
are little different from SLL.
printf(“overflow”);
else
Double Circular-linked List (DCL)
TOP = TOP + 1;
Double circular-linked list can be traversed in both direc- S[TOP] = x;
tions again and again. DCL is very similar to DLL, except }
the last node’s next pointer points to first node of list and (ii) POP (S, N, TOP, x)
first node’s previous pointer points to last node of list. {
So, the insertion and deletion operations at head and tail if (TOP = = –1)
in DCL are little different in adjusting the reference as com- printf(“underflow”);
pared to DLL. else
Storing ordered table as linked list: The table is stored as x = S[TOP]
a linked list, it is retrieved and stored with two pointers, one TOP = TOP – 1
pointer will point to node holding a record having the smallest return x;
key and other pointer performs the search. }
3.52 | Unit 3 • Programming and Data Structures
10. Linked list uses NULL pointers to signal (C) Both (A) and (B)
(A) end of list (B) start of list (D) None of these
(C) Either (A) or (B) (D) Neither (A) nor (B) 1 4.
Linked list are not suitable for implementing
11. Which of the following is essential for converting an (A) Insertion sort
infix to postfix form efficiently? (B) Binary search
(A) Operator stack (B) Operand stack (C) Radix sort
(C) Both (A) and (B) (D) Parse tree (D) Polynomial manipulation
12. Stacks cannot be used to 15. Insertion of node in a double-linked list requires how
(A) Evaluate postfix expression many changes to previous (prev) and next pointers?
(B) Implement recursion (A) No changes (B) 2 next and 2 prev
(C) Convert infix to postfix (C) 1 next and 1 prev (D) 3 next and 3 prev
(D) Allocate resource like CPU by the operating system 16. Minimum number of stacks required to implement a
3. Linked list can be sorted
1 queue is
(A) By swapping data only (A) 1 (B) 2
(B) By swapping address only (C) 3 (D) 4
7. The result of evaluating the postfix expression 10 5 + 9. The attributes of three arithmetic operators in some
60 6/* 8 – is [2015] programming language are given below.
(A) 284 (B) 213
Operator Precedence Associativity Arity
(C) 142 (D) 71
8. Let Q denote a queue containing sixteen numbers and + High Left Binary
S be an empty stack. – Medium Right Binary
Head (Q) returns the element at the head of the queue * Low Left Binary
Q without removing it from Q. Similarly Top(S) The value of the expression
returns the element at the top of S without removing
2 – 5 + 1 – 7 * 3 in this language is ______. [2016]
it from S.
10. A circular queue has been implemented using a sin-
Consider the algorithm given below.
gly linked list where each node consists of a value
and a single pointer pointing to the next node. We
while Q is not Empty do maintain exactly two external pointers FRONT and
if S is Empty OR Top(S) ≤ Head (Q) REAR pointing to the front node and the rear node of
then the queue, respectively. Which of the following state-
x : = Dequeue (Q) ments is/are CORRECT for such a circular queue,
Push (S, x); so that insertion and deletion operations can be per-
else formed in O (1) time?
x : = Pop (S); I. Next pointer of front node points to the rear
enqueue (Q, x); node.
end II. Next pointer of rear node points to the front
end node.
[2017]
The maximum possible number of iterations of the (A) I only (B) II only
while loop in the algorithm is ____ . [2016] (C) Both I and II (D) Neither I nor II
Answer Keys
Exercises
Practice Problems 1
1. A 2. C 3. B 4. B 5. D 6. A 7. A 8. D 9. D 10. A
11. A 12. D 13. C 14. B 15. B 16. B
Practice Problems 2
1. D 2. A 3. C 4. C 5. D 6. A 7. C 8. C 9. A 10. B
11. A