The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
COMP171 Fall 2005 Lists.
Linear Lists – Array Representation
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
List Representation - Chain Fall 2010 CSE, POSTECH.
Linked List
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 A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the list.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
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 Lists – Linked List Representation CSE, POSTECH.
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
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)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
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.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table 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
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
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.
CSCE 210 Data Structures and Algorithms
Reminder: Course Policy
CS505 Data Structures and Algorithms
Lists CS 3358.
Abstract Data Types data object set or collection of instances
Programming Abstractions
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.
Chapter 16-2 Linked Structures
The Class arrayList General purpose implementation of linear 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.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
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.
CMSC 341.
Linked Representation
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.
CMSC 341.
The Class chain.
The Class chain.
Data Structures & Algorithms
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.
Presentation transcript:

The Template Class Chain

Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.

The Class Chain link (datatype ChainNode *) data (datatype T) Use ChainNode abcde NULL first

The Template Class Chain template class Chain { public: Chain() {first = 0;} // constructor, empty chain ~Chain(); // destructor bool IsEmpty() const {return first == 0;} // other methods defined here private: ChainNode * first; };

The Destructor template chain ::~chain() {// Chain destructor. Delete all nodes // in chain. while (first != NULL) {// delete first ChainNode * next = first->link; delete first; first = next; }

The Method IndexOf template int Chain ::IndexOf(const T& theElement) const { // search the chain for theElement ChainNode * currentNode = first; int index = 0; // index of currentNode while (currentNode != NULL && currentNode->data != theElement) { // move to next node currentNode = currentNode->next; index++; }

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

Delete An Element delete(0) abcde NULL first deleteNode = first; first = first  link; delete deleteNode;

Delete An Element template void Chain ::Delete(int theIndex) { if (first == 0) throw “Cannot delete from empty chain”; ChainNode * deleteNode; if (theIndex == 0) {// remove first node from chain deleteNode = first; first = first->link; }

Delete(2) Find & change pointer in beforeNode beforeNode  link = beforeNode  link  link; delete deleteNode; beforeNode abcde null first

Delete An Element else { // use p to get to beforeNode ChainNode * p = first; for (int i = 0; i < theIndex - 1; i++) {if (p == 0) throw “Delete element does not exist”; p = p->next;} deleteNode = p->link; p->link = p->link->link; } delete deleteNode; }

One-Step Insert(0,’f’) abcde NULL first f newNode first = new ChainNode (‘f’, first);

Insert An Element template void Chain ::Insert(int theIndex, const T& theElement) { if (theIndex < 0) throw “Bad insert index”; if (theIndex == 0) // insert at front first = new chainNode (theElement, first);

Two-Step Insert(3,’f’) beforeNode = first  link  link; beforeNode  link = new ChainNode (‘f’, beforeNode  link); abcde NULL first f newNode beforeNode c

Inserting An Element else { // find predecessor of new element ChainNode * p = first; for (int i = 0; i < theIndex - 1; i++) {if (p == 0) throw “Bad insert index”; p = p->next;} // insert after p p->link = new ChainNode (theElement, p->link); }