03 ADT-List
03 ADT-List
03 ADT-List
1/14
§2 The List ADT
ADT:
Objects: ( item0, item1, , itemN1 )
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
3/14
§2 The List ADT
2. Linked Lists ptr
4/14
§2 The List ADT
ptr
Insertion
node
takes O(1) time.
a1 ... ai ai+1 ... an NULL
5/14
§2 The List ADT
ptr
Deletion
pre
takes O(1) time.
a1 ... ai ai+1 ... an NULL
6/14
§2 The List ADT
Doubly Linked Circular Lists
An empty list : H
7/14
§2 The List ADT
Two Applications
8/14
§2 The List ADT
【 Representation 1 】
typedef struct {
int CoeffArray [ MaxDegree + 1 ] ;
int HighPower;
} *Polynomial ;
9/14
§2 The List ADT
【 Representation 2 】
Given A( x ) a m 1 x em1 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 */
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];
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