Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240: Data Structures Thursday, June 21 th Vector, Linked List.

Similar presentations


Presentation on theme: "CS 240: Data Structures Thursday, June 21 th Vector, Linked List."— Presentation transcript:

1 CS 240: Data Structures Thursday, June 21 th 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. No “R”s were given for Lab 3. You can keep your grade or submit a revision. Revisions do not completely replace your grade. Revisions do not completely replace your grade. The old grade will be retained, however, an additional revision grade will be added. 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). 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. 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. Most people missed the questions and test cases for Lab 3. Remember, your revision requires the following: Remember, your revision requires the following: A writeup discussing each error and correction A writeup discussing each error and correction The original submission The original submission A corrected 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). 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. 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. 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. The review session is from 4-5pm tomorrow in EBQ3. Lab 3 revisions are due at 4pm next lab (July 9 th ). Lab 3 revisions are due at 4pm next lab (July 9 th ). There are no classes next week. 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. 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 12 th at 4pm. Defense of the project is on July 13 th, throughout the day. We will make appointments on July 9 th or 10 th. The project is due on July 12 th at 4pm. Defense of the project is on July 13 th, throughout the day. We will make appointments on July 9 th or 10 th.

6 Specification The specification will probably be between 2 and 5 pages. 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. Success of the project is highly dependent on the time you spend considering the specification. Card: 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?

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

8 Specification Player: Player: A player is a Deck, but… 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 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 discard hand into a Deck. Needs to be able to receive Cards – just like Deck, but location doesn’t matter. 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 Needs to answer the question: “Do I want another card?” – compare hand value to some predetermined value Doesn’t need the other Deck methods Doesn’t need the other Deck methods

9 Specification No specification will ever be perfect. 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). 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 Address: 0xABCD ADT: Node Size: X+32 bits thedata: (?? bits) -> T next_node (32 bits) -> Node * class Node {public: T thedata; Node * next_data; };

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

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

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

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

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

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

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

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

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

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

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

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

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

25 Outputting Start at first. Start at first. If first points somewhere, follow it If first points somewhere, follow it Print the current value Print the current value Follow path to next node if it exists 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. If we start from an empty list we can guarantee it is sorted by using invariants. Invariant: Invariant: Whenever we add an item, we place it in the proper sorted location. Whenever we add an item, we place it in the proper sorted location. Using mathematical induction we can demonstrate that this is true. Using mathematical induction we can demonstrate that this is true.

27 Insertion Sort Therefore, we must change insert: Therefore, we must change insert: We need to find the node that precedes us. We need to find the node that precedes us. We need to find the node that follows us. We need to find the node that follows us. This is very similar to insert! 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 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: 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. Vector is a built-in (via the STL) container. Requires “#include ” Requires “#include ” Declaring a vector of int: Declaring a vector of int: vector myintarray; vector myintarray; Vector is designed to work with various types via a technique called templating. Vector is designed to work with various types via a technique called templating. Any fully-qualified ADT can be used with vector. Any fully-qualified ADT can be used with vector.

30 Vector #include<iostream>#include<vector> using namespace std; int main() { vector 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; }

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


Download ppt "CS 240: Data Structures Thursday, June 21 th Vector, Linked List."

Similar presentations


Ads by Google