24
CS235102 CS235102 Data Structures Data Structures Chapter 4 Lists Chapter 4 Lists

CS235102 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

Embed Size (px)

Citation preview

Page 1: CS235102 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

CS235102 CS235102 Data StructuresData Structures

Chapter 4 ListsChapter 4 Lists

Page 2: CS235102 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

Chain (1/3)Chain (1/3) Chain:Chain:

A singly linked list in which the last node has a null linkA singly linked list in which the last node has a null link Operations for chainsOperations for chains

Inverting a chainInverting a chain For a list of For a list of lengthlength 1 nodes, the ≧ 1 nodes, the ≧ whilewhile loop is executed loop is executed lengtlengt

hh times and so the computing time is linear or O( times and so the computing time is linear or O( lengthlength).).

...

...

NULL

×NULL

lead

lead

invert

Page 3: CS235102 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

Chain Chain (2/3)(2/3)

......

NULLtrialtrialmiddlemiddleleadlead

Two extra pointers

middlemiddle leadlead

trialtrial

NULL

Page 4: CS235102 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

Chain (3/3)Chain (3/3) Concatenates two chainsConcatenates two chains Concatenates two chains, ptr1 and ptr2.Concatenates two chains, ptr1 and ptr2. Assign the list Assign the list

ptr1 followed ptr1 followed by the list ptr2.by the list ptr2.

O(length of list ptr1)

NULL

ptr1

NULL

ptr2

temp

Page 5: CS235102 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

Circularly Linked Lists (1/10)Circularly Linked Lists (1/10)

Circular Linked listCircular Linked list The link field of the last node points to the first The link field of the last node points to the first

node in the list.node in the list. ExampleExample

Represent a polynomial Represent a polynomial ptrptr = 3x = 3x1414+2x+2x88+1 as a +1 as a circularly linked list.circularly linked list.

ptr 3 14 2 8 1 0

Page 6: CS235102 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

Circularly Linked Lists (2/10)Circularly Linked Lists (2/10) Maintain an Available ListMaintain an Available List

We free nodes that are no longer in use so that we mWe free nodes that are no longer in use so that we may reuse these nodes lateray reuse these nodes later

We can obtain an efficient erase algorithm for circular We can obtain an efficient erase algorithm for circular lists, by maintaining our own list (as a chain) of nodes lists, by maintaining our own list (as a chain) of nodes that have been “freed”.that have been “freed”.

Instead of usingInstead of using mallocmalloc andand freefree, we now use get_no, we now use get_node (program 4.13) and ret_node (program 4.14).de (program 4.13) and ret_node (program 4.14).

availavail ... NULLNULL

List of freed nodes

Page 7: CS235102 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

Circularly Linked Lists (3/10)Circularly Linked Lists (3/10) Maintain an Available List (cont’d)Maintain an Available List (cont’d)

When we need a new node, we examine this list. When we need a new node, we examine this list. If the list is not empty, then we may use one of its nodes.If the list is not empty, then we may use one of its nodes.

Only when the Only when the

list is empty we list is empty we do need to use do need to use mallocmalloc to to create a new create a new node.node.

Page 8: CS235102 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

Circularly Linked Lists (4/10)Circularly Linked Lists (4/10) Maintain an Available List (cont’d)Maintain an Available List (cont’d) Insert ptr to the front of this list

Let Let availavail be a variable of type poly_pointer that points be a variable of type poly_pointer that points to the first node in our list of freed nodes.to the first node in our list of freed nodes.

Henceforth, we call this list the available space list or Henceforth, we call this list the available space list or availavail list. list.

Initially, we set Initially, we set availavail to to NULLNULL

Page 9: CS235102 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

Circularly Linked Lists (5/10)Circularly Linked Lists (5/10)

Maintain an Available ListMaintain an Available List Erase a circular list in a fixed Erase a circular list in a fixed

amount (amount (constantconstant) ) of time of time O(1) independent of the numbindependent of the number of nodeser of nodes in the list using in the list using cceraseerase

temp

ptr

NULLavail

紅色 link 所連接而成的 chain

Page 10: CS235102 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

Circularly Linked Lists (6/10)Circularly Linked Lists (6/10) We must handle the We must handle the zero polynomialzero polynomial as a special c as a special c

