Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Writing a Good Program 8. Elementary Data Structure.

Similar presentations


Presentation on theme: "1 Writing a Good Program 8. Elementary Data Structure."— Presentation transcript:

1 1 Writing a Good Program 8. Elementary Data Structure

2 2 Linked Lists In using array, one needs to define the size beforehand. If the size is too big, it’s a waste of memory. If the size is too small, it may even crash the system. If we want the number of items in an array to be dynamically defined during the program execution, linked lists are more useful. A linked list is a data structure that consists of a number items, with each item having a pointer points to the location of the next item. Computer Programming and Basic Software Engineering 8. Elementary Data Structure

3 3 Linked List Free Store or the heap Global Name Space Code Space The Stack CAT 0 CAT 1... CAT 499 A pointer Head is kept to point to the beginning memory location of a linked list of CAT objects in Free Store.. 0. A null pointer means the end of the list Computer Programming and Basic Software Engineering 8. Elementary Data Structure Address of the next item

4 4. Linked lists have 3 forms Singly linked Doubly linked Tree Data... Head 0 Singly linked Data... Head 0 Doubly linked... 0 Data....... 000 00 Tree Head Computer Programming and Basic Software Engineering 8. Elementary Data Structure

5 5 Linked Lists - Add an item Data... Head 0 Data. 0 Append an item to the end Data.. Head Data. 0 Insert an item in between Data. Computer Programming and Basic Software Engineering 8. Elementary Data Structure

6 6 Data... Head 0 Data. 0 Remove the last item Linked Lists - Remove an item Data. 0.. Head Remove an item in between Data. Computer Programming and Basic Software Engineering 8. Elementary Data Structure

7 7 Linked Lists - How can it be done? #include using namespace std; class CAT //Use a number CatNum to represent a cat { public: CAT() {pNext=0;} ~CAT(){;} int GetNum() const {return CatNum;} void SetNum(int num) {CatNum = num;} CAT * GetNext() {return pNext;} void SetNext(CAT *pN) {pNext = pN;} private: int CatNum; CAT *pNext; }; A linked list of CAT objects is to be created Record the cat number Record the pointer of the next cat Computer Programming and Basic Software Engineering 8. Elementary Data Structure All functions implemented

8 8 CAT * create(int n) { //Create a linked list CAT *pH,*pT,*pL; int i; pH = new CAT; pL = pH; pL->SetNum(0); for (i=1; i<n; i++) {pT = new CAT; pL->SetNext(pT); pL = pT; pL->SetNum(i); } //pL points to last item return pH; } if n = 3 pL. pH 0 0 0. 1 0. 2 pT Computer Programming and Basic Software Engineering 8. Elementary Data Structure

9 9 int main() { int num; CAT *pHead, *pTemp; cout << "How many Cats: "; cin >> num; pHead = create(num); pTemp = pHead; do { cout GetNum() << endl; pTemp = pTemp->GetNext(); } while (pTemp!=0); return 0; } Ask the user to determine the number of cats As the number of CAT is dynamically determined, we cannot use array approach (array index is not a variable) Call create() to create a linked list of CAT objects Computer Programming and Basic Software Engineering 8. Elementary Data Structure Print out all the CAT number recorded in each CAT object

10 10 Computer Programming and Basic Software Engineering 8. Elementary Data Structure Add an item to the end // Add an item to the end // Return the head pointer CAT * addend(CAT *pH) { CAT *pCurr, *pPrev, *pCat; int j=0; // CatNum to be written pCurr = pH; do // find the item {pPrev = pCurr; j++; pCurr = pCurr->GetNext(); } while (pCurr != 0); // pPrev is pointing last item pCat = new CAT; pPrev->SetNext(pCat); pCat->SetNum(j); // write CatNum return pH; } pCurr. pH 0 0 0. 1 0. 2 pPrev pCurr 0. pCat pPrev 3 Go from head to end, then add item. How about inserting an item somewhere inside?

11 11 Computer Programming and Basic Software Engineering 8. Elementary Data Structure Remove an item // Remove the second item n=1 // Return the head pointer CAT * remove(int n, CAT *pH) { CAT *pCurr, *pPrev, *pCat; int i=0; pCurr = pH; do // find the item { pPrev = pCurr; pCurr = pPrev->GetNext(); i++; } while (i<n); // pCurr is pointing the deleted item pCat=pCurr->GetNext(); pPrev->SetNext(pCat); delete pCurr; return pH; } if n = 1 pCurr pCat. pH 0. 1 0. 2 pPrev How about removing the first item (pointed by pH )? pCurr

12 12 Exercise 8.1 Redesign the program in p. 9 such that every item contains the name of the cat, which is a string. After creating the linked list, it will continuously ask the user to show all items, insert an item, delete an item in the linked list, or quit. We need to modify the main() and write two functions. CAT * insert(int n, char *pName, CAT *pHead;) // int n - the insert position in the linked list // The first item is in position 0 // char *pName - the name of the cat to be inserted // CAT *pHead - the head pointer (initially pHead = 0) // The function should return the revised head pointer CAT * del(int n, CAT *pHead;) // int n - the delete position in the linked list // CAT *pHead - the head pointer (initially pHead = 0) // The function should return the revised head pointer Computer Programming and Basic Software Engineering 8. Elementary Data Structure

13 13 Computer Programming and Basic Software Engineering 8. Elementary Data Structure Your program should be able to allocate or free the memory required to implement the two functions above. It should also check whether the integer n is bigger than the size of the list. Exercise 8.1 (cont)

14 14


Download ppt "1 Writing a Good Program 8. Elementary Data Structure."

Similar presentations


Ads by Google