Vous êtes sur la page 1sur 6

Linked Lists

1 of 6

https://campuscommune.tcs.com/communities/aspire-2016/content/lin...

CC Home (/home)

My Channels (/channels/my_channels)

Image Gallery (/image_gallery)

ExOP (/job_posts)

My Profile (/users/ct20131052920)

People (/users)

Logout (/logout)

Chapter 3: Basic Data Structures

Problem Solving - All Chapters (/communities/aspire-2016/course/problem-solving-6)


Home (/communities/aspire-2016) See Leaderboard
(/communities/aspire2016/aspire_dashboard)
Index
3.1.Introduction (/communities/aspire-2016/content/introduction-37)
3.2.Arrays (/communities/aspire-2016/content/arrays-6)
3.3.Linked Lists (/communities/aspire-2016/content/linked-lists-4)
3.4.Stack (/communities/aspire-2016/content/stack-4)
3.5.Queue (/communities/aspire-2016/content/queue-4)
Go to Doubts

3.3. Linked Lists


Search for a node in the linearly ordered linked list
Problem Statement
Design and implement an algorithm to search a linear ordered linked list for a given alphabetic key or name.
Algorithm development
With the stack and queue data structures it is always clear in advance exactly where the current item should be
retrieved from or inserted. This favourable situation does not prevail when it is necessary to perform insertion
and deletions on linear linked lists. Before we can carry out such operations with an ordered linked list we must
first carry out a search of the list to establish the position where the change must be made. It is this problem
that we wish to consider in the present discussion. We will need such a search algorithm later when we come
to discuss linked list insertion and deletion operations. Before starting on the algorithm design let us consider
an example that defines the problem we wish to solve. Suppose we have a long alphabetically ordered list of
names as shown in Figure : 3.7 and that we wish to insert the name DAVID.

The task that must be performed before the insertion can be made is to locate exactly where DAVID should be
inserted. After examining the names list above we quickly respond that DAVID needs to be inserted between
DANIEL and DROVER. At this point, we will not concern ourselves with how the insertion (or deletion) is made
but rather we will concentrate on developing the accompanying search algorithm for a linked list structure. In
deciding where DAVID should be inserted, what we have to do is search the list until we have either found a

20-04-2016 13:30

Linked Lists

2 of 6

https://campuscommune.tcs.com/communities/aspire-2016/content/lin...

name that comes alphabetically later (in the insertion case) or until we have found a matching name (in the
deletion case). On the assumption that the list is ordered, there will be no need to search further in either case.
The central part of our search algorithm will therefore be:
1. While current search name comes alphabetically after current list name do,
(a) Move to next name in the list.
This algorithm does not take into account that the search name may come after the last name in the list and as
such it is potentially infinite, we will need to take this into account later. Our development so far has been
straightforward and the problem would be almost solved if we only had to search a linear list rather than a
linked linear list. The motivation for maintaining an ordered set of names as a linked list arises because it
allows for efficient insertion and deletion of names while still retaining the alphabetical order. As we will see
later this is certainly not true for a list of names stored as a linear array. At this point, we are ready to consider
the linked list representation of an ordered list and the accompanying search algorithm.
In the linked list data structure each record or name has an appended pointer field that points to the locate-on
the next record in logical (usually alphabetical) order in the list. From this definition we can see that there no
longer has to be a direct correspondence between the way the names are arranged physically and their logical
order. It is this relaxation of the relationship between physical and logical order that makes it possible to do
efficient insertions and deletions. The difference is illustrated below in Figure : 3.8

In conducting a search of a linked list we must follow the logical order rather than the physical order. The
search of an ordered list like the one we have just described must begin at first list element in logical order. If all
list nodes have the structure,
listnode = record
listname : nameformat;
next: listpointer
end
then we will need to use the linkage information to proceed along the list (i.e. if the search cannot be terminated
at the first element). This will simply amount to assigning the pointer value next in the current node to the
current node pointer, i.e.
current:=next
If we step through the logical order of the list in this manner we will eventually arrive at the desired position
corresponding to the place where the search name either exists or is able to be inserted. Assuming the search
has terminated we must now ask what information must be passed back to the calling procedure to allow an
amendment to the list or to indicate whether the name is present or not? A Boolean variable not found which is
set to true when the search procedure is called and which may subsequently be set to false, can be used to
terminate the search. In our search, at the time of termination, we will have a pointer to the current node (as
shown in Figure : 3.9 and 3.10). i.e.,

20-04-2016 13:30

Linked Lists

3 of 6

https://campuscommune.tcs.com/communities/aspire-2016/content/lin...

