Professional Documents
Culture Documents
Lecture 4
Lecture 4
Lecture 4
1
// ARRAY-BASED LIST ( list.h )
const int MAX_LENGTH = 50 ;
typedef int ItemType ;
How to Implement a List
class SortedList
{
public : // public member functions
use a built-in array stored in contiguous memory
SortedList ( ) ; // constructor locations, implementing operations Insert and
bool IsEmpty ( ) const ;
bool IsFull ( ) const ;
Delete by moving list items around in the array,
int Length ( ) const ; // returns length of list as needed
void Insert ( ItemType item ) ;
void Delete ( ItemType item ) ;
bool IsPresent( ItemType item ) const ;
use a linked list (to avoid excessive data
void Print ( ) ; movement from insertions and deletions) not
necessarily stored in contiguous memory
private : // private data members
locations
int length ; // number of values currently stored
ItemType data[MAX_LENGTH] ;
void BinSearch ( ItemType item, bool& found, int& position ) const ;
}; 7
00 11 22 33 44 55 66 77
12
12 33 33
33 77 22
MAX_SIZE
MAX_SIZE == 88
size
size == 00
head
head == -1
-1 Tail
tail
tail == 00
vv
40
40
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
12
12 33 33
33 77 22 40
40 12
12 33 33
33 77 22 40
40
vv
22
22
if
if list
list is
is not
not full
full Tail tail
list[tail]
list[tail] == vv
tail++
tail++
size++
size++
2
List Implementation Via Arrays List Implementation via Arrays
insertBeginning(22) insertBeginning(22)
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
12
12 33 33
33 77 22 40
40 40
40 12
12 33 33
33 77 22 22 40
40
vv vv
22
22 22
22
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
12
12 33 33
33 77 77 22 40
40 12
12 33 33
33 33
33 77 22 40
40
vv vv
22
22 22
22
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
12
12 33 33 33
33 77 22 40
40 12
12 12
12 33 33
33 77 22 40
40
vv vv
22
22 22
22
3
List Implementation via Arrays List Implementation via Arrays
insertBeginning(22) delete(33)
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
22
22 12
12 33 33
33 77 22 40
40 22
22 12
12 33 33
33 77 22 40
40
if
if list
list is
is not
not full
full
for
for i=(tail-1)
i=(tail-1) to
to 00 tail tail
list[i+1]
list[i+1] == list[i]
list[i]
list[0] =
list[0] = vv
tail++
tail++
size++
size++
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
22
22 12
12 33 33
33 77 22 40
40 22
22 12
12 33 33
33 77 22 40
40
is
is this
this 33?
33?
is
is this
this 33?
33?
tail
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
22
22 12
12 33 33
33 77 22 40
40 22
22 12
12 33 33
33 77 22 40
40
is
is this
this 33?
33? is
is this
this 33?
33?
4
List Implementation via Arrays List Implementation via Arrays
delete(33) delete(33)
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
22
22 12
12 33 77 77 22 40
40 22
22 12
12 33 77 22 22 40
40
00 11 22 33 44 55 66 77 00 11 22 33 44 55 66 77
22
22 12
12 33 77 22 40
40 40
40 22
22 12
12 33 77 22 40
40
if
if list
list is
is not
not empty
empty
ii == head+1;
head+1;
while
while (v(v !=
!= list[i]
list[i] and
and ii << tail)
tail)
i++
i++
if
if (i
(i << tail)
tail) tail’ tail
for
for j=i
j=i to
to (tail-2)
(tail-2)
list[i]
list[i] == list[i+1]
list[i+1]
list[tail-1]
list[tail-1] == null;
null;
size--
size--
tail--
tail--
5
Self-referential data types A Linked List
a linked list is a list in which the order of the
class Node { components is determined by an explicit link
private Object info; // the “info” member in each node
the nodes are structs--each node contains a
private Node next; // the “link” component member and also a link member that
} gives the location of the next node in the list
an external pointer (or head pointer) points to the
first node in the list
data next
head 10 8 50
8 6000
34
info next
ptr = head ;
while (ptr != NULL) {
(*ptr).info ptr 8 6000 cout << ptr->info ;
info next // Or, do something else with
ptr->info ptr = ptr->next ;
}
35
6
Traversing a Linked List Traversing a Linked List
ptr 3000 ptr 3000
//PRE: head points to a linked list //PRE: head points to a linked list
node *ptr ; node *ptr ;
//PRE: head points to a linked list //PRE: head points to a linked list
node *ptr ; node *ptr ;
//PRE: head points to a linked list //PRE: head points to a linked list
node *ptr ; node *ptr ;
7
Traversing a Linked List Traversing a Linked List
ptr 2000 ptr 2000
//PRE: head points to a linked list //PRE: head points to a linked list
node *ptr ; node *ptr ;
//PRE: head points to a linked list //PRE: head points to a linked list
node *ptr ; node *ptr ;
8
Inserting a Node at the Front of Inserting a Node at the Front of
a List a List
item 6 item 6
head 5 8 3 head 5 8 3
49 location 50
head 5 8 3 head 5 8 3
location 51 location 6 52
head 5 8 3 head 5 8 3
location 6 53 location 6 54
9
Using Operator delete Deleting the First Node from the List
item
head 6 5 8 3
55 tempPtr 56
Deleting the First Node from the List Deleting the First Node from the List
item 6 item 6
Node *tempPtr; Node *tempPtr;
head 6 5 8 3 head 6 5 8 3
tempPtr 57 tempPtr 58
Deleting the First Node from the List Deleting the First Node from the List
item 6 item 6
Node *tempPtr; Node *tempPtr;
head 6 5 8 3 head 5 8 3
tempPtr 59 tempPtr 60
10
// SPECIFICATION FILE person.h
class PersonList
#include “bool.h”
. . .
struct PersonRec
PersonList {
Private M a x \0 E d \0 char* name ; // Pointer to person’s name
~PersonList int age ; // Person’s age
data:
};
IsEmpty
head 13 21
CurrentRec struct PersonNode
{
currPtr
InsertAfter char* name ; // Pointer to person’s name
int age ; // Person’s age
Advance PersonNode *next ; // Pointer to next node in list
};
Delete 61 62
11
Ted \0
void PersonList::InsertAfter ( PersonRec someRec )
// PRE: Assigned (someRec) && NOT IsEmpty( ) Insert 16 into the PersonList
// && NOT IsFull ( ) && NOT EndOfList ( )
// POST: someRec inserted after list cursor
// && This new node has become the current record
{ PersonList
// obtain and fill a node Private M a x \0 E d \0
~PersonList
PersonNode *ptr = new PersonNode ; data:
IsEmpty
ptr->name = new char [ strlen ( someRec.name) + 1 ] ; head 13 21
strcpy( ptr->name, someRec.name ); CurrentRec
ptr->age = someRec.age ; currPtr
ptr->next = currPtr->next ; InsertAfter
currPtr->next = ptr ; Advance
currPtr = ptr ;
}
Delete
67
Ted \0 Ted \0
PersonNode *ptr = new PersonNode ;
Inserting 16 into the PersonList 16
someRec someRec
after list cursor
ptr
head 13 21 head 13 21
currPtr currPtr
50 70
Ted \0 Ted \0
16
ptr->name = new char[4] ; 16
strcpy( ptr->name, someRec.name ) ;
someRec someRec
Ted \0
ptr ptr
head 13 21 head 13 21
currPtr currPtr
71 72
12
Ted \0 Ted \0
ptr->age = someRec.age ; ptr->next = currPtr->next ;
16 16
someRec someRec
Ted \0 Ted \0
ptr 16 ptr 16
head 13 21 head 13 21
currPtr currPtr
73 74
Ted \0 Ted \0
16
currPtr->next = ptr ; 16 currPtr = ptr ;
someRec someRec
Ted \0
head 13 21 head 13 21
currPtr currPtr
75 76
13