1 / 30

CSE 2341 Object Oriented Programming with C++ Note Set #18

CSE 2341 Object Oriented Programming with C++ Note Set #18. Overview. Dynamic Data structures with linked lists. Linked List. Series of connected nodes Can grow/shrink at runtime dynamic allocation of nodes allows for this. Data. Head. Null. Linked List Node. Data. struct ListNode {

Download Presentation

CSE 2341 Object Oriented Programming with C++ Note Set #18

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. CSE 2341Object Oriented Programming with C++Note Set #18

  2. Overview • Dynamic Data structures with linked lists

  3. Linked List • Series of connected nodes • Can grow/shrink at runtime • dynamic allocation of nodes allows for this Data Head Null

  4. Linked List Node Data struct ListNode { double data; ListNode* next; }; ListNode is a self referentialdata structure.

  5. Basic Linked List Operations • Basic Operations of LL • appending a node • traversing the list • inserting a node • deleting a node • destroying the list

  6. Class NumberList class NumberList { private: struct ListNode { double value; ListNode* next; }; ListNode* head; private: NumberList() {head = NULL;} ~NumberList(); void appendNode(double); void insertNode(double); void deleteNode(double); void displayList(); };

  7. AppendNode void NumberList::appendNode(double num) { ListNode* newNode, *nodePtr; newNode = new ListNode; newNode -> value = num; newNode -> next = NULL; if(!head) //If there aren’t any nodes in list head = newNode; else { nodePtr = head; while(nodePtr->next) nodePtr = nodePtr->next; nodePtr -> next = newNode; } }

  8. Append newNode Null 5 Head 3 4 Null NodePtr

  9. Append newNode Null 5 Head 3 4 Null NodePtr

  10. Append newNode Null 5 Head 3 4 NodePtr

  11. Display List void NumberList::DisplayList() { ListNode* nodePtr; nodePtr = head; while(nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } }

  12. Display NodePtr Head Null 3 4 5 3

  13. Display NodePtr Head Null 3 4 5 3 4

  14. Display NodePtr Head Null 3 4 5 3 4 5

  15. Display NodePtr Head Null 3 4 5 3 4 5

  16. Insert A Node void NumberList::insertNode(double num) { ListNode *newNode, *nodePtr, *previousNode; newNode = new ListNode; newNode -> value = num; if(!head) { head = newNode; newNode->next = NULL; } else { nodePtr = head; previousNode = NULL;

  17. Insert A Node //Continued from previous slide while(nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } if(previousNode == NULL) //if insert as first { head = newNode; newNode -> next = nodePtr; } else { previousNode -> next = newNode; newNode -> next = nodePtr; } } }

  18. Insert newNode 4.5 PreviousNode NodePtr Null Head Null 3 4 5

  19. NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5

  20. NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5

  21. NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5

  22. NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5

  23. NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5

  24. NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5

  25. Deleting A Node void NumberList::deleteNode(double num) { ListNode *nodePtr, *previousNode; if(!head) return; if(head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { nodePtr = head; while(nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } if(nodePtr) { previousNode->next = nodePtr -> next; delete nodePtr; } } }

  26. Delete num 4 PreviousNode NodePtr Null Head Null 3 4 5

  27. Delete num 4 PreviousNode NodePtr Head Null 3 4 5

  28. Delete num 4 PreviousNode NodePtr Head Null 3 4 5

  29. Delete num 4 PreviousNode NodePtr Head Null 3 4 5

  30. Fini ?

More Related