03 ADT-List

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

CHAPTER 3

Lists, Stacks, and Queues

§1 Abstract Data Type (ADT)


【 Definition 】 Data Type = { Objects }  { Operations }

〖 Example 〗 int = { 0, 1, 2,   , INT_MAX, INT_MIN }


 { , , , , ,    }

【 Definition 】 An Abstract Data Type (ADT) is a data


type that is organized in such a way that the
specification on the objects and specification of the
operations on the objects are separated from the
representation of the objects and the implementation
on the operations.

1/14
§2 The List ADT
 ADT:
Objects: ( item0, item1,  , itemN1 )
Operations:
 Finding the length, N, of a list. Why after?
 Printing all the items in a list.
 Making an empty list.
 Finding the k-th item from a list, 0  k < N.
 Inserting a new item after the k-th item of a list, 0  k < N.
 Deleting an item from a list.
 Finding next of the current item from a list.
 Finding previous of the current item from a list.

2/14
§2 The List ADT
1. Simple Array implementation of Lists

array[ i ] = itemi Address Content


…… ……
array+i itemi
Sequential mapping array+i+1 itemi+1
…… ……

 MaxSize has to be estimated.


 Find_Kth takes O(1) time.
 Insertion and Deletion not
only take O(N) time, but also
involve a lot of data movements
which takes time.

3/14
§2 The List ADT
2. Linked Lists ptr

Address Data Pointer ZHAO QIAN


0010 SUN 1011
0011 QIAN 0010 SUN LI NULL
0110 ZHAO 0011
1011 LI NULL To link ‘ZHAO’ and ‘QIAN’:

Head pointer ptr = 0110 list_ptr N1, N2 ;


N1 = (list_ptr)malloc(sizeof(struct list_node));
N2 = (list_ptr)malloc(sizeof(struct list_node));
Locations of the nodes may
Initialization: N1->data = ‘ZHAO’ ;
change onN2->data
different runs.
= ‘QIAN’ ;
N1->next = N2 ;
typedef struct list_node *list_ptr; N2->next = NULL ;
typedef struct list_node { ptr = N1 ;
char data [ 4 ] ;
list_ptr next ;
};
list_ptr ptr ; ptr
ZHAO QIAN NULL

4/14
§2 The List ADT

ptr
Insertion
node
 takes O(1) time.
a1 ... ai ai+1 ... an NULL

 temp->next = b  node->next = temp


node->next
temp

Question: What will happen


if the order of the two steps is reversed?

Question: How can we insert a new first item?

5/14
§2 The List ADT

ptr
Deletion
pre
 takes O(1) time.
a1 ... ai ai+1 ... an NULL

 pre->next = b  free ( node )


node->next
node
Question: How can we
delete the first node from a list?

Answer: We can add a dummy


head node to a list.

Read programs in Figures 3.6-3.15


for detailed implementations of operations.

6/14
§2 The List ADT
Doubly Linked Circular Lists

typedef struct node *node_ptr ; llink rlink


typedef struct node {  item 
node_ptr llink; Uhhh ... Then I’ll have to
element item; Don’t
ptr = we have
ptr->llink->rlink
go from the 1st node again.
node_ptr rlink; enough headache already?
}; But hey, why do I=wantta
ptr->rlink->llink
Why do we need
Suppose youI’ll
find the have
previousa list
node?
Why do you ask me? :-) thelists?
the go
doubly from
linked 1st node
A doubly linked circular 1->2->3->…->m.
list with to the
head m-th node.
node:
Maybe you wantta delete
Nowyou
Then howare
would
askedyouto find
the m-th node?
itsget the m-th
previous m  1?
node?
node
H    item1   item2   item3 

An empty list : H  

7/14
§2 The List ADT
Two Applications

 The Polynomial ADT


Objects : P ( x ) = a1 x e1 +  + an x en ; a set of ordered pa
irs of < ei , ai > where ai is the coefficient and ei
is the exponent. ei are nonnegative integers.
Operations:
 Finding degree, max { e }, of a polynomial.
i

 Addition of two polynomials.


 Subtraction between two polynomials.
 Multiplication of two polynomials.
 Differentiation of a polynomial.

8/14
§2 The List ADT
【 Representation 1 】
typedef struct {
int CoeffArray [ MaxDegree + 1 ] ;
int HighPower;
} *Polynomial ;

Try to apply MultPolynomial (p.47)


I like it!*NIt’s
O(What easy to complexity
Really?
On P1(x) =N110x is) the
21000 +5x14time
+1 and
forimplement
finding
What’s
P (x)
most
the
wrong
= 3x
of the
product
1990with
2x of operations,
that?
1492 two polynomials
+11x+5
suchof
2 asdegree
Add and N1 Multiplication.
and N2?
-- now do you see my point?

9/14
§2 The List ADT
【 Representation 2 】
Given A( x )  a m 1 x em1    a0 x e0
: em 1  em  2    e0  0 and ai  0 for i  0, 1,  , m  1.
where
We represent each term as a node Coefficient Exponent Next 

Declaration:
typedef struct poly_node *poly_ptr;
struct poly_node {
int Coefficient ; /* assume coefficients are integers */
int Exponent;
poly_ptr Next ;
};
typedef poly_ptr a ; /* nodes sorted by exponent */

a am1 em1  …… a0 e0 NULL

10/14
§2 The List ADT
 Multilists
〖 Example 〗 Suppose that we have 40,000 students and
2,500 courses. Print the students’ name list for each course,
and print the registered classes’ list for each student.

【 Representation 1 】
int Array[40000][2500];

 1 if student i is registered for course j


Array[i ][ j ]  
 0 otherwise

11/14
§2 The List ADT
【 Representation 2 】

S1 S2 S3 S4 S5

C1

C2

C3

C4

12/14
§2 The List ADT
3. Cursor Implementation of Linked Lists (no pointer)
 Features that a linked list must have:
a) The data are stored in a collection of structures. Each struct
ure contains data and a pointer to the next structure.
b) A new structure can be obtained from the system’s global m
emory by a call to malloc and released by a call to free.

0 1 2 …… S-1
Cursor Element
Space Next 1 2 3 S-1 0

Note:
Note:The
Theinterface
interfacefor
forthe
thecursor
cursorimplementation
implementation(given
(givenin
in
Figure
Figure3.27
3.27on
onp.
p.52)
52)isisidentical
identicalto
tothe
thepointer
pointer
implementation
implementation(given
(givenin inFigure
Figure3.6
3.6on
onp.
p.40).
40).

13/14
§2 The List ADT
0 1 2 …… S-1
Element
Next 2x 5 S-2 0

p
malloc: p = CursorSpace[ 0 ].Next ;
CursorSpace[ 0 ].Next = CursorSpace[ p ].Next ;

0 1 2 …… S-1
Element
Next p
2 5 S-2 2 0

p
free(p): CursorSpace[ p ].Next = CursorSpace[ 0 ].Next ;
CursorSpace[ 0 ].Next = p ;

Note:
Note:TheThecursor
cursorimplementation
implementation
Read operation
isisusually
usuallysignificantly
significantlyfaster
faster
implementations given in
because
becauseofofthe
thelack
lackof
ofmemory
memory
Figures 3.31-3.35
management
managementroutines.
routines.
14/14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy