The Class chain.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

COMP171 Fall 2005 Lists.
Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
CSE Lecture 12 – Linked Lists …
List Representation - Chain Fall 2010 CSE, POSTECH.
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.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
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.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Linear Lists – Linked List Representation CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
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.
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.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linear List Array representation Data structures A data structure is a particular way of storing and manipulating data in a computer so that it can be.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
SNU IDB Lab. Ch6. Linear Lists – Linked Representation © copyright 2006 SNU IDB Lab.
CSCS-200 Data Structure and Algorithms Lecture
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Dictionaries Collection of items. Each item is a pair. (key, element)
Reminder: Course Policy
Lectures linked lists Chapter 6 of textbook
The Class ArrayLinearList
Lists CS 3358.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
List Representation - Array
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Linked List Yumei Huo Department of Computer Science
The Class chain.
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Stacks template<class T> class stack { public:
Object Oriented Programming COP3330 / CGS5409
The Class arrayList General purpose implementation of linear lists.
Chapter 18: Linked Lists.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Stacks template<class T> class stack { public:
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Stacks template<class T> class stack { public:
Linked Representation
Doubly Linked List Implementation
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Stacks public interface Stack { public boolean empty();
The Class chain.
Data Structures & Algorithms
Stacks template<class T> class stack { public:
Programming II (CS300) Chapter 07: Linked Lists
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
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.
Data Structures & Programming
Presentation transcript:

The Class chain

Reminder: Chain firstNode NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL pointer.

next (datatype chainNode<T>*) The Class chain a b c d e NULL firstNode listSize = number of elements Use chainNode next (datatype chainNode<T>*) element (datatype T)

The Class chain template<class T> class chain : public linearList<T> { public: // constructors and destructor defined here   // ADT methods bool empty() const {return listSize == 0;} int size() const {return listSize;} // other ADT methods defined here protected: void checkIndex(int theIndex) const; chainNode<T>* firstNode; int listSize; };

Constructor template<class T> chain<T>::chain(int initialCapacity = 10) {// Constructor. if (initialCapacity < 1) {ostringstream s; s << "Initial capacity = " << initialCapacity << " Must be > 0"; throw illegalParameterValue(s.str()); } firstNode = NULL; listSize = 0;

The Destructor template<class T> chain<T>::~chain() {// Chain destructor. Delete all nodes // in chain. while (firstNode != NULL) {// delete firstNode chainNode<T>* nextNode = firstNode->next; delete firstNode; firstNode = nextNode; }

The Method get firstNode a b c d e template<class T> NULL a b c d e template<class T> T& chain<T>::get(int theIndex) const {// Return element whose index is theIndex. checkIndex(theIndex); // move to desired node chainNode<T>* currentNode = firstNode; for (int i = 0; i < theIndex; i++) currentNode = currentNode->next; return currentNode->element; }

The Method indexOf template<class T> int chain<T>::indexOf(const T& theElement) const { // search the chain for theElement chainNode<T>* currentNode = firstNode; int index = 0; // index of currentNode while (currentNode != NULL && currentNode->element != theElement) // move to next node currentNode = currentNode->next; index++; }  

The Method indexOf // make sure we found matching element   // make sure we found matching element if (currentNode == NULL) return -1; else return index; }

Erase An Element erase(0) deleteNode = firstNode; b c d e NULL firstNode erase(0) First do a checkIndex. deleteNode = firstNode; firstNode = firstNode->next; delete deleteNode;

Remove An Element template<class T> void chain<T>::erase(int theIndex) { checkIndex(theIndex);   chainNode<T>* deleteNode; if (theIndex == 0) {// remove first node from chain deleteNode = firstNode; firstNode = firstNode->next; }

Find & change pointer in beforeNode erase(2) firstNode null a b c d e beforeNode Find & change pointer in beforeNode beforeNode->next = beforeNode->next->next; delete deleteNode;

Remove An Element else { // use p to get to beforeNode chainNode<T>* p = firstNode; for (int i = 0; i < theIndex - 1; i++) p = p->next; deleteNode = p->next; p->next = p->next->next; } listSize--; delete deleteNode;

firstNode = new chainNode<char>(‘f’, firstNode); One-Step insert(0,’f’) a b c d e NULL firstNode f newNode firstNode = new chainNode<char>(‘f’, firstNode);

Insert An Element template<class T> void chain<T>::insert(int theIndex, const T& theElement) { if (theIndex < 0 || theIndex > listSize) {// Throw illegalIndex exception }   if (theIndex == 0) // insert at front firstNode = new chainNode<T> (theElement, firstNode);

Two-Step insert(3,’f’) beforeNode = firstNode->next->next; a b c d e NULL firstNode f newNode beforeNode beforeNode = firstNode->next->next; beforeNode->next = new chainNode<char> (‘f’, beforeNode->next);

Inserting An Element else { // find predecessor of new element chainNode<T>* p = firstNode; for (int i = 0; i < theIndex - 1; i++) p = p->next; // insert after p p->next = new chainNode<T> (theElement, p->next); } listSize++;

50,000 operations of each type Performance 50,000 operations of each type Start with an empty list and insert 50000 elements. Get each element once.

Performance 50,000 operations of each type Operation FastArrayLinearList Chain get 1.0ms 13.2sec best-case inserts 2.1ms 45.1ms average inserts 1.5sec 49.3sec worst-case inserts 2.5sec 12.9sec best-case removes 2.0ms 2.1ms average removes 1.5sec 68.8sec worst-case removes 2.5sec 12.9sec 1.7GHz Pentium 4 with 512MB. Worst-case times less than average because of cache effects (see explanation in text). Average inserts  inserts at random positions

Indexed AVL Tree (IAVL) Performance Indexed AVL Tree (IAVL) We will study this linked structure when we get to Chapter 16. This structure does not have an effective array representation.

Chain With Header Node a b c d e headerNode NULL Now insert/erase at left end (I.e., index = 0) are no different from any other insert/erase. So insert/erase code is simplified.

Empty Chain With Header Node NULL

Circular List a b c d e firstNode Useful, for example, when each node represents a supplier and you want each supplier to be called on in round-robin order. Other applications seen later. A variant of this has a header node—circular list with header node. When you don’t have a header node, it is often useful to have a last node pointer rather than a first node pointer. By doing this, additions to the front and end become O(1).

Doubly Linked List a b c d e firstNode lastNode NULL Can be used to reduce worst-case run time of a linear list add/remove/get by half. Start at left end if index <= listSize/2; otherwise, start at right end.

Doubly Linked Circular List firstNode

Doubly Linked Circular List With Header Node

Empty Doubly Linked Circular List With Header Node

The STL Class list Linked implementation of a linear list. Doubly linked circular list with header node. Has many more methods than chain. Similar names and signatures.

To-do Don’t forget assignment 2 Practice (Easy Challenges): https://www.hackerrank.com/domains/data-structures/linked-lists Read more on list: http://www.cplusplus.com/reference/list/list/ Don’t forget assignment 2