Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Chapter 4 Lists Pointers Singly Linked Lists
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
CSE Lecture 12 – Linked Lists …
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Chapter 4.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Data Structures Using C++ 2E
Linear Lists – Linked List Representation CSE, POSTECH.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Introduction to Data Structure
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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)
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Linked List Containers. Linked List Example Polynomials –Interested in representing and manipulating polynomials –Polynomial defined as: Y = coef_n *
Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Data Structures Chapter 4: Linked Lists 4-1. Singly Linked List DABC 561 data link(pointer, address) 以 -1 代表結尾 (null pointer) first = 2 header,
Chap 4 Linked Lists. Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
Chapter 4 (cont.) Additional Lists Operations Circular Lists The link field of the last node points to the first node in the list... BATCATFATWAT.
More Linking Up with Linked Lists Chapter 11 5/19/2015 Adopted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
L.Chen Chapter 3 DATA REPRESENTATION(3) L.Chen 3.8 APPLICATIONS Bin Sort Bin Sort (箱子排序)  It is a faster way to accomplish.
Linked List Containers. Concatenate Example Concatenate(LinkedList listB): Add all of the elements of a second list to the end of the first list Concatenate(LinkedList.
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.
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
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.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Linked List Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
C++ Programming:. Program Design Including
CS505 Data Structures and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
EEE2108: Programming for Engineers Chapter 4. Linked Lists
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CH4. LINKED LISTS.
ليست هاي پيوندي.
Linked List (Part I) Data structure.
Chapter 16 Linked Structures
Pointers & Dynamic Data Structures
Data Structures & Algorithms
Data Structures Chapter 4: Linked Lists.
Data Structures & Programming
Presentation transcript:

Chapter 4 1

Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something

Usual way to draw a linked list 3

Inserting into a linked list 4

Delete GAT 5

Defining a node in C++ class ThreeLetterNode { private: char data[3]; ThreeLetterNode * link; } 6

Example 4.1: The class definitions class NodeA { private: int data1; char data2; float data3; NodeA *linka; NodeB *linkb; }; class NodeB { private: int data; NodeB *link; }; 7

Designing a chain class in C++: Design attempt 1 Use a global variable first which is a pointer of ThreeLetterNode. Unable to access to private data members: data and link. ThreeLetterNode *first; first  data, first  link first  data[0], first  data[1], first  data[2] 8

Designing a chain class in C++: Design attempt 2 Make data members public or define public member functions GetLink(), SetLink() and GetData() Defeat the purpose of data encapsulation The ideal solution would only permit those functions that perform list manipulation operations (like inserting a node into or deleting a node from a chain) access to the data members of ThreeLetterNode 9

Designing a chain class in C++: Design attempt 3 Use of two classes. Create a class that represents the linked list. The class contains the items of another objects of another class. HAS-A A data object of type A HAS-A data object of type B if A conceptually contains B or B is a part of A. Eg. Computer HAS-A Processor, Book HAS-A Page 10

11 ThreeLetterChain Conceptual relationship between ThreeLetterChain and ThreeLetterNode Actual relationship between ThreeLetterChain and ThreeLetterNode

Program 4.1: Composite classes class ThreeLetterChain; // forward declaration class ThreeLetterNode { friend class ThreeLetterChain; private: char data[3]; ThreeLetterNode *link; }; class ThreeLetterChain { public: // Chain manipulation operations.. private: ThreeLetterNode *first; };}; 12

Program 4.2: Nested classes class ThreeLetterChain { public: // Chain Manipulation operations.. private: class ThreeLetterNode { // nested class public: char data[3]; ThreeLetterNode *link; }; ThreeLetterNode *first; };}; 13

Pointer manipulation in C++ Two pointer variables of the same type can be compared. Eg. x == y, x != y, x == 0 14 x = y*x = *y

Program 4.3: Creating a two-node list void Chain::Create2() { // create and set fields of second node ChainNode* second = new ChainNode(20,0); // create and set fields of first node first = new ChainNode(10,second); } 15

Program 4.4: Inserting a node void Chain::Insert50(ChainNode* x) { if ( first ) // insert after x x→link = new ChainNode(50, x→link); else // insert into empty list first = new ChainNode(50); } 16

Program 4.5: Deleting a node void Chain::Delete(ChainNode* x, ChainNode *y) { if(x = = first) first = first→link; else y→link = x→link; delete x; } 17 Example: Delete the second node Delete the first node Reference: J.L.

Program 4.6: Template definition of chains template class Chain; template class ChainNode { friend class Chain ; private: T data; ChainNode * link; }; template class Chain { public: Chain( ) {first = 0;} // initializing first to 0 // Chain manipulation operations.. private: ChainNode * first; } 18

Iterator An iterator is an object that is used to access the elements of a container class one by one 19 void main( ) { int x [3] = {0,1,2}; // use a pointer y to iterate the array x for (int* y = x; y != x+3; y + +) cout << *y << “ ”; cout << endl; }

Program 4.11: Inserting at the back of a list template void Chain ::InsertBack(const T& e) { if (first) { // nonempty chain last→link = new ChainNode (e); last = last→link; } else first = last = new ChainNode (e); } 20

Program 4.12: Concatenating two chains template void Chain ::Concatenate(Chain & b) { // b is concatenated to the end of *this if ( first ) { last→link = b. first; last = b.last; } else { first = b. first; last = b.last; } b. first = b.last = 0; } 21

Program 4.13: Reversing a list template void Chain ::Reverse() {// A chain is reversed so that (a 1, …, a n ) becomes (a n, …, a 1 ) ChainNode *current = first, *previous = 0; // previous trails current while (current) { ChainNode *r = previous; previous = current; // r trails previous current = current  link; // current moves to next node previous  link = r; // link previous to preceding node } first = previous; } 22

Circular lists 23 How to insert at the front of a circular list? Read Program 4.14

Linked stacks and queues 24 Read Program 4.19, 4.20, 4.21 and 4.22

Program 4.23: Polynomial class definition struct Term { int coef; int exp; Term Set(int c,int e) {coef = c; exp = e; return *this;}; }; class Polynomial { public: // public functions defined here private: Chain poly; }; 25

Polynomial representation 26 (a) 3x 14 +2x 8 +1(b) 8x 14 -3x x 6

Adding polynomials: 3x 14 +2x 8 +1 and 8x 14 -3x x 6 27

28

29

Equivalence classes For an arbitrary relation by the symbol  Reflexive x  x Symmetric If x  y, then y  x Transitive If x  y and y  z, then x  z A relation over a set, S, is said to be an equivalence relation over S if and only if it is symmetric, reflexive, and transitive over S. 30

Example 0  4, 3  1, 6  10, 8  9, 7  4, 6  8, 3  5, 2  11, 11  0 Three equivalent classes: {0,2,4,7,11}; {1,3,5}; {6,8,9,10} 31

Program 4.26: First version of equivalence algorithm void Equivalence() { initialize; while more pairs { input the next pair (i,j); process this pair; } initialize for output; for(each object not yet output) output the equivalence class that contains his object; } 32

Program 4.27: A more detail version of equivalence algorithm void Equivalence() { read n; initialize first[0:n-1] to 0 and out[0:n-1] to false; while more pairs { read the next pair (i, j); put j on the chain first[i]; put i on the chain first[j]; } for (i = 0; i < n; i++) if (!out[i]) { out[i] = true ; output the equivalence class that contains object i; } 33

Lists after pairs have been input 34 0  4, 3  1, 6  10, 8  9, 7  4, 6  8, 3  5, 2  11, 11  0

Program 4.28: C++ function to find equivalence classes class ENode { friend void Equivalence( ); public: ENode(int d = 0) {data = d; link = 0;} private: int data; ENode *link; }; 35

void Equivalence( ) {// input and output ifstream inFile( "equiv.in", ios::in); if (!inFile) throw “Cannot open input file.”; int i, j, n; inFile >> n; // read number of objects // initialize first and out ENode **first = new ENode* [n]; bool *out = new bool[n]; // use STL function fill to initialize fill (first, first + n, 0); fill (out, out + n, false); 36

// Phase 1: input equivalence pairs inFile >> i >> j; while (inFile.good()) { // check end of file first[i] = new ENode (j, first[i]) first[j] = new ENode (i, first[j]) inFile >> i >> j; } 37

// Phase 2: output equivalence classes for (i = 0; i < n; i++) if (!out[i]) { // needs to be output cout << endl << "A new class: " << i; out[i] = true; ENode *x = first[i]; ENode *top = 0; // initialize stack while (1) { //find rest of class while (x) { // process the list j = x  data; if (!out[j]) { cout << ", " << j; out[j] = true; ENode *y = x  link; x  link = top; top = x; x = y; } else x = x  link; } if (!top) break; x = first [top  data]; top = top  link; // unstack } // end of while(1) 38

} for (i = 0; i < n; i++) while (first[i]) { ENode *delnode = first[i]; first[i] = delnode  link; delete delnode; } delete [] first; delete [] out; } 39

Sparse matrices Next nonzero row Next nonzero column

Example 41 Thus for an nxm sparse matrix with r nonzero terms, the number of nodes needed is max{n, m} + r + 1.

Program 4.29: Class definition for sparse matrices struct Triple{int row, col, value;}; class Matrix; class MatrixNode { friend class Matrix; friend istream& operator>>(istream&, Matrix&); private: MatrixNode *down, *right; bool head; union { MatrixNode *next; Triple triple; }; MatrixNode(bool, Triple*); // constructor } 42

MatrixNode::MatrixNode(bool b, Triple *t) // constructor { head = b; if (b) {right = down = this;} // row/column header node else triple = *t; // element node or header node for list of header nodes } class Matrix{ friend istream& operator>>(istream&, Matrix&); public: ~Matrix(); // destructor private: MatrixNode *headnode; }; 43

Doubly linked lists Move in forward and backward direction. How to get the preceding node during deletion or insertion? Using 2 pointers Node in doubly linked list left link field (left link) data field (item) right link field (right link) 44

Doubly linked circular list with header node 45 Empty doubly linked circular list with header node

Program 4.32: Class definition of a doubly linked list class DblList; class DblListNode { friend class DblList; private: int data; DblListNode *left, *right; }; class DblList { public: // List manipulation operations. private: DblListNode *first; // points to header node }; 46

Program 4.33: Deletion from a doubly linked circular list void DblList :: Delete(DblListNode *x) { if (x = = first) throw "Deletion of header node not permited"; else { x → left → right = x → right; //(1) x → right → left = x → left; //(2) delete x; } 47 Reference: J.L.

Program 4.34: Insertion into a doubly linked circular list void DblList :: Insert(DblListNode *p, DblListNode *x) { // insert node p to the right of node x p → left = x; //(1) p → right = x → right; //(2) x → right → left = p; //(3) x → right = p; //(4) } 48 Reference: J.L.