DSStud2023Link Listr (2)
DSStud2023Link Listr (2)
DSStud2023Link Listr (2)
• The structure should be simple enough that one can effectively process the data
when necessary.
Practical Applications of Data Structures
• Score Board of a game is an example of array application.
• A simple question Paper is an array of numbered questions with each of them
assigned some marks.
• 2D arrays, commonly known as, matrices, are used in image processing.
• Speech processing, in which each speech signal is an array.
• Your viewing screen is also a multidimensional array of pixels.
• Book titles in a Library Management Systems.
• Online ticket booking.
• Contacts on a cell phone.
• For CPU scheduling in computer.
• To store the possible moves of chess on a chessboard.
• To store images of a specific size on an android or laptop.
Categorization of Data Structures
• Arrays
• Linked Lists
• Stacks
• Queues
• The arrays for which we need two or more indices are known
as multidimensional array.
• Linked List
15 10
Data member
and pointer NULL pointer (points to nothing)
struct node {
int data;
struct node *nextPtr;
}
• nextPtr
– Points to an object of type node
– Referred to as a link
• Ties one node to another node
Types of Linked Lists
• The top node of a tree is called the root node and each subsequent
node is called the child node of the root. Each node can have one or
more than one child nodes.
• General Tree
– Tree with all nodes having any number of child nodes
• Binary Tree
– Tree with all nodes having maximum two child nodes
A D
C
Graph- A graph, G , is an ordered set (V,E) where V represent set of
elements called nodes or vertices in graph terminology and E represent
the edges between these elements.
Properties of Algorithms:
• Running time of a function call is 1 for setup + the time for any
parameter calculations + the time required for the execution of the
function body.
• Time-Space Tradeoff- The best algorithm to solve a given problem is
one that requires less space in memory and takes less time to
complete its execution. But in practice, it is not always possible to
achieve both of these objectives. Thus, we may have to sacrifice one
at the cost of other. This is known as Time-space tradeoff among
algorithms. Thus if space is our constraint, we have to choose an
algorithm that requires less space at the cost of more execution time.
On the other hand, if time is our constraint, such as in real time
systems, we have to choose a program that takes less time to complete
execution at the cost of more space.
Expressing Space and time complexity: Big ‘O’ notation
• The most important notation used for expressing this function f(n) is
Big O notation.
• Big O notation is a characterization scheme that allows to measure the properties
of algorithm such as performance and/or memory requirements in general fashion.
Assumptions of Big ‘O’ notation
• Uses the dominant term of the function
• Omits lower order terms and coefficient of dominant terms
Apart from n (size of input), efficiency measure will depend on three cases which
will decide number of operations to be performed.
Big ‘O’ notation tries to analyze each algorithm performance in worst condition.
It is the rate of increase of f(n) that is examined as a measure of efficiency.
Rate of growth: Big O notation
• Suppose F is an algorithm and suppose n is the size of input data. Clearly,
complexity f(n) of F increases as n increases. Rate of increase of f(n) is examined
by comparing f(n) with some standard functions such as log 2n, n, nlog2n, n2,n3 and
2n. One way to compare f(n) with these standard functions is to use functional O
notation defined as follows:
• Definition: If f(n) and g(n) are functions defined on positive integers with the
property that f(n) is bounded by some multiple of g(n) for almost all n. That is,
suppose there exist a positive integer no and a positive number M such that for all n>
no we have
|f(n) | ≤ M |g(n)|
• Then we may write
f(n)=O g(n)
Which is read as f(n)(time taken for number of operations) is of the order of g(n). If
g(n)=n, it means that f(n) is a linear proportional to n. For g(n)=n 2 , f(n) is
proportional to n2. Thus if an array is being sorted using an algorithm with g(n)=n 2,
it will take 100 times as long to sort the array that is 10 times the size of another
array.
For the statement f(n)=O g(n) to be informative, g(n) should be as small a function
of n as possible for which f(n)=Og(n) is true.
• Based on Big O notation, algorithms can be categorized as
• Constant time ( O(1)) algorithms
• Logarithmic time algorithms (O(logn))
• Linear Time algorithm (O(n))
• Polynomial or quadratic time algorithm (O(nk))
• Exponential time Algorithm (O(kn))
– It can be seen that logarithmic function log(n) grows most slowly
whereas kn grows most rapidly and polynomial function nk grows
in between the two extremities. Big-O notation, concerns only the
dominant term, low-order terms and constant coefficients are
ignored in a statement. Thus if g(n) = n2+2n, the variation is taken
as O(n2) rather than O(n). Complexities of some well known
searching and sorting algorithms is:
• Linear Search: O(n) Mergesort: O(nlogn)
• Binary Search: O(log2n)
• Bubble sort: O(n2)
2n
10 8
n3
107
n2
10 6
Time taken
105
nlogn
104
n
10 3
logn
10 2
• Repeat-While Loop
– Repeat while condition:
– [End of loop]
Various operations performed on Arrays
• Traversal- processing each element in the list
• Step 7: Return
• Deletion refers to the operation of removing an element from exiting
list of elements. Like insertion, deletion can be done easily from the
end of the list. However to delete an element from any other location,
the elements are to be moved upwards in order to fill up the location
vacated by the removed element.
Algorithm: (Deleting from a Linear Array) DELETE(LA,N,K,ITEM)
Here LA is a linear array with N elements and K is a
positive integer such that K<=N. This algorithm deletes an
element ITEM from Kth position in LA.
• Step 1: [Initialize counter] Set ITEM:=LA[K]
• Step 2: Repeat for J=K to N-1:
[Move J+1st element upward] Set LA[J]:=LA[J+1]
[End of Step 2 Loop]
• Step 3: [Reset N] Set N:=N-1
• Step 4: Return
Algorithm: (Deleting from a Linear Array) DELNUM(LA,N,ITEM)
Here LA is a linear array with N elements. This algorithm
deletes a Number ITEM in LA.
• Step 1: [Initialize counter] Set K=1
• Step 2: Repeat while LA[K] ≠ ITEM
Set K:= K +1
[End of Loop]
• Step 2: Repeat for J=K to N-1:
[Move J+1st element upward] Set LA[J]:=LA[J+1]
[End of Step 2 Loop]
• Step 3: [Reset N] Set N:=N-1
• Step 4: Return
Analysis of Insertion and deletion operation
• The best possible case in insertion operation is when the item is
inserted at the last position. In this case, no movement of elements is
required. The worst case occurs when the element has to be inserted at
the beginning of the list. In this case, we have to move all the
elements down the list. Therefore, the while loop executes n times,
each moving one element down. Thus complexity of insertion
operation is O(n) in worst case and average case, i.e linear time and is
O(1) in best case .
• The best case in deletion occurs when the element to be deleted is the
last element of the array. In this case, no element is moved up. Thus
complexity of deletion in best case is O(1). The worst case occurs
when element is deleted from the first position. In this case, all (n-1)
elements are moved up. The while loop executes n-1 times, each time
moving one element down. Thus complexity of deletion operation is
also O(n) in worst case and average case i.e linear time.
Search In Arrays
• Algorithm: (Linear Search) LINEAR(DATA, N, ITEM, LOC)
[Here DATA is a linear array with N elements and ITEM is a given
item of information. This algorithm finds the location LOC of ITEM
in DATA, or sets LOC:=0 if search is unsuccessful ]
• Step 1: [Initialize Counter] Set LOC:=1
• Step 2: [Search for ITEM]
Repeat while LOC<=N:
If DATA[LOC] = ITEM, Then:
Write: ’Element is at the location’, LOC
Return
[End of if structure]
Set LOC:=LOC+1
[End of Loop]
• Step 3: [Unsuccessful] If LOC=N+1 , then:
Set LOC:=0
• Step 4: Return
• Binary Search: Binary search is a method of searching in which array
list is divided into two equal parts. The main requirement of binary
search method is that the array has to be in sorted order. If the
elements of array list are in ascending order, then , if desired element
is less than the middle element of the list, it will never be present in
second half of the array and if desired element is greater than middle
element, it will never be present in first half of the array list. Thus
focus is given only to the desired half of the array list.
• Algorithm: BINARY(DATA, LB, UB, ITEM, LOC)
[Here DATA is a sorted array with lower bound LB and
upper bound UB, and the ITEM is a given item of
information. The variables BEG, END and MID denote,
beginning, end and middle location of a segment of
elements of DATA. This algorithm finds the location LOC
of ITEM in DATA or set LOC=NULL]
• Step 1: [Initialize segment variable] Set BEG:=LB, END:=UB and
MID:= INT((BEG+END)/2)
Step 2: Repeat while BEG<=END and DATA[MID]≠ITEM
If ITEM < DATA[MID], then:
Set END:= MID -1
Else:
Set BEG:= MID +1
[End of if structure]
• Step 3: Set MID:=INT((BEG+END)/2)
[End of step2 loop]
• Step 4: If DATA[MID]=ITEM, then:
Set LOC:=MID
Else :
Set LOC:=NULL
[End of if structure]
• Step 5: Return
• Analysis of Linear Search and Binary Search
• LINEAR SEARCH
• In best possible case, item may occur at first position. In this case, search operation
terminates in success with just one comparison. The best case of a successful search
is O(1). However, the worst case occurs when either item is present at last position
or element is not there in the array. In former case, search terminates in success with
n comparisons. In latter case, search terminates in failure with n comparisons. Thus
in worst case, the linear search is O(n) operation. In average case, the average
number of comparisons required to find the location of item is approximately equal
to half of the number of elements O(n/2 ) in the array.
.
Complexity of Binary Search
Complexity of binary search (contd.)
In best case, the complexity of insertion sort will be O(n) as only the
passes will work but no movement of elements will be done
LINKED LIST
• Advantages and disadvantages of arrays over a linked list
Advantages:
• Arrays use contiguous memory location which facilitate calculation of
address of nth element if address of first element is known.
• Random access of elements is possible
• Arrays consume less memory as they do not require extra memory for
storage of pointers as required in a linked list
• Due to random access , binary search is possible in arrays which is an
efficient searching algorithm
Disadvantages:
• Arrays require size specification before using them which cannot be
changed . In other words , Arrays cannot grow in size
• Arrays cannot use scattered memory
• Insertion and deletion in Arrays is expensive
• Arrays support only homogenous elements and thus, cannot store
records or heterogeneous data
• Advantage and disadvantages of linked list over an array
Advantages:
• Linked list can use scattered memory easily
• Linked lists are dynamic and grow in size as per the requirement
• Linked lists support heterogeneous data and can store records
• Insertion and deletion of elements in a Linked list is more conveneint
and less expensive in terms of time taken
Disadvantages:
• Linked list does not support random access of elements
• Linked lists can lead to problem of dangling pointers
• Extra memory is wasted in each node of a linked list as each node
requires a pointer for storing the address of next element
• There is no way of calculating the address of nth node of a linked list
from the address of first node
• Binary search is not implemented in a linked list as random access is
not possible
• Linked List- A linked list or one-way list is a linear collection of data
elements, called nodes , where linear order is given by means of
pointers. Each node is divided into two parts:
• The first part contains the information of the element.
• Second part, called the linked field or next pointer field, contains the
address of the next node in the list.
Representation of Linked list in memory
• Linked list is maintained in memory by two linear arrays: INFO and
LINK such that INFO[K] and LINK[K] contain respectively the
information part and the next pointer field of node of LIST. LIST also
requires a variable name such as START which contains the location
of the beginning of the list and the next pointer denoted by NULL
which indicates the end of the LIST.
START
INFO[PTR]
LINK[PTR]
BED NUMBER PATIENT NEXT
START
1 Kirk 7
5
2
3 Dean 11
4 Maxwell 12
5 Adams 3
6
7 Lane 4
8 Green 1
9 Samuels 0
10
11 Fields 8
12 Nelson 9
• Algorithm: (Traversing a Linked List)
Let LIST be a linked list in memory. This algorithm
traverses LIST, applying an operation PROCESS to each
element of LIST. Variable PTR points to the node
currently being processed.
• Step 1: Set PTR:= START
• Step 2: Repeat while PTR ≠ NULL:
Apply PROCESS to INFO[PTR]
Set PTR:= LINK[PTR] [PTR now points to next node]
[End of Step 2 Loop]
• Step 3: Exit
Searching an unsorted Linked List
• 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 appears in
LIST, or sets LOC=NULL
• Step 1: Set PTR:=START
• Step 2: Repeat while PTR≠ NULL
If ITEM = INFO[PTR], then:
Set LOC := PTR
Return
Set PTR:= LINK[PTR]
[End of If structure]
[End of Step 2 Loop]
• Step 3: [Search is unsuccessful] Set LOC:=NULL
• Step 4: Return
Complexity of this algorithm is same as that of linear search
algorithm for linear array. Worst case running time is proportional to
number n of elements in LIST and the average case running time is
proportional to n/2 with condition that ITEM appears once in LIST
but with equal probability in any node of LIST.
The best case complexity of linear search for an element in linked list
is O(1)
AVAIL
ITEM
NEW
AVAIL
NEW
18
ITEM
INSERTING BEFORE A GIVEN NODE
• Algorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, ITEM1)
This algorithm inserts ITEM so that ITEM PRECEDES the
node with data item ITEM1
• Step 1: [OVERFLOW] If AVAIL=NULL, then:
Write: OVERFLOW
Return
• Step 2: [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
• Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]
• Step 4: Set PTR:=START and SAVE:=NULL
• Step 5: Repeat while INFO[PTR]≠ ITEM1
Set SAVE:=PTR and PTR:=LINK[PTR]
[End of Loop]
• Step 6:Set LOC:=SAVE
• Step 7: If LOC=NULL, then:
Set LINK[NEW]:=START and START:=NEW
Else:
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW
[End of If structure]
• Step 8: Return
INSERTING BEFORE A GIVEN NODE WITH LOCATION GIVEN
• Algorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, LOC)
This algorithm inserts ITEM so that ITEM PRECEDES the
node with location LOC
• Step 1: [OVERFLOW] If AVAIL=NULL, then:
Write: OVERFLOW
Return
• Step 2: [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
• Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]
• Step 4: Set PTR:=START and SAVE:=NULL
• Step 5: Repeat while PTR≠ LOC
Set SAVE:=PTR and PTR:=LINK[PTR]
[End of Loop]
• Step 6:Set LOC:=SAVE
• Step 7: If LOC=NULL, then:
Set LINK[NEW]:=START and START:=NEW
Else:
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW
[End of If structure]
• Step 8: Return
INSERTING BEFORE NTH NODE
• Algorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, N)
This algorithm inserts ITEM so that ITEM PRECEDES the Nth node
• Step 1: [OVERFLOW] If AVAIL=NULL, then:
Write: OVERFLOW
Return
• Step 2: [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
• Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]
• Step 4: Set PTR:=START , K:=1, LOC:=NULL
• Step 5: Repeat while K< N-1
PTR:=LINK[PTR]
• Set K:=K+1
Set LOC:=PTR
• [End of Loop]
• Step 7: If LOC=NULL, then:
Set LINK[NEW]:=START and START:=NEW
Else:
Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW
[End of If structure]
• Step 8: Return
INSERTING IN SORTED LIST • SAVE:=PTR and PTR:=LINK[PTR]
• Algorithm: INSSORT(INFO, [End of while loop]
LINK,START,AVAIL, LOC, ITEM)
[This algorithm inserts ITEM so that ITEM
follows the node with location LOC or • Step 6: Set LOC:=SAVE
inserts ITEM as the first node] • Step 7:
when LOC =NULL] If LOC=NULL, then:
• Step 1: [OVERFLOW] Set LINK[NEW]:=START and
• If AVAIL=NULL, then: START:=NEW
Write: OVERFLOW Else:
Return Set LINK[NEW]:=LINK[LOC] and
[end of if structure] LINK[LOC]:= NEW
• Step 2: [Remove first node from AVAIL list] [End of If structure]
Set NEW:=AVAIL and • Step 8: Return
AVAIL:=LINK[AVAIL]
• Step 3: Set INFO[NEW]:= ITEM
[Copies new data into new node]
• Step 4: [save previous address]
• Set SAVE:=NULL and PTR:=START
• Step 5:
• Repeat while ITEM>=INFO[PTR] and
PTR ≠ NULL
Complexity of insertion in linked list
• Complexity of inserting a node in a linked list is O(1)in an empty all the cases as
elements need not be shifted for list or at beginning of the list. This is the best case.
• Inserting at any other position in a linked list is O(n). This complexity is same for
worst and average case.
Deletion of a node from a linked list
• Case 1: Deletion of node following a given node
• Algorithm: DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
This algorithm deletes node N with location LOC. LOCP
is location of node which precedes N or when N is first
node, LOCP=NULL.
• Step 1: If LOC=NULL, then:
Write: ‘UNDERFLOW’
Return
• Step 2: If LOCP=NULL, then:
Set START:=LINK[START] [Deletes first node]
Else:
Set LINK[LOCP]:=LINK[LOC]
[End of if structure]
• Step 3: [Return deleted node to AVAIL list]
Set LINK[LOC]:=AVAIL and
AVAIL:=LOC
• Step 4: Return
Deleting the node with a given item of information
For deleting with given item of information, we first need to know the
location of item to be deleted and also the location preceding the item
to be deleted. For this, one algorithm will be called from the main
algorithm to search for the two locations. Two variables, SAVE and
PTR will be used to save the location of preceding and current node at
every comparison respectively. Once , locations are found, deletion
will be done in main algorithm.
• Algorithm: DELETE(INFO,LINK,START,ITEM,LOC,LOCP)
[This algorithm finds the location LOC of first node N
which contains ITEM and location LOCP of node
preceding N. if ITEM does not appear in the list, procedure
sets LOC=NULL and if ITEM appears in first node, then it
sets LOCP=NULL]
• Step 1: If START=NULL, then:
Write: ‘Underflow’
Return
• Step 2: Set PTR=START
• Step 3: Set SAVE:=NULL and PTR:=START
Repeat while INFO[PTR] ≠ ITEM and PTR≠NULL:
Set SAVE:=PTR and PTR:=LINK[PTR]
[end of while loop]
• Step 4: Set LOC:=PTR and LOCP:=SAVE
• Step 5: IF LOC=NULL, then
Write:`element not in list’
Return
• Step 3: If LOCP=NULL, then:
Set START:= LINK[START]
Else:
Set LINK[LOCP]:=LINK[LOC]
[End of if structure]
• Step 4: Set LINK[LOC]:=AVAIL and AVAIL:=LOC
• Step 5: Return
Concatenating two linear linked lists
• Algorithm: Concatenate(INFO,LINK,START1,START2)
This algorithm concatenates two linked lists with start
pointers START1 and START2
• Step 1: Set PTR:=START1
• Step 2: Repeat while LINK[PTR]≠NULL:
Set PTR:=LINK[PTR]
[End of Step 2 Loop]
• Step 3: Set LINK[PTR]:=START2
• Step 4: Return
• Circular Linked List- A circular linked list is a linked list in which
last element or node of the list points to first node. For non-empty
circular linked list, there are no NULL pointers. The memory
declarations for representing the circular linked lists are the same as
for linear linked lists. All operations performed on linear linked lists
can be easily extended to circular linked lists with following
exceptions:
• While inserting new node at the end of the list, its next pointer field is
made to point to the first node.
• While testing for end of list, we compare the next pointer field with
address of the first node
Circular linked list is usually implemented using header linked
list. Header linked list is a linked list which always contains a special
node called the header node, at the beginning of the list. This header
node usually contains vital information about the linked list such as
number of nodes in lists, whether list is sorted or not etc. Circular
header lists are frequently used instead of ordinary linked lists as
many operations are much easier to state and implement using header
lists
• This comes from the following two properties of circular header
linked lists:
• The null pointer is not used, and hence all pointers contain valid
addresses
• Every (ordinary ) node has a predecessor, so the first node may not
require a special case.
• Algorithm: (Traversing a circular header linked list)
This algorithm traverses a circular header linked list with
START pointer storing the address of the header node.
• Step 1: Set PTR:=LINK[START]
• Step 2: Repeat while PTR≠START:
Apply PROCESS to INFO[PTR]
Set PTR:=LINK[PTR]
[End of Loop]
• Step 3: Return
Searching a circular header linked list
• Algorithm: SRCHHL(INFO,LINK,START,ITEM,LOC)
• This algorithm searches a circular header linked list
• Step 1: Set PTR:=LINK[START]
• Step 2: Repeat while INFO[PTR]≠ITEM and PTR≠START:
Set PTR:=LINK[PTR]
[End of Loop]
• Step 3: If INFO[PTR]=ITEM, then:
Set LOC:=PTR
Else:
Set LOC:=NULL
[End of If structure]
• Step 4: Return
Insertion in a circular header linked list
Algorithm: INSRT(INFO,LINK,START,AVAIL,ITEM,LOC)
This algorithm inserts item in a circular header linked list
after the location LOC
• Step 1:If AVAIL=NULL, then
Write: ‘OVERFLOW’
Exit
• Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
• Step 3: Set INFO[NEW]:=ITEM
• Step 4: Set LINK[NEW]:=LINK[LOC]
Set LINK[LOC]:=NEW
• Step 5: Return
Insertion in a sorted circular header linked list
Algorithm: INSSRT(INFO,LINK,START,AVAIL,ITEM)
This algorithm inserts an element in a sorted circular header
linked list
Step 1: If AVAIL=NULL, then
Write: ‘OVERFLOW’
Return
Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
Step 3: Set INFO[NEW]:=ITEM
Step 4: Set SAVE:=START and PTR:=LINK[START]
Step 5: Repeat while PTR≠START and ITEM <=INFO[PTR]
Set SAVE:=PTR and PTR:=LINK[PTR]
[End of Loop]
Step 6 : Set LOC:=SAVE
Step 5: Set LINK[NEW]:=LINK[LOC]
Set LINK[LOC]:=NEW
Step 6: Return
Deletion from a circular header linked list
Algorithm: DELLOCHL(INFO,LINK,START,AVAIL,ITEM)
This algorithm deletes an item from a circular header linked list.
• Step 1: Set SAVE:=START and PTR:=LINK[START]
• Step 2: Repeat while INFO[PTR]≠ITEM and PTR≠START
Set SAVE:=PTR and PTR:=LINK[PTR]
[End of Loop]
• Step 3: If INFO[PTR]=ITEM, then:
Set LOC:=PTR and LOCP:=SAVE
Else:
Set LOC:=NULL and LOCP:=SAVE
[End of If Structure]
• Step 2: If LOC=NULL, then:
Write: ‘item not in the list’
Return
• Step 3: Set LINK[LOCP]:=LINK[LOC] [Node deleted]
• Step 4: Set LINK[LOC]:=AVAIL and AVAIL:=LOC
• [Memory retuned to Avail list]
• Step 5: Return
Doubly Linked List: Two-way List
• A two-way list is a linear linked list which can be traversed in two
directions: in usual forward direction from beginning of the list to end
and in backward direction from end of list to the beginning. Thus,
given the location LOC of a node N in list, one has immediate access
to both the next node and the preceding node in the list.
• Each node of a two-way list is divided into three parts:
– An information field INFO which contains data of N
– A pointer field FORW which contains the location of next node in
the list
– A pointer field BACK which contains the location of preceding
node.
The list also requires two list pointer variables FIRST which
points to first node in the list and LAST which points to the last node
in the list. Thus null pointer will appear in FORW field of last node in
list and also in BACK field of first node in list.
• Two way lists are maintained in memory by means of linear arrays in
same way as one way list except that two pointer arrays , FORW and
BACK , are required instead of one list pointer variable. The list
AVAIL will still be maintained as a one-way list.
LAST
FIRST
FORW[PTR]
BACK[PTR]
• Trailer node- A trailer node is like a header node in a linked list except
that it stores the address of the last node in a linked list. The
significance of this node is in a doubly linked list that can be traversed
in both the direction. In doubly linked list, we can take the advantage
of trailer node in searching a node in a sorted doubly linked list. The
search will be more efficient.
Operations on a Two-way list
• Traversal
• Algorithm: Traversal
This algorithm traverses a two-way list. FORW and BACK
are the two address parts of each node containing the address
of next node and previous node respectively. INFO is the
information part of each node. START contains the address
of the first node
Step 1: Set PTR:=START
Step 2: Repeat while PTR≠ NULL
Apply PROCESS to INFO[PTR]
Step 3: Set PTR:=FORW[PTR]
[End of Step 2 Loop]
Step 4: Exit
• Algorithm: SEARCH(INFO,FORW,BACK,ITEM,START,LOC)
This algorithm searches the location LOC of ITEM in a two-
way list and sets LOC=NULL if ITEM is not found in the list
• Step 1: Set PTR:=START
• Step 2: Repeat while PTR≠NULL and INFO[PTR]≠ITEM
Set PTR:=FORW[PTR]
[End of Loop]
• Step 3: If INFO[PTR]=ITEM, then:
Set LOC:=PTR
Else:
Set LOC:=NULL
• Step 4: Return
• Algorithm:INSRT (INFO,FORW,BACK,START,AVAIL,LOCA, LOCB, ITEM)
This algorithm inserts an item in a doubly linked list.
LOCA and LOCB location of adjacent nodes A and B
• Step 1: [OVERFLOW] If AVAIL=NULL, then:
Write: OVERFLOW
Return
• Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
Set INFO[NEW]:=ITEM
• Step 3: Set FORW[LOCA]:=NEW and BACK[NEW]:=LOCA
Set FORW[NEW]:=LOCB and BACK[LOCB]:=NEW
• Step 4: Return
• Algorithm: DELETE(INFO,FROW,BACK,START,AVAIL,LOC)
This algorithm deletes a node from a two-way list
• Step 1: If LOC=START , then:
START:=FORW[START]
BACK[START]:=NULL
Return
• Step 2: [Delete node] Set FORW[BACK[LOC]]:=FORW[LOC]
Set BACK[FORW[LOC]]:=BACK[LOC]
• Step 3: [Returning node to AVAIL]
Set FORW[LOC]:=AVAIL and AVAIL:=LOC
• Step 4: Return
Algorithm: DELETE(INFO,FROW,BACK,START,AVAIL,LOC, BACK[START]:=NULL
ITEM) Return
[This algorithm deletes a node from a two-way list when • Step 6: [Delete node]
item to be deleted is given]
Set FORW[BACK[LOC]]:=FORW[LOC]
• Step 1: If START =NULL
Set BACK[FORW[LOC]]:=BACK[LOC]
Write: ‘Underflow’ • Step 7: [Returning node to AVAIL]
Return
Set FORW[LOC]:=AVAIL and AVAIL:=LOC
[End of If Structure] • Step 8: Return
• Step 2: Set PTR:=START
• Step 3: Repeat while PTR ≠ NULL and INFO[PTR] ≠ ITEM
PTR:=FORW[PTR]
[End of While Loop]
• Step 4: If PTR=NULL
Write : ’element already deleted’
Return
[End of If Structure]
• Step 5: If INFO[PTR]=ITEM
Set LOC:=PTR
[End of If Structure]
• Ste 5: If LOC=START , then:
START:=FORW[START]