Unformatted text preview: Object Programming Cust1
tmp tmp Cust2
0 curr Cust1 Head
0 curr Add Add Object Programming Cust1 Head
tmp Cust1 Head
0 curr Object Programming Cust2
0 curr Cust3
0 Add Add Object Programming Cust1 Head
tmp Object Programming Cust1 Head
Head Cust2 Cust2 Cust3
0 curr Add Remove Object Programming Object Programming Cust1 Head
tmp Cust1 Head
tmp Cust2 Cust2 Cust1 Cust3
0 Remove Cust3
0 Remove Object Programming Object Programming Head
Head tmp Cust2 Cust1 Cust2 Cust1 Cust3
0 Remove Object Programming Code:
Code: Object Programming //
// void Initialise(CustPtr& Head)
Head = 0;
} if (Head != 0)
curr = Head;
while (curr->next != 0)
curr = curr->next;
curr->next = tmp;
Head = tmp;
tmp void Add(char name, int psize, CustPtr& Head)
void Add(char name, int
CustPtr tmp, curr;
// create a new customer and
store the information
tmp = new Cust;
tmp->partysize = psize;
tmp->next = 0; add to the end of the list
if the list is not empty // is this the end? } Object Programming Object Programming bool Remove(char name, int& psize, CustPtr& Head)
name, int psize CustPtr
CustPtr tmp; //
// if (Head == 0)
psize = Head->partysize;
make a copy of the pointer to current head
tmp = Head;
Head = Head->next; // change the head
delete tmp; // delete old head's memory
return true; } These three functions enable us to create a list of
No limit is set at compile time!
Note that the only thing the function that calls
these three functions knows is the pointer Head.
The functions get from one entry to another via
the links to the next customer
– hence the name linked list.
linked Object Programming W e can conceal even the Head from the calling
function by making it local to the three functions
and not available to the calling function.
And take Head from the argument lists.
Now the calling function knows nothing about the
contents of the list, or even how the list is
organised – whether it was an array or a linked
Object Programming Object Programming But what if we want two lists of customers
– maybe for two different sized tables.
W e could have two Heads, each passed to the
same functions when needed.
Oops. Our data hiding has been lost.
W ouldn't it be nice to have any number of lists,
but with the protection?
View Full Document
- Spring '14
- Sort, Pointer, Garbage collection, Stype