Title: More on
1More on Linked List Algorithms
2- Linked List and Template
-
- Remember the implementation of a double linked
list we learned before. Some of methods are shown
below
class DLL private NodePtr top void
destroyDLL(NodePtr) public DLL()
DLL() void printDLL()
void insertDLL(int number) void
destroy(NodePtr) int searchDLL(int
key)
class Node typedef Node NodePtr class Node
public int number NodePtr next
NodePtr prev
3void DLLdestroyDLL(NodePtr top) NodePtr
temp top while (top ! NULL) top
top-gtnext delete temp temp top
top NULL
void DLLinsertDLL(int number) NodePtr
temp top temp new Node temp-gtnext
top temp-gtnumber number temp-gtprev
NULL if (top ! NULL) top-gtprev
temp top temp
int DLLsearchDLL(int key) NodePtr curr
top int count 0 while (curr ! NULL)
if (curr-gtnumber key) count
curr curr -gt next return
count
void DLLprintDLL() NodePtr cNULL,
currtop while (curr ! NULL)
cout ltlt curr-gtnumber ltlt "--gt" curr
curr-gtnext cout ltlt endl
See Example 1
4- Consider the class node. It has three members
-
- number of type integer
- next of type NodePtr
- prev of type NodePtr
- next and prev are just pointers to the successor
and predecessor of a node respectively. - But number represents the actual content of a
node. If this content has a different structure
instead of being a simple integer, we need to
create a different node class for creating
different types of linked lists. - Fortunately with the help of template tools we
learned in C we can make the content of a node
to be of any data type - Next slide shows the doubly linked list structure
with template
5template ltclass Tgt class DLL private NodeltTgt
top public DLL() DLL()
void destroyDLL( NodeltTgt ) void
printDLL() void insertDLL(T obj) bool
searchDLL(T key) . .
template ltclass Tgt class Node public T elemen
t NodeltTgt next NodeltTgt prev
See Example 2
6template ltclass Tgt bool DLLltTgtsearchDLL(T key)
NodeltTgt curr top while (curr !
NULL) if (curr-gtelement key) return
true curr curr -gt next return false
template ltclass Tgt void DLLltTgtinsertDLL(T obj)
NodeltTgt temp top temp new
NodeltTgt temp-gtnext top temp-gtelement
obj temp-gtprev NULL if (top !
NULL) top-gtprev temp top temp
template ltclass Tgt void DLLltTgtprintDLL()
NodeltTgt currNULL cout ltlt endl curr
top while (curr ! NULL) cout ltlt
curr-gtelement ltlt "--gt" curr
curr-gtnext cout ltlt endl
template ltclass Tgt void DLLltTgtdestroyDLL(NodeltTgt
top) NodeltTgt temp top while (top !
NULL) top top-gtnext delete temp temp
top top NULL
7- Skip Lists
-
- The serious drawback with the linked list is that
to search an element we need to do sequential
scanning of the linked list - One way to solve this problem is to order the
list to speed up searching, but sequential search
is still required. - The Skip List is an interesting variant of
ordered linked list which makes such a
non-sequential search possible - In the skip list method
- Every node is linked (1 ?2 ?3, )
- Every second node is linked (1?3?5?7, )
- Every fourth node is linked (1?5?9? )
- Every eighth node is linked (1?9?17?25, )
- Every sixteenth node is linked (1?17 ?33, )
- .
8(No Transcript)
9- Suppose we are looking for number 16 in the list
- Level 4 in the root node is tried first and we
follow the link realizing that 22 gt 16. - Then level 3 in the root node is tried and we
follow the link to 10 and then from 10 to 22
realizing that 22gt16 - Next we set the pointer to the last valid node
(in this case node 10) we visited but we try next
level. (level 2 in this case) - From 10 we get to 17 and 17gt 16
- So we stay at node 10 again and we try the first
level (level 1) - We follow the link to 12 and then 17 and we
realize that 17gt16 - Since we cannot go any lower, we conclude that 16
is not in the list
10- Searching will improve with skip list method
relative to the regular linked list - But what about insert and delete?
- If we insert a node or delete a node from the
list, the whole structure changes. - The skip list we discussed was a type of evenly
spaced nodes of different levels. Skip lists can
be made of unevenly spaced nodes of different
levels as shown in the next slide
11(No Transcript)
12- Self-Organizing Lists
-
- Other methods to improve the efficiency of the
search in the linked list is to dynamically
organize the linked list in certain order - This organization depends on the configuration of
the data, thus the stream of the data requires
reorganizing the nodes already on the list - Several ways to reorganize the nodes are
- Move to the front method
- Transpose method
- Count method and
- Ordering method
13- Move to the front method
- After the desired element is located, put it at
the beginning of the list - Transpose method
- After the desired element is located, swap it
with its predecessor unless it is at the head of
the list - Count Method
- Order the list by the number of times elements
are being accessed - Ordering Method
- Order the list based on ascending or descending
order
14(No Transcript)
15(No Transcript)
16(No Transcript)
17(No Transcript)
18-
- With the first three methods, we try to locate
the elements most likely to be looked for near
the beginning of the list - Every method except the ordering approach may
require the search for an element to go to the
end of the list - It is important to see how the data in your
environment arrive the system. Then based on the
behavior of the data, you can choose the best
method of searching to implement for your linked
list
19- Sparse Table
-
- In many applications, the choice of table seems
to be the most natural one but space
consideration may also be an important factor to
be considered - Suppose we want to store grades of all students
in a university for a certain semester - Suppose there are 8000 students and 300 classes
(courses) - One way to do this is to create two dimensional
table making the Student-Ids as the columns and
Class-Ids (course Number) to be the rows of the
array. - Then we record the students grades in array
cells
20(No Transcript)
21- Assuming that every student on average can take
five courses per semester. In this case, we would
have 300 - 5 295 empty cells per column - Since there are 8000 columns, 8000295 cells
would be unused which is waste of a lot of space - Another solution is to create two 2-dimensional
arrays - One is array of ClassesTakes where the columns
represent the student-Ids and rows would be the
maximum number of courses that can be possibly
taken by the students - Second is array of StudentInClasses where the
columns are the Course-Ids and rows refer to the
maximum number of students per class
22(No Transcript)
23- Although the numbers of free cells have been
reduced a lot, we still have a lot of free
spaces. - Further, the major limitation of the ClassesTaken
is that no student can take more than 8 courses
per term. Similarly, in StudentsInClasses array,
we must limit the enrollment of each course to
250 students per class - If space is our main concern, the best solution
is to reduce the wasted space as much as possible
without having too much effect in the efficiency
of search. - The next slide shows this sparse table using the
two-dimensional linked list techniques
24(No Transcript)
25- As you see in the slide, Two two-Dimensional
arrays of linked list can be used - Each cell of the array class is a pointer to a
linked list of students taking a class and each
cell of the array students indicates a linked
list of classes taken by a student - The linked lists contain nodes of five members
student-Id, Course-Id, the grade, a pointer to
next student and a pointer to next class - Note that in this method we dynamically obtain
space (creating a node) in memory only if we
require that node to store the related information