More on Linked List Algorithms. Linked List and Template Remember the implementation of a double linked list we learned before. Some of methods are shown.

Slides:



Advertisements
Similar presentations
COMP171 Fall 2005 Lists.
Advertisements

Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
CSE Lecture 12 – Linked Lists …
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 19: Searching and Sorting Algorithms
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Tirgul 9 Amortized analysis Graph representation.
Lists Lists as an abstract data type (ADT)
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Lecture 10: Search Structures and Hashing
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 3: Arrays, Linked Lists, and Recursion
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 2.
Doubly Linked Lists Deleting from the end of the list – Have to traverse the entire list to stop right in front of tail to delete it, so O(n) – With head.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Data Structures Week 6 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Multi-way Trees. M-way trees So far we have discussed binary trees only. In this lecture, we go over another type of tree called m- way trees or trees.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Linked List by Chapter 5 Linked List by
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
© Love Ekenberg Hashing Love Ekenberg. © Love Ekenberg In General These slides provide an overview of different hashing techniques that are used to store.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Data Structure and Algorithms
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
CSCE 210 Data Structures and Algorithms
Algorithms for iSNE Dr. Kenneth Cosh Week 2.
Lists CS 3358.
Data Structure Dr. Mohamed Khafagy.
COP3530- Data Structures Advanced Lists
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
Chapter 20: Binary Trees.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Further Data Structures
Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

More on Linked List Algorithms

Linked List and Template Remember the implementation of a double linked list we learned before. Some of methods are shown below class Node; typedef Node* NodePtr; class Node { public: int number; NodePtr next; NodePtr prev; }; class DLL { private: NodePtr top; void destroyDLL(NodePtr&); public: DLL(); ~DLL(); void printDLL(); void insertDLL(int number); void destroy(NodePtr&); int searchDLL(int key); … };

void DLL::insertDLL(int number) { NodePtr temp = top; temp = new Node; temp->next = top; temp->number = number; temp->prev = NULL; if (top != NULL) top->prev = temp; top = temp; } void DLL::destroyDLL(NodePtr& top) { NodePtr temp = top; while (top != NULL) { top = top->next; delete temp; temp = top; } top = NULL; } int DLL::searchDLL(int key) { NodePtr curr = top; int count = 0; while (curr != NULL) { if (curr->number == key) count++; curr = curr -> next; } return count; } void DLL::printDLL() { NodePtr c=NULL, curr=top; while (curr != NULL) { cout number "; curr = curr->next; } cout << endl; } See: Example 1

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

template class Node { public: Telement; Node *next; Node *prev; }; template class DLL { private: Node *top; public: DLL(); ~DLL(); void destroyDLL( Node *&); void printDLL(); void insertDLL(T obj); bool searchDLL(T key); …. }; See: Example 2

template void DLL ::insertDLL(T obj) { Node *temp = top; temp = new Node ; temp->next = top; temp->element = obj; temp->prev = NULL; if (top != NULL) top->prev = temp; top = temp; } template bool DLL ::searchDLL(T key) { Node *curr = top; while (curr != NULL) { if (curr->element == key) return true; curr = curr -> next; } return false; } template void DLL ::printDLL() { Node *curr=NULL; cout << endl; curr = top; while (curr != NULL) { cout element "; curr = curr->next; } cout << endl; } template void DLL ::destroyDLL(Node *&top) { Node *temp = top; while (top != NULL) { top = top->next; delete temp; temp = top; } top = NULL; }

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, …) ….

Therefore, a node may have different levels of pointers. The following shows an example of a skip list. This type of skip list is called evenly spaced skip list

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 > 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 22>16 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 17> 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 17>16 Since we cannot go any lower, we conclude that 16 is not in the list

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

Making the skip list unevenly spaced with multiple levels, insert becomes simple and restructuring is not required Deleting still needs a bit of work. When we delete a node x, we need to make sure that at all levels predecessors of node x are properly connected to the successors of node x For example if we delete node 17, we need to make sure that node 8 is connected to node 28, node 10 is connected to node 22 and node 12 is connected to node

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

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

Example of Move-to-front Method Suppose a set data A, B, C, D, and E are sent to the system. Using Move-to-front technique we insert an element if it is not in the list or move it to the front if it does already exist in the list: Suppose some data arrive in this order: A C B C D A D A C A C C E E ECADBACBDEE CADBEACBDEE CADBACBDC CADBACBDC ACDBACBDA CADBACBDC ADCBACBDA DACBACBDD A CABDACBDD CABACBC B AC C AAA Mover-to-front Method PlainElements

Example of Transpose Method Suppose a set data A, B, C, D, and E are sent to the system. Using Transpose technique we insert an element if it is not in the list or swap it with its predecessor if it is already in the list Suppose some data arrive in this order: A C B C D A D A C A C C E E CADEBACBDEE CADBEACBDEE CADBACBDC CADBACBDC ACDBACBDA CADBACBDC ACDBACBDA ACDBACBDD A CABDACBDD CABACBC B AC C AAA Transpose Method PlainElements

Example of Count Method Suppose a set data A, B, C, D, and E are sent to the system. Using Count technique we sort the elements based on the number of times that have been accessed Suppose some data arrive in this order: A C B C D A D A C A C C E E CAEDBACBDEE CADBEACBDEE CADBACBDC ACDBACBDC ACDBACBDA CADBACBDC ADCBACBDA DCABACBDD CABDACBDA CABDACBDD CABACBC B AC C AAA Count MethodPlainElements

Example of Ordering Method Suppose a set data A, B, C, D, and E are sent to the system. Using Ordering technique we sort the elements in ascending order (in this example) Suppose some data arrive in this order: A C B C D A D A C A C C E E ABCDEACBDEE ABCDEACBDEE ABCDACBDC ABCDACBDC ABCDACBDA ABCDACBDC ABCDACBDA ABCDACBDD ABCDACBDA ABCDACBDD ABCACBC ABCACBB AC C AAA Ordering Method PlainElements

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

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 student’s grades in array cells

F D C- C C+ B- B B+ A- A j i h g f e d c b a 8000…5206…402401… … … Students For example, this table shows that the student with Student- Id 5206 has received d ( grade B) in the course with course- Id 30

Assuming that every student on average can take five courses per semester. In this case, we would have = 295 empty cells per column Since there are 8000 columns, 8000*295 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

… … … b136 f 5207 f404 h124 g 404 e3 e31a 3 d3078a1 b5208 b …..115….321 StudentsInClasses ClassesTaken The ClassesTaken array shows that the student with Student-Id 5207 has taken 3 courses with course Ids 290, 115, and 116 and has received grades e, a, and d respectively. The StudentsInClasses array shows that course 115 have been taken by three students with students Ids 3078, 404, and These students have received grades a, e, and f in this course respectively.

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

d 1 2 b 3 2 e 2 30 a 1 31 a d h b f ….5208…5206…405404… …..… 30 … 2 1 Classes Students

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