linked lists n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Linked Lists PowerPoint Presentation
Download Presentation
Linked Lists

Loading in 2 Seconds...

play fullscreen
1 / 48

Linked Lists - PowerPoint PPT Presentation


  • 78 Views
  • Uploaded on

Linked Lists. Chapter 4. Why “linked lists”. Insert “FAT” ? Or delete something. Usual way to draw a linked list. Inserting into a linked list. Delete GAT. Defining a node in C++. class ThreeLetterNode { private: char data[3]; ThreeLetterNode * link; }.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Linked Lists' - vidor


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
linked lists

Linked Lists

Chapter 4

why linked lists
Why “linked lists”

Insert “FAT” ?

Or delete something

defining a node in c
Defining a node in C++
  • class ThreeLetterNode

{

private:

char data[3];

ThreeLetterNode* link;

}

designing a chain class in c design attempt 1
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]

designing a chain class in c design attempt 2
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
designing a chain class in c design attempt 3
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
slide11

Conceptual relationship between ThreeLetterChain and ThreeLetterNode

Actual relationship between ThreeLetterChain and ThreeLetterNode

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

x = y

*x = *y

program 4 5 deleting a node
Program 4.5: Deleting a node

Example:

Delete the

second node

Delete the

first node

Reference: J.L. Huang@NCTU

iterator
Iterator
  • An iterator is an object that is used to access the elements of a container class one by one
program 4 13 reversing a list
Program 4.13: Reversing a list

template <class T>

void Chain<T>::Reverse()

{// A chain is reversed so that (a1, …, an)becomes(an, …, a1)

ChainNode<T> *current = first,

*previous = 0; // previoustrails current

while (current) {

ChainNode<T> *r = previous;

previous = current; // rtrailsprevious

current = current  link; // currentmoves to next node

previouslink = r; // link previousto preceding node

}

first = previous;

}

circular lists
Circular lists

How to insert at the front of a circular list?

Read Program 4.14

linked stacks and queues
Linked stacks and queues

Read Program 4.19, 4.20, 4.21 and 4.22

program 4 23 polynomial class definition
Program 4.23: Polynomial class definition

structTerm

{

intcoef;

intexp;

Term Set(intc,inte) {coef = c;exp = e;return *this;};

};

classPolynomial {

public:

// public functions defined here

private:

Chain<Term> poly;

};

polynomial representation
Polynomial representation

(a) 3x14+2x8+1 (b) 8x14-3x10+10x6

equivalence classes
Equivalence classes
  • For an arbitrary relation by the symbol 
  • Reflexive
    • xx
  • Symmetric
    • If xy, then yx
  • Transitive
    • If xyand 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.
example
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}
program 4 26 first version of equivalence algorithm
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;

}

program 4 27 a more detail version of equivalence algorithm
Program 4.27: A more detail version of equivalence algorithm

voidEquivalence()

{

readn;

initializefirst[0:n-1] to0and out[0:n-1] tofalse;

whilemore pairs

{

read the next pair(i, j);

put jon the chainfirst[i];

put i on the chainfirst[j];

}

for (i = 0;i < n;i++)

if (!out[i]) {

out[i] = true;

output the equivalence class that contains objecti;

}

}

lists after pairs have been input
Lists after pairs have been input

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
Program 4.28: C++ function to find equivalence classes

classENode{

friendvoidEquivalence( );

public:

ENode(intd = 0)

{data = d; link = 0;}

private:

intdata;

ENode *link;

};

slide36

voidEquivalence( )

{// input and output

ifstreaminFile( "equiv.in", ios::in);

if (!inFile) throw “Cannot open input file.”;

inti, j, n;

inFile >> n; // read number of objects

// initializefirstandout

ENode **first = newENode* [n];

bool *out = newbool[n];

// use STL functionfillto initialize

fill (first, first + n, 0);

fill (out, out + n, false);

slide37

// Phase 1: input equivalence pairs

inFile >>i >> j;

while (inFile.good()) { //check end of file

first[i] = newENode(j, first[i])

first[j] = newENode(i, first[j])

inFile >>i >> j;

}

slide38

// 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;

}

elsex = xlink;

}

if (!top) break;

x = first [topdata];

top = toplink; // unstack

} // end of while(1)

slide39

}

for (i = 0; i < n;i++)

while (first[i]) {

ENode *delnode = first[i];

first[i] = delnodelink;

deletedelnode;

}

delete [] first;delete [] out;

}

sparse matrices
Sparse matrices

1 0 4

Next nonzero column

Next nonzero row

example1
Example

Thus for an nxmsparse matrix with r nonzero terms, the number of nodes needed is

max{n, m} + r + 1.

program 4 29 class definition for sparse matrices
Program 4.29: Class definition for sparse matrices

structTriple{introw, col, value;};

classMatrix;

classMatrixNode{

friendclassMatrix;

friendistream&operator>>(istream&, Matrix&);

private:

MatrixNode *down , *right;

boolhead;

union {

MatrixNode *next;

Tripletriple;

};

MatrixNode(bool, Triple*); // constructor

}

slide43

MatrixNode::MatrixNode(boolb, Triple *t) // constructor

{

head = b;

if (b) {right = down = this;} // row/column header node

elsetriple = *t;

// element node or header node for list of header nodes

}

classMatrix{

friendistream&operator>>(istream&, Matrix&);

public:

~Matrix(); // destructor

private:

MatrixNode *headnode;

};

doubly linked lists
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)
doubly linked circular list with header node
Doubly linked circular list with header node

Empty doubly linked circular list with header node

program 4 32 class definition of a doubly linked list
Program 4.32: Class definition of a doubly linked list

class DblList;

class DblListNode {

friend class DblList;

private:

intdata;

DblListNode*left,*right;

};

class DblList {

public:

// List manipulation operations

.

.

private:

DblListNode*first; // points to header node

};

program 4 33 deletion from a doubly linked circular list
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;

}

Reference: J.L. Huang@NCTU

program 4 34 insertion into a doubly linked circular list
Program 4.34: Insertion into a doubly linked circular list

void DblList:: Insert(DblListNode *p, DblListNode *x)

{ // insert node pto the right of nodex

p→left = x; //(1)

p→right = x→right; //(2)

x→right→left = p; //(3)

x→right= p; //(4)

}

Reference: J.L. Huang@NCTU