Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Chapter 2 Data Structure

This document is a course material for a BSC FY class on Introduction to Data Structures, focusing on Linked Lists. It covers topics such as the structure of linked lists, their representation in memory, traversal, searching, and operations like insertion and deletion. The document also explains concepts like overflow and underflow in the context of linked lists.

Uploaded by

Shukla Arun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 2 Data Structure

This document is a course material for a BSC FY class on Introduction to Data Structures, focusing on Linked Lists. It covers topics such as the structure of linked lists, their representation in memory, traversal, searching, and operations like insertion and deletion. The document also explains concepts like overflow and underflow in the context of linked lists.

Uploaded by

Shukla Arun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Dayanand Science College Latur.

CLASS: BSC FY Sub: Introduction to Data Structure Prepared by : Dr. R. B. Shinde

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.

2.1 Introduction to Linked List


Linked Lists:
A Linked list, or one-way list, is a linear collection of data elements, called nodes, where the linear order is
given by means of pointers. Each node is divided into two parts: the first part contains the information of the
elements, and the second part, called the link field, or nextpointer field, which contains the address of the
next node in the list.
Following figure is a schematic diagram of a linked list with4 nodes. Each node is pictured with two part.
The left part represents the information part of the node, which may contain an entire record of data items.
The right part represents the nextpointer field of the node, and there is an arrow drawn from it to the next
node in the list. This follows the usual practice of drawing an arrow from a field to a node when the address
of the node appears in the given field.
The pointer of the last node contains a special value, called the null pointer. The null pointer, denoted by x in
the diagram, signals the end of the list. The linked list also contains a list pointer variable-called START or
NAME- which contains the address of the first node in the list. Hence there is an arrow drawn from START
to the first node. We need only this address in START to trace through the list.

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

2.2 Representation of linked list in memory


Let LIST be a linked list. Then LIST will be maintained in memory specified as follows. First of all, LIST
requires two linear arrays we will call them here INFO and LINK- such that INFO[K] and LINK[K] contain,
respectively, the information part and the nextpointer field of a node LIST. As noted above LIST also
requires a variable name- such as START. START contains the location of the beginning of the list, and a
nextpointer sentinel -denoted by NULL- which indicate the end of the list. Since the subscripts of the array
INFO and LINK are usually positive, we will choose NULL=0.
The following example of linked list indicate that the nodes of a list need not occupy adjacent
elements in the array INFO and LINK, and that more than one list may be maintained in the same linear
array INFO and LINK. However, each list must have its own pointer variable giving the location of its first
node.
INFO LINK
1
2
START 9 3 O 6
4 T 0
5
6 Space 11
7 X 10
8
9 N 3
10 I 4
11 E 7
12
Dayanand Science College Latur.
CLASS: BSC FY Sub: Introduction to Data Structure Prepared by : Dr. R. B. Shinde

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.

In other words, NO EXIT is the character string.


Types of Linked List
Following are the various types of linked list.

• Simple Linked List − Item navigation is forward only.

• Doubly Linked List − Items can be navigated forward and backward.

• 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: 1. Set PTR:=START.[Initializes pointer PTR]


2. Repeat Step 3 and 4 while PTR≠ NULL.
3. Apply PROCESS to INFO[PTR].
4. Set PTR:= LINK[PTR]. [PTR now points to the next node.]
[End of Step 2 loop.]
5. Exit.

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

2.5 Overflow and Underflow


Sometimes new data are to be inserted into a data structure but there is no available space, i.e. the free-storage
list is empty. This situation is usually called overflow. The programmer may handle overflow by printing the
message OVERFLOW. In such a case, the programmer may then modify the program by adding space to the
underlying arrays. Observe that overflow will occur with our linked lists when AVAIL=NULL and there is an
insertion.
Analogously, the term underflow refers to the situation where one wants to delete data from a data structure
that is empty. The programmer may handle underflow by printing the message UNDERFLOW. Observe that
underflow will occur with our linked when START = NULL and there is a deletion.

2.6 Inserting after given node


Adding a new node in linked list is a more than one step activity. We shall learn this with diagrams here. First,
create a node using the same structure and find the location where it has to be inserted.

Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point
B.next to C −

NewNode.next −> RightNode;

It should look like this −

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

LeftNode.next −> NewNode;

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.

2.7 deleting node with a given item of information.


Deletion is also a more than one step process. We shall learn with pictorial representation. First, locate the
target node to be removed, by using searching algorithms.

The left (previous) node of the target node now should point to the next node of the target node −

LeftNode.next −> TargetNode.next;

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

TargetNode.next −> NULL;

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.

You might also like