ase. To avoid it, we introduce a ase. To avoid it, we introduce a head nodehead node into eac into each polynomialh polynomial each polynomial, zero or nonzero, contains one additional each polynomial, zero or nonzero, contains one additional

node.node. The The expoexpon and n and coefcoef fields of this node are irrelevant. fields of this node are irrelevant.

Why ?

So !

Page 11: CS235102 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

Circularly Linked Circularly Linked Lists (7/10)Lists (7/10)

For fit the For fit the circular list with hcircular list with head node represead node representationentation We may remove We may remove

the test for (*ptr) fthe test for (*ptr) from rom cerasecerase

Changes the origiChanges the original nal paddpadd to to cpadcpaddd

/* head node */

/*a->expon=-1, so b->expont > -1 */

/* link to the first node */

Page 12: CS235102 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

Circularly Linked Lists (8/10)Circularly Linked Lists (8/10) Operations for circularly linked listsOperations for circularly linked lists

Question: What happens when we want to insert a new node

at the front of the circular linked list ptr?

Answer: move down the entire length of ptr.

Possible Solution:

ptr x1 x2 x3

x1 x2 x3 ptr

Page 13: CS235102 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

Circularly Linked Lists (9/10)Circularly Linked Lists (9/10)

x1 x2 x3 ptr

node

Insert a new nodInsert a new node at the front of e at the front of a circular lista circular list

To insert To insert nodenode a at the rear, we onlt the rear, we only need to add thy need to add the additional state additional statement ement *ptr = nod*ptr = nodee to the else cla to the else clause of use of insert_froinsert_frontnt

Page 14: CS235102 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

Circularly Linked Lists (10/10)Circularly Linked Lists (10/10)

Finding the length of a circular listFinding the length of a circular list

Page 15: CS235102 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

Equivalence Relations (1/6)Equivalence Relations (1/6) Reflexive RelationReflexive Relation

for any polygon for any polygon xx, , xx ≡ ≡ xx (e.g., (e.g., xx is electrically equivalent to itself) is electrically equivalent to itself)

Symmetric RelationSymmetric Relation for any two polygons for any two polygons xx and and yy, if , if xx ≡ ≡ yy, then , then yy ≡ ≡ xx..

Transitive RelationTransitive Relation for any three polygons for any three polygons xx, , yy, and , and zz, if , if xx ≡ ≡ yy and and yy ≡ ≡ zz, then , then xx ≡ ≡ zz..

Definition:Definition: A relation over a set, A relation over a set, SS, is said to be an , is said to be an equivalence relation ove ove

rr S iff S iff it is it is symmertric symmertric,, reflexive reflexive, and, and transitive transitive over over SS..

Example:Example: ““equal to” relationship is an equivalence relationequal to” relationship is an equivalence relation

Page 16: CS235102 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

Equivalence Relations (2/6)Equivalence Relations (2/6) Example:Example: if we have 12 polygons numbered 0 through 11if we have 12 polygons numbered 0 through 11

0 0 4 4, , 3 3 1 1, , 6 6 10 10, , 8 8 9 9, , 7 7 4 4, , 6 6 8 8, , 3 3 5 5, , 2 2 11 11, , 11 11 0 0

we can partition the twelve polygons into the following we can partition the twelve polygons into the following equivalence classes:equivalence classes:

{0, 2, 4, 7, 11};{1, 3, 5};{6, 8, 9,10}{0, 2, 4, 7, 11};{1, 3, 5};{6, 8, 9,10} Two phases to determine equivalenceTwo phases to determine equivalence

First phase: the equivalence pairs (First phase: the equivalence pairs (ii, , jj) are read in and ) are read in and stored.stored.

Second phase:Second phase: we begin at 0 and find all pairs of the form (0, we begin at 0 and find all pairs of the form (0, jj). ).

Continue until the entire equivalence class containing 0 has been Continue until the entire equivalence class containing 0 has been found, marked, and printed.found, marked, and printed.

Next find another object not yet output, and repeat Next find another object not yet output, and repeat the above process.the above process.

Page 17: CS235102 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

Equivalence Relation (3/6)Equivalence Relation (3/6) Program to find equivalence classesProgram to find equivalence classes