In both situations, the pointer to the current node is actually the information stored in the "DANIEL" node. To
make either an insertion or a deletion the pointer information stored in the "DANIEL" node is going to have to
be changed (i.e.in the insertion case DANIEL's pointer would have to point to the DAVID node rather than the
DROVER node). Our search mechanism should therefore always keep around the pointer to the previous node
which can be returned to the calling procedure when the search terminates. This can be done (assuming the
statement issued) using the following steps at each node before moving on to the next node:
previous:=current;
current:=next;
The only other consideration that must be made relates to the termination problem that we had mentioned
earlier. Our search mechanism should be able to handle the case where the name we are searching for is
alphabetically beyond the end of the list (e.g. the last name in the list might be WILSON and the search name
ZELMAN). Since the last name in the list has no successor its pointer points to "nowhere". This situation is
signalled by setting the pointer field to nil(as shown in Fig: 3.11). If WILSON were the last name in the list we
would have:

The search loop should therefore terminate either on reaching the search name's position (virtual or real) or on
encountering a nil pointer. A special pointer to the start of the listhead will need to be supplied to the search
procedure. To accommodate the case where the search name occurs before the first name in the list the
pointer to the previous node will need to be initialized to nil. Our algorithm can now be detailed.
Algorithm description
1.
2.

Establish search name, and the pointer to the start of the list.
Initialize previous node pointer to nil and set flag for continuing the search to true and current pointer
to start of list.
3. While search can logically proceed and not at end of list do,
4.
If search name is alphabetically less than current list name then,
5.
(a.1) set condition for terminating search.
6.
else
7.
(a'.1) set previous node pointer to current node pointer,

20-04-2016 13:30

Linked Lists

4 of 6

https://campuscommune.tcs.com/communities/aspire-2016/content/lin...

8.
9.
10.

(a'.2) adjust current node pointer so that it points to next node.


Establish whether or not search name found.
Return previous and current node pointers and found status.

Performance Analysis
The cost of the ordered list search is proportional to the number of nodes that have to be examined before the
search terminates. Over a large number of trials, if the search has equal probability of stopping at each node,
then on average half the list will need to be searched. In the worst case all nodes will have to be examined.
Applications
1. Maintaining short linked lists.
For further reading you may refer to the below links
http://webserver.ignou.ac.in/virtualcampus/adit/course/cst103/block3/unit2/cst103-bl3-u2-06.htm
(http://webserver.ignou.ac.in/virtualcampus/adit/course/cst103/block3/unit2/cst103-bl3-u2-06.htm )
http://www.ee.ryerson.ca/~courses/coe428/sorting/mergesort.html
/sorting/mergesort.html)

(http://www.ee.ryerson.ca/~courses/coe428

http://algorithms.openmymind.net/search/linear.html (http://algorithms.openmymind.net/search/linear.html)
http://www.sparknotes.com/cs/searching/linearsearch/section1.rhtml
/linearsearch/section1.rhtml)

(http://www.sparknotes.com/cs/searching

Related Videos:

Previous
Arrays
(/communities/aspire-2016/content/arrays-6) (/communities/aspire-2016/content/arrays-6)
Next
Stack
(/communities/aspire-2016/content/stack-4) (/communities/aspire-2016/content/stack-4)

Ask a Doubt:
Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune
Note: Please do not use the doubts section for any quiz/quiz-content related queries.
Use the helpline (/feedbacks/new) ( ) located above in top right corner for problems/queries related to
quizzes.

20-04-2016 13:30

Linked Lists

5 of 6

https://campuscommune.tcs.com/communities/aspire-2016/content/lin...

Characters (including HTML): 0

Submit

Open Doubts

Closed Doubts

My Doubts

Prabhu T (/users/ct20141388485) about 18 days ago


Reply

Suman B (/users/ct20151766537) about 21 days ago


some questions in quiz are out of the study material? what we need to do? Need study the links and
videos provided there?
Reply

Sainishanth Bodapati (/users/ct20151715343) about 12 days ago


its better if you go through the videos for better scoring

Suman B (/users/ct20151766537) about 21 days ago


please update the quiz? its showing wrong even for correct answres? we loosing our marks for this.

Reply

Haritha Mallu (/users/ct20151447772) about 1 month ago


What is a smallest divisior of an even integer?
Reply

Rahul Goyal (/users/ct20141383988) about 22 days ago


1

Sidhanth Mohapatro (/users/ct20151707279) about 17 days ago


2

Sidhanth Mohapatro (/users/ct20151707279) about 17 days ago


2

Maitri Roy (/users/ct20151588979) about 16 days ago


2

Maitri Roy (/users/ct20151588979) about 16 days ago

20-04-2016 13:30

Linked Lists

6 of 6

https://campuscommune.tcs.com/communities/aspire-2016/content/lin...

Rashid Akhtar (/users/ct20151724428) about 10 days ago


Depends if the integer is positive or negative. If the integer is positive (eg. 4) then the smallest divisor will
be -4, else if the integer is negative (eg. -4) then the number itself will be the smallest divisor(-4).

Nikhil Muskula (/users/ct20130913682) about 1 day ago


2

Anmol Goel (/users/ct20141175168) about 1 month ago


Reply

20-04-2016 13:30

Vous aimerez peut-être aussi