CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009Linked List – Concord List NodesInsert at frontInsert at backRemove from frontRemove from back17.4 Linked ListsDisplay 17.3 Adding a Node to the Head of a Linked ListLost Nodes Pitfall: Display 17.5 Lost NodesDisplay 17.6 Inserting in the Middle of a Linked List (1 of 2)Display 17.6 Inserting in the Middle of a Linked List (2 of 2)Display 17.7 Removing a NodeSearching a Linked ListPseudocode for search FunctionAlgorithm for search Function17.5 StacksA Stack—Graphic: Display 17.12 A StackStack Push and PopSlide 1917.6 QueuesSlide 2117.7 TreesTrees IntroductionSlide 24Tree Structure: Display 17.35 A Binary Tree (1 of 2)Tree Structure: Display 17.35 A Binary Tree (2 of 2)Tree PropertiesTrees and RecursionTree ProcessingTree StorageSlide 31Slide 32Slide 33treenode.h (1 of 2)treenode.h (2 of 2)tree.h (1 of 6)tree.h (2 of 6)tree.h (3 of 6)tree.h (4 of 6)tree.h (5 of 6)tree.h (6 of 6)fig17_19.cpp (1 of 3)fig17_19.cpp (2 of 3)fig17_19.cpp (3 of 3)fig17_19.cpp output (1 of 1)1CISC181 Introduction to Computer ScienceDr. McCoyLecture 22November 17, 20092Linked List – Concord List Nodes•See Example: ~/Class/cisc181/examples/concord-list•Driver Program•Header files for both node and list•Implementation files for both•Fun with linked lists – adding, deleting, walking through,… 2003 Prentice Hall, Inc. All rights reserved.3Insert at front7 11firstPtr12newPtra)7 11firstPtr12newPtrb)newPtr->nextPtr = firstPtrfirstPtr = newPtrIf list empty, then firstPtr = lastPtr = newPtr 2003 Prentice Hall, Inc. All rights reserved.4Insert at backfirstPtr lastPtra)newPtrfirstPtr lastPtrb)newPtr12 7 11512 11 57lastPtr->nextPtr = newPtrlastPtr = newPtrIf list empty, then firstPtr = lastPtr = newPtr 2003 Prentice Hall, Inc. All rights reserved.5Remove from frontfirstPtr lastPtra)firstPtr lastPtrb)tempPtr12 71212 7511511tempPtr = firstPtrfirstPtr = firstPtr->nextIf there are no more nodes,firstPtr = lastPtr = 0delete tempPtr 2003 Prentice Hall, Inc. All rights reserved.6Remove from back5511771212firstPtr lastPtra)firstPtr lastPtrb)tempPtrcurrentPtr11tempPtr = lastPtr"Walk" list until get next-to-last node, untilcurrentPtr->nextPtr = lastPtrlastPtr = currentPtrdelete tempPtr 2003 Prentice Hall, Inc. All rights reserved.717.4 Linked Lists•Types of linked lists–Singly linked list (used in example)•Pointer to first node•Travel in one direction (null-terminated)–Circular, singly-linked•As above, but last node points to first–Doubly-linked list•Each node has a forward and backwards pointer•Travel forward or backward•Last node null-terminated–Circular, double-linked•As above, but first and last node joinedDisplay 17.3 Adding a Node to the Head of a Linked List17-8Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Lost Nodes Pitfall: Display 17.5 Lost Nodes17-9Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Display 17.6 Inserting in the Middle of a Linked List (1 of 2)17-10Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Display 17.6 Inserting in the Middle of a Linked List (2 of 2)17-11Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Display 17.7 Removing a Node17-12Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Searching a Linked List•Function with two arguments:IntNodePtr search(IntNodePtr head, int target);//Precondition: pointer head points to head of//linked list. Pointer in last node is NULL.//If list is empty, head is NULL//Returns pointer to 1st node containing target//If not found, returns NULL•Simple "traversal" of list–Similar to array traversal17-13Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Pseudocode for search Function•while (here doesn’t point to target node orlast node){ Make here point to next node in list}if (here node points to target) return here;else return NULL;17-14Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Algorithm for search Function•while (here->getData() != target &&here->getLink() != NULL) here = here->getLink();if (here->getData() == target) return here;else return NULL;•Must make "special" case for empty list–Not done here17-15Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2003 Prentice Hall, Inc. All rights reserved.1617.5 Stacks•Stack–Nodes can be added/removed from top•Constrained version of linked list•Like a stack of plates–Last-in, first-out (LIFO) data structure–Bottom of stack has null link•Stack operations–Push: add node to top–Pop: remove node from top•Stores value in reference variableA Stack—Graphic: Display 17.12 A Stack17-17Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Stack Push and Pop•Adding data item to stack push–Considered "pushing" data onto stack–Recall: goes to "top" of stack•Removing data item from stack pop–Considered "popping" item off stack–Recall: removed from "top" of stack17-18Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2003 Prentice Hall, Inc. All rights reserved.1917.5 Stacks•Upcoming program–Create stack from list•insertAtFront, removeFromFront–Software reusability•Inheritance–Stack inherits from List•Composition–Stack contains a private List object–Performs operations on that object–Makes stack implementation simple 2003 Prentice Hall, Inc. All rights reserved.2017.6 Queues•Queue–Like waiting in line–Nodes added to back (tail), removed from front (head)–First-in, first-out (FIFO) data structure–Insert/remove called enqueue/dequeue•Applications–Print spooling•Documents wait in queue until printer available–Packets on network–File requests from server 2003 Prentice Hall, Inc. All rights reserved.2117.6 Queues•Upcoming program–Queue implementation–Reuse List as before•insertAtBack (enqueue)•removeFromFront (dequeue) 2003 Prentice Hall, Inc. All rights reserved.2217.7 Trees•Linear data structures–Lists, queues, stacks•Trees–Nonlinear, two-dimensional–Tree nodes have 2 or more links–Binary trees have exactly 2 links/node•None, both, or one link can be nullTrees Introduction•Trees can be complex data structures•Only basics here:–Constructing, manipulating–Using nodes and pointers•Recall linked list: nodes have only onepointer next node•Trees have two, & sometimes more,pointers to other
View Full Document