Chapter 2 Data Structure
Chapter 2 Data Structure
Paper No –IV
Introduction To Data Structure
(Marks : 50 Periods : 40)
Chapter 2.
Linked List (08 periods)
Content:
1. Introduction to Linked list,
2. Representation of linked list in memory,
3. Traversing,
4. Searching in Unsorted linked list,
5. Overflow and Underflow,
6. Inserting at the beginning of a list,
7. deleting node following a given Node.
Information
Next Pointer Field of
Part of first
node
first node
Dayanand Science College Latur.
CLASS: BSC FY Sub: Introduction to Data Structure Prepared by : Dr. R. B. Shinde
Example: A hospital ward contains 12 beds of which 9 are occupied as given diagram. Suppose we want an
alphabetical listing of the patients. This listing may be given by the pointer field, called Next in the figure.
We use the variable START to point to the first patient. Hence START contains 5, since the first patient,
AMAR occupies bed 5. Also AMAR patient posses next pointer 3 which denotes the bed number of next
patient i.e. dinesh and so on Dinesh patients to next patient bed i.e. 11 which denotes Firdous.
START
Bed Number Patient NEXT
5
1 Kartik 7
2
3 Dinesh 11
4 Mohan 12
5 AMAR 3
6
7 Leela 4
8 Guru 1
9 Smita 0
10
11 Firdous 8
12 Nikhil 9
Above picture is of linked list in memory where each node of the list contains a single character. We can
obtain the actual list of characters as follows.
Algorithm
START= 9, so INFO[9]=N is the first character.
LINK[9]=3, so INFO[3]=O is the second character.
LINK[3]=6, so INFO[6]= (Blank) is the third character.
LINK[6]=11, so INFO[11]=E is the fourth character.
LINK[11]=7, so INFO[7]=X is the fifth character.
LINK[7]=10, so INFO[10]=I is the sixth character.
LINK[10]=4, so INFO[4]=T is the seventh character.
LINK[4]=0, the NULL value, so the List has ended.
• Circular Linked List − Last item contains link of the first element as next and the first element has a
link to the last element as previous.
2.3 Traversing
A linked list is a linear data structure that needs to be traversed starting from the head node until the end of the
list. Unlike arrays, where random access is possible, linked list requires access to its nodes through sequential
traversal. Traversing a linked list is important in many applications. For example, we may want to print a list or
search for a specific node in the list. Or we may want to perform an advanced operation on the list as we traverse
the list. The algorithm for traversing a list is fairly trivial.
a. Start with the head of the list. Access the content of the head node if it is not null.
b. Then go to the next node(if exists) and access the node information
c. Continue until no more nodes (that is, you have reached the last node)
Let LIST be a linked list in memory stored in linear array INFO and LINK with START pointing to the first
element and NULL indicating the end of LIST. Suppose we want to traverse LIST in order to Process each node
exactly once. This section presents an algorithm that does so and then uses the algorithm in some applications.
Our traversing algorithm uses a pointer variable PTR which points to the nodes that is currently being processed.
Accordingly, LINK[PTR] points to the next node to be processed. Thus PTR:=LINK[PTR]
Moves the pointer to the next node in the list, as follows.
Name or
•
Start
• • • X
Dayanand Science College Latur.
CLASS: BSC FY Sub: Introduction to Data Structure Prepared by : Dr. R. B. Shinde
Algorithm details: Initialize PTR or START. Then process INFO[PTR], the information at the first node.
Update PTR by the assignment PTR:=LINK[PTR], so that PTR points to the second node. Then Process
INFO[PTR], the information at the second node. Again update PTR by the assignment operator
PTR:=LINK[PTR], and then process INFO[PTR], the information at the third node. And so on. Continue
until PTR=NULL, which signals the end of the list.
Example:
The following procedure prints the information at each node of a linked list. Since the procedure must
traverse the list.
Algorithm: PRINT(INFO, LINK, START)
This process prints the information at each node of the list.
1. Set PTR:=START.
2. Repeat Step 3 and 4 while PTR ≠ NULL:
3. Write: INFO[PTR].
4. SET PTR:=LINK[PTR]. [Update Pointer.]
[End of Step 2 loop.]
5. Return.
2.4 Searching Unsorted linked list
Let LIST be a linked list in memory which is not sorted, then one searches for ITEM in LIST by traversing
through the list using a pointer variable PTR and comparing ITEM with the contents INFO[PTR] of each
node, one by one, of LIST. Before we update the pointer PTR by PTR:=LINK[PTR]
We require two tests. First we have to check to see whether we have reached the end of list i.e.
PTR= NULL
Then we check to see whether
INFO[PTR]=ITEM
Algorithm:
SEARCH (INFO, LINK, START, ITEM, LOC)
LIST is a linked list in memory. This algorithm finds the location LOC of the node where ITEM first appear
in LIST, or set LOC=NULL.
1. set PTR:=START.
2. Repeat step 3 while PTR ≠ NULL:
3. If ITEM =INFO[PTR], then:
Set LOC:=PTR, and Exit.
Else:
Set PTR:=LINK[PTR].[PTR now points to the next node.]
[End of IF structure]
[End of step 2 loop]
4. [Search is unsuccessful.] set LOC:=NULL.
5. Exit.
Dayanand Science College Latur.
CLASS: BSC FY Sub: Introduction to Data Structure Prepared by : Dr. R. B. Shinde
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point
B.next to C −
Now, the next node at the left should point to the new node.
Dayanand Science College Latur.
CLASS: BSC FY Sub: Introduction to Data Structure Prepared by : Dr. R. B. Shinde
This will put the new node in the middle of the two. The new list should look like this –
Similar steps should be taken if the node is being inserted at the beginning of the list. While inserting it at the
end, the second last node of the list should point to the new node and the new node will point to NULL.
The left (previous) node of the target node now should point to the next node of the target node −
This will remove the link that was pointing to the target node. Now, using the following code, we will remove
what the target node is pointing at.
Dayanand Science College Latur.
CLASS: BSC FY Sub: Introduction to Data Structure Prepared by : Dr. R. B. Shinde
We need to use the deleted node. We can keep that in memory otherwise we can simply deallocate memory
and wipe off the target node completely.