0 curr head while curr next 0 curr curr next curr

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

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

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online