1 / 31

CS 240: Data Structures

CS 240: Data Structures. Thursday, June 21 th Vector, Linked List. A couple of details. No “R”s were given for Lab 3. You can keep your grade or submit a revision. Revisions do not completely replace your grade.

aricky
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 Thursday, June 21th Vector, Linked List

  2. A couple of details • No “R”s were given for Lab 3. You can keep your grade or submit a revision. • Revisions do not completely replace your grade. • The old grade will be retained, however, an additional revision grade will be added. • Generally, the original submission will count for 2/3 of the total grade (the revision being 1/3). • You may submit additional revisions after the first but no grade change will be made after the first.

  3. A couple of details • Most people missed the questions and test cases for Lab 3. • Remember, your revision requires the following: • A writeup discussing each error and correction • The original submission • A corrected submission • 10 extra points are awarded to assignments that do not require a revision (generally only style errors, and at most 1 logic error). A revision is still welcome in these cases (I’ll want at least 1 revision from each student). • I recommend completing your revision (even if not ready for submission) before the exam.

  4. A couple of details • I will be here during the break, feel free to make appointments to go over material or assignments. • The review session is from 4-5pm tomorrow in EBQ3. • Lab 3 revisions are due at 4pm next lab (July 9th). • There are no classes next week.

  5. A couple of details • You will need to commit to a project choice when you submit the project 1 specification. It is due at the exam. • The project is due on July 12th at 4pm. Defense of the project is on July 13th, throughout the day. We will make appointments on July 9th or 10th.

  6. The specification will probably be between 2 and 5 pages. Success of the project is highly dependent on the time you spend considering the specification. Card: Has a value (A,2-10,J,Q,K) Has a suit (Clubs, Diamonds, Hearts, Spades) Can be Faceup or Facedown Requires default constructor, destructor, assignment operator. Requires explicit constructor in order to create usable cards. Requires overloaded output (<<) Output is represented by a string (such as “Ace of Hearts”, or “10 of Diamonds”. Also, needs to handle facedown. Need methods to retrieve value, suit, face status. Should getValue() and getSuit() work if card is facedown? Specification

  7. Specification • Deck: • Requires default constructor, destructor, assignment operator. • Requires a method to create a standard playing deck. • Has an array of Card objects – dynamic memory • Has a currentsize and maxcapacity. • Needs to resize when appropriate. • Cards can be added directly to the top of Deck. • Cards can be added directly to the bottom of Deck • Shuffle: Reorder all Cards in our deck. • Cards are removed directly from the top of the Deck. • Are they facedown or faceup? • Need to manage when we run out of cards – return NULL.

  8. Specification • Player: • A player is a Deck, but… • Can determine the value of all owned cards – must be able to scan each card – all cards are faceup for the player • Needs to be able to discard hand into a Deck. • Needs to be able to receive Cards – just like Deck, but location doesn’t matter. • Needs to answer the question: “Do I want another card?” – compare hand value to some predetermined value • Doesn’t need the other Deck methods

  9. Specification • No specification will ever be perfect. • The original specification will be compared to the final specification and code submission. The final specification should address changes and clarifications that were made to the original specification (due to mistakes or unforeseen situations).

  10. Back to lists… You will being doing this for a grade later.

  11. 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 *

  12. 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

  13. 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

  14. 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.

  15. 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?

  16. 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!

  17. 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

  18. 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

  19. 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

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

  21. 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

  22. 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

  23. 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.

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

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

  26. 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.

  27. 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

  28. 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?

  29. 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.

  30. #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

  31. 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