1 / 14

Writing a Good Program 8. Elementary Data Structure

Writing a Good Program 8. Elementary Data Structure. Computer Programming and Basic Software Engineering. 8. Elementary Data Structure. Linked Lists. In using array , one needs to define the size beforehand. If the size is too big, it’s a waste of memory.

annora
Télécharger la présentation

Writing a Good Program 8. Elementary Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing a Good Program 8. Elementary Data Structure

  2. Computer Programming and Basic Software Engineering 8. Elementary Data Structure 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.

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

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

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

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

  7. Computer Programming and Basic Software Engineering Linked Lists - How can it be done? 8. Elementary Data Structure #include <iostream> 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 CATobjects is to be created All functions implemented Record the cat number Record the pointer of the next cat

  8. pH pL . . . pL 0 pT pT pL 0 0 Computer Programming and Basic Software Engineering 8. Elementary Data Structure if n = 3 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; } 0 1 2

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

  10. pH pPrev pPrev pPrev pCurr . . . . 0 pCurr pCat 3 pCurr pPrev 0 0 0 pCurr Computer Programming and Basic Software Engineering 8. Elementary Data Structure Add an item to the end Go from head to end, then add item. // 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; } 0 1 2 How about inserting an item somewhere inside?

  11. pPrev pCurr . . . pCurr 1 pCat Computer Programming and Basic Software Engineering Remove an item 8. Elementary Data Structure if n = 1 pH // 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; } 0 2 How about removing the first item (pointed by pH)? 0

  12. Computer Programming and Basic Software Engineering Exercise 8.1 8. Elementary Data Structure 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

  13. Computer Programming and Basic Software Engineering 8. Elementary Data Structure Exercise 8.1 (cont) 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.

More Related