48
Linked Lists Chapter 4 1

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

Embed Size (px)

Citation preview

Linked Lists

Linked ListsChapter 41Why linked lists2IndexWordA[0]BATA[1]CATA[2]EATA[n]WATInsert FAT ?Or delete somethingUsual way to draw a linked list3

Inserting into a linked list4

Delete GAT5

Defining a node in C++class ThreeLetterNode { private: char data[3]; ThreeLetterNode * link; }6Example 4.1: The class definitionsclass 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 1Use a global variable first which is a pointer of ThreeLetterNode.Unable to access to private data members: data and link.ThreeLetterNode *first; firstdata, firstlink firstdata[0], firstdata[1], firstdata[2]8

Designing a chain class in C++: Design attempt 2Make data members public or define public member functions GetLink(), SetLink() and GetData()Defeat the purpose of data encapsulationThe 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 ThreeLetterNode9Designing a chain class in C++: Design attempt 3Use of two classes.Create a class that represents the linked list.The class contains the items of another objects of another class.HAS-AA 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 Page1011ThreeLetterChain

Conceptual relationship between ThreeLetterChain and ThreeLetterNodeActual relationship between ThreeLetterChain and ThreeLetterNodeProgram 4.1: Composite classesclass ThreeLetterChain; // forward declarationclass ThreeLetterNode {friend class ThreeLetterChain;private:char data[3];ThreeLetterNode *link;};class ThreeLetterChain {public:// Chain manipulation operations..private:ThreeLetterNode *first;};12Program 4.2: Nested classesclass ThreeLetterChain {public:// Chain Manipulation operations..private:class ThreeLetterNode { // nested classpublic:char data[3];ThreeLetterNode *link;};ThreeLetterNode *first;};13Pointer manipulation in C++Two pointer variables of the same type can be compared.Eg. x == y, x != y, x == 014

x = y

*x = *yProgram 4.3: Creating a two-node listvoid Chain::Create2(){// create and set fields of second nodeChainNode* second = new ChainNode(20,0);// create and set fields of first nodefirst = new ChainNode(10,second);}15

Program 4.4: Inserting a nodevoid Chain::Insert50(ChainNode* x){if ( first )// insert after xxlink = new ChainNode(50, xlink);else// insert into empty listfirst = new ChainNode(50);}16

Program 4.5: Deleting a nodevoid Chain::Delete(ChainNode* x, ChainNode *y){if(x = = first) first = firstlink;else ylink = xlink;delete x;}17

Example:

Delete thesecond node

Delete thefirst node

Reference: J.L. Huang@NCTUProgram 4.6: Template definition of chainstemplate < class T > class Chain; template < class T >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;}18IteratorAn iterator is an object that is used to access the elements of a container class one by one19void main( ){int x [3] = {0,1,2};// use a pointer y to iterate the array xfor (int* y = x; y != x+3; y + +)cout > n; // read number of objects // initialize first and out ENode **first = new ENode* [n]; bool *out = new bool[n]; // use STL function fill to initializefill (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