300 likes | 411 Views
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 {
E N D
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 { double data; ListNode* next; }; ListNode is a self referentialdata structure.
Basic Linked List Operations • Basic Operations of LL • appending a node • traversing the list • inserting a node • deleting a node • destroying the list
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(); };
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; } }
Append newNode Null 5 Head 3 4 Null NodePtr
Append newNode Null 5 Head 3 4 Null NodePtr
Append newNode Null 5 Head 3 4 NodePtr
Display List void NumberList::DisplayList() { ListNode* nodePtr; nodePtr = head; while(nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } }
Display NodePtr Head Null 3 4 5 3
Display NodePtr Head Null 3 4 5 3 4
Display NodePtr Head Null 3 4 5 3 4 5
Display NodePtr Head Null 3 4 5 3 4 5
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;
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; } } }
Insert newNode 4.5 PreviousNode NodePtr Null Head Null 3 4 5
NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5
NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5
NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5
NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5
NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5
NodePtr Insert newNode 4.5 PreviousNode Head Null 3 4 5
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; } } }
Delete num 4 PreviousNode NodePtr Null Head Null 3 4 5
Delete num 4 PreviousNode NodePtr Head Null 3 4 5
Delete num 4 PreviousNode NodePtr Head Null 3 4 5
Delete num 4 PreviousNode NodePtr Head Null 3 4 5
Fini ?