1 / 30

CS 240: Data Structures

CS 240: Data Structures. Tuesday, June 17 th Vector, Linked List. A couple of details. Exam 1: Monday, July 7 th . Review Session: Monday, July 7 th , 1pm-3pm. Project 1:

coulson
Télécharger la présentation

CS 240: Data Structures

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. CS 240: Data Structures Tuesday, June 17th Vector, Linked List

  2. A couple of details • Exam 1: • Monday, July 7th. • Review Session: • Monday, July 7th, 1pm-3pm. • Project 1: • Make sure you have an idea of what you need to do for project 1. I recommend starting it as soon as possible and figuring out what problems you bump into. • It is currently due on 6/27 at 8pm (electronically).

  3. Presentation • The presentations will be next Tuesday during class. • Groups will have to determine which of the 4 possible presentations they want to present on. • The order to presentation will be determined at random on presentation day.

  4. Group 1: Juan Stubbs Tyler Moody Eugene Mun Group 2: Chris Poole Tim Ng Piya Pintong Group 3: Robert Burgos Steve Welte Ken Williams Presentation Choices: Circular Linked List Multiply Linked List Doubly Linked List Assume that all lists are inserted in order. Multiply linked list should have three data types. Groups

  5. Back to lists… You will being doing this for a grade on Tuesday.

  6. Nodes class Node { public: T thedata; Node * next_data; }; Address: 0xABCD ADT: Node Size: X+32 bits thedata: (?? bits) -> T next_node (32 bits) -> Node *

  7. Lets clean this up. first: 0x50F4 0x3120 0x8458 ADT: Node Size: X+32 bits thedata: “apple” next_node: 0x846c ADT: Node Size: X+32 bits thedata: “cashew” next_node: 0x4278 ADT: Node Size: X+32 bits thedata: “hat” next_node: NULL NULL 0x4278 0x846c 0x5610 ADT: Node Size: X+32 bits thedata: “tomato” next_node: 0x5610 ADT: Node Size: X+32 bits thedata: “donut” next_node: 0x3120 ADT: Node Size: X+32 bits thedata: “banana” next_node: 0x8458

  8. Node thedata: “hat” next_node: Node thedata: “cashew” next_node: Node thedata: “apple” next_node: Node thedata: “donut” next_node: Node thedata: “tomato” next_node: Node thedata: “banana” next_node: NULL

  9. Inserting • In mycontainer, we inserted new items at the end of the list. • Because it was easy! • However, random insertion into a linked list is trivial.

  10. Insertion • Where do we insert the new item? • Who does the new item point to? • Who points to the new item? • What are our special cases?

  11. Insertion Issues • When can insertion be a problem? • Inserting at the end of the mycontainer is easy! • Inserting in the middle isn’t too bad! • Inserting at the front…. • With a list, we can insert easily!

  12. Base case, list is empty • Where do we insert the new item? • Who does the new item point to? • Who points to the new item? • What are our special cases? Node thedata: “apple” next_node: NULL First

  13. List is not empty • Where do we insert the new item? • Who does the new item point to? • Who points to the new item? • What are our special cases? Node thedata: “donut” next_node: Node thedata: “apple” next_node: NULL First

  14. Alternatively • List is not empty • Where do we insert the new item? • Who does the new item point to? • Who points to the new item? • What are our special cases? Node thedata: “donut” next_node: Node thedata: “apple” next_node: NULL First

  15. Removal • Removal a node has similar questions. • What happens to who pointed to us? • What happened to who we pointed to?

  16. Removal • Remove the node. • What happens to who pointed to us? • What happened to who we pointed to? Node thedata: “apple” next_node: Node thedata: “donut” next_node: NULL First

  17. Alternatively • Remove the node. • What happens to who pointed to us? • What happened to who we pointed to? Node thedata: “apple” next_node: Node thedata: “donut” next_node: NULL First

  18. Logic • Insertion and Removal don’t have a lot of cases but they need to be clearly understood. • When adding a node we need to know: • The node that will precede us. • The node that will come after us. • When removing a node we need to know: • The node that precedes us. • The node that comes after us. • If we consider first and NULL as nodes, these cases may be easier to understand.

  19. Searching • Start at first. • If first points somewhere, follow it • Check the current value • Follow path to next node if it exists

  20. Outputting • Start at first. • If first points somewhere, follow it • Print the current value • Follow path to next node if it exists

  21. Insertion Sort • If we start from an empty list we can guarantee it is sorted by using invariants. • Invariant: • Whenever we add an item, we place it in the proper sorted location. • Using mathematical induction we can demonstrate that this is true.

  22. Insertion Sort • Therefore, we must change insert: • We need to find the node that precedes us. • We need to find the node that follows us. • This is very similar to insert! • Instead of saying where we want to insert, we need to search the list and discover where we belong

  23. Insertion Sort • In the list: First -> 1 -> 15 -> 19 -> 22 -> 33 -> 102 -> 119 -> NULL If we want to insert 27? If we want to insert 115? If we want to insert 130? If we want to insert 0?

  24. Linked List • Let us finish up writing code for Linked List.

  25. Mycontainer will be given a “current location” that refers to an item in mycontainer. It is initialized to 0 at creation time. Basic Functionality: void begin() void end() bool previous() bool next() int current() Lab 3

  26. Current Location • Error handing: • int geterror() • Insertion and Removal • bool insertHere(int newvalue) • int removeHere()

  27. New functionality • Searching • int howManyOf(int searchfor) • int & operator [](int index)

  28. Vector • Vector is a built-in (via the STL) container. • Requires “#include<vector>” • Declaring a vector of int: • vector<int> myintarray; • Vector is designed to work with various types via a technique called templating. • Any fully-qualified ADT can be used with vector.

  29. #include<iostream> #include<vector> using namespace std; int main() { vector<int> mydata; int userinput; do { cin >> userinput; mydata.push_back(userinput); } while(userinput>0); int sum = 0; for(int i=0;i<mydata.size();i++) { sum += mydata[i]; } cout << sum << endl; } Vector

  30. Review Session • On a piece of paper list the following: • Topics you think you know very well • Topics you are concerned about, uncertain of, unclear about, etc. • These will allow me to determine what to focus on for the review session. • Without this feedback, I’ll just answer questions at the review session and won’t have anything to work from.

More Related