void main(void){short int out[MAX_SIZE];node_pointer seq[MAX_SIZE];node_pointer x, y, top;int i, j, n;printf(“Enter the size (<=%d) ”, MAX_SIZE);scanf(“%d”, &n);for(i=0; i<n; i++){

/*initialize seq and out */out[i] = TRUE; seq[i] = NULL;

}/* Phase 1 *//* Phase 2 */

}

typedef struct node *node_pointer;typedef struct node {

int data;node_pointer link;

};

#include <stdio.h>#define MAX_SIZE 24#define IS_FULL(ptr) (!(ptr))#define FALSE 0#define TRUE 1

Page 18: CS235102 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

Equivalence Relations (4/6)Equivalence Relations (4/6) Phase 1: Phase 1: read in and store the equivalence pairs <i, j>

Insert x to the top of lists seq[i]

Insert x to the top of lists seq[j]

(1) (2)

[11]

[10]

[9]

[8]

[7]

[6]

[5]

[4]

[3]

[2]

[1]

[0]

0 4, 3 1, 6 10, 8 9, 7 4, 6 8, 3 5, 2 11, 11 0

4 NULL

0 NULL

1 NULL

3 NULL

10 NULL

6 NULL

9 NULL

8 NULL

4 NULL

7

8

6

5

3 NULL

11 NULL

2 NULL0

11

Page 19: CS235102 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

Equivalence Relations (5/6)Equivalence Relations (5/6)

Phase 2: begin at 0 and find all pairs of the form <0, begin at 0 and find all pairs of the form <0, jj>, where 0 >, where 0

and and jj are in the same equivalence class are in the same equivalence class by transitivity, all pairs of the form <by transitivity, all pairs of the form <jj, , kk> imply that > imply that kk

in the same equivalence class as 0in the same equivalence class as 0 continue this way until we have found, marked, and continue this way until we have found, marked, and

printed the entire equivalent class containing 0printed the entire equivalent class containing 0

Page 20: CS235102 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

Equivalence Relations (6/6)Equivalence Relations (6/6)

Phase 2Phase 2

[11]

[10]

[9]

[8]

[7]

[6]

[5]

[4]

[3]

[2]

[1]

[0] 4 NULL

0 NULL

1 NULL

3 NULL

10 NULL

6 NULL

9 NULL

8 NULL

4 NULL

7

8

6

5

3 NULL

11 NULL

2 NULL0

11

New class: 0

i=[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

out:

x

top j= 11y

11

4

4

7

7

0402

2

1101

Page 21: CS235102 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

Doubly Linked Lists (1/4)Doubly Linked Lists (1/4) Singly linked lists pose problems because we caSingly linked lists pose problems because we ca

n move easily only in the direction of the linksn move easily only in the direction of the links

Doubly linked list has at least three fieldsDoubly linked list has at least three fields left link field(left link field(llinkllink), data field(), data field(itemitem), right link field(), right link field(rlinkrlink).). The necessary declarations:The necessary declarations:

typedef struct node *node_pointer;typedef struct node *node_pointer;

typedef struct node{typedef struct node{

node_pointer llink;node_pointer llink;

element item;element item;

node_pointer rlink;node_pointer rlink;

};};

... NULL

ptr?

Page 22: CS235102 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

Doubly Linked Lists (2/4)Doubly Linked Lists (2/4) SampleSample

doubly linked circular with head node: (Figure 4.23)doubly linked circular with head node: (Figure 4.23)

empty double linked circular list with head node (Fiempty double linked circular list with head node (Figure 4.24)gure 4.24)

suppose that suppose that ptr ptr points to any node in a doubly linkpoints to any node in a doubly linked list, then:ed list, then: ptr = ptr -> llink -> rlink = ptr -> rlink -> llinkptr = ptr -> llink -> rlink = ptr -> rlink -> llink

Page 23: CS235102 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

Doubly Linked Lists (3/4)Doubly Linked Lists (3/4) Insert nodeInsert node

Head node

llink item rlink

New node

node

Page 24: CS235102 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

Doubly Linked Lists (4/4)Doubly Linked Lists (4/4) Delete nodeDelete node

Head node

llink item rlink

deleted node