cs 240: data structures thursday, june 21 th vector, linked list
Post on 20-Dec-2015
214 views
TRANSCRIPT
CS 240: Data StructuresCS 240: Data Structures
Thursday, June 21Thursday, June 21thth
Vector, Linked ListVector, Linked List
A couple of detailsA couple of details
No “R”s were given for Lab 3. You can keep No “R”s were given for Lab 3. You can keep your grade or submit a revision.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 The old grade will be retained, however, an additional
revision grade will be added.revision grade will be added. Generally, the original submission will count for 2/3 of Generally, the original submission will count for 2/3 of
the total grade (the revision being 1/3).the total grade (the revision being 1/3).
You may submit additional revisions after the You may submit additional revisions after the first but no grade change will be made after the first but no grade change will be made after the first. first.
A couple of detailsA couple of details Most people missed the questions and test cases for Lab Most people missed the questions and test cases for Lab
3. 3. Remember, your revision requires the following:Remember, your revision requires the following:
A writeup discussing each error and correctionA writeup discussing each error and correction The original submissionThe original submission A corrected submissionA corrected submission
10 extra points are awarded to assignments that do not 10 extra points are awarded to assignments that do not require a revision (generally only style errors, and at require a revision (generally only style errors, and at most 1 logic error). A revision is still welcome in these most 1 logic error). A revision is still welcome in these cases (I’ll want at least 1 revision from each student).cases (I’ll want at least 1 revision from each student).
I recommend completing your revision (even if not ready I recommend completing your revision (even if not ready for submission) before the exam.for submission) before the exam.
A couple of detailsA couple of details
I will be here during the break, feel free to I will be here during the break, feel free to make appointments to go over material or make appointments to go over material or assignments.assignments.
The review session is from 4-5pm The review session is from 4-5pm tomorrow in EBQ3.tomorrow in EBQ3.
Lab 3 revisions are due at 4pm next lab Lab 3 revisions are due at 4pm next lab (July 9(July 9thth).).
There are no classes next week.There are no classes next week.
A couple of detailsA couple of details
You will need to commit to a project choice You will need to commit to a project choice when you submit the project 1 when you submit the project 1 specification. It is due at the exam.specification. It is due at the exam.
The project is due on July 12The project is due on July 12thth at 4pm. at 4pm. Defense of the project is on July 13Defense of the project is on July 13thth, , throughout the day. We will make throughout the day. We will make appointments on July 9appointments on July 9thth or 10 or 10thth. .
SpecificationSpecification
The specification The specification will probably be will probably be between 2 and 5 between 2 and 5 pages.pages.
Success of the Success of the project is highly project is highly dependent on dependent on the time you the time you spend spend considering the considering the specification. specification.
Card:Card: Has a value (A,2-10,J,Q,K)Has a value (A,2-10,J,Q,K) Has a suit (Clubs, Diamonds, Hearts, Has a suit (Clubs, Diamonds, Hearts,
Spades)Spades) Can be Faceup or FacedownCan be Faceup or Facedown Requires default constructor, destructor, Requires default constructor, destructor,
assignment operator.assignment operator. Requires explicit constructor in order to Requires explicit constructor in order to
create usable cards.create usable cards. Requires overloaded output (<<)Requires overloaded output (<<) Output is represented by a string (such as Output is represented by a string (such as
“Ace of Hearts”, or “10 of Diamonds”. “Ace of Hearts”, or “10 of Diamonds”. Also, needs to handle facedown.Also, needs to handle facedown.
Need methods to retrieve value, suit, face Need methods to retrieve value, suit, face status.status.
Should getValue() and getSuit() work if Should getValue() and getSuit() work if card is facedown?card is facedown?
SpecificationSpecification Deck:Deck:
Requires default constructor, destructor, assignment Requires default constructor, destructor, assignment operator.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 memoryHas 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 DeckCards 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.
SpecificationSpecification
Player:Player: A player is a Deck, but…A player is a Deck, but… Can determine the value of all owned cards – must be Can determine the value of all owned cards – must be
able to scan each card – all cards are faceup for the able to scan each card – all cards are faceup for the playerplayer
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, Needs to be able to receive Cards – just like Deck,
but location doesn’t matter.but location doesn’t matter. Needs to answer the question: “Do I want another Needs to answer the question: “Do I want another
card?” – compare hand value to some predetermined card?” – compare hand value to some predetermined valuevalue
Doesn’t need the other Deck methodsDoesn’t need the other Deck methods
SpecificationSpecification
No specification will ever be perfect.No specification will ever be perfect.The original specification will be compared The original specification will be compared
to the final specification and code to the final specification and code submission. The final specification should submission. The final specification should address changes and clarifications that address changes and clarifications that were made to the original specification were made to the original specification (due to mistakes or unforeseen situations).(due to mistakes or unforeseen situations).
Back to lists…Back to lists…
You will being doing this for a grade later.
NodesNodes
Address: 0xABCD
ADT: Node
Size: X+32 bits
thedata: (?? bits) -> Tnext_node (32 bits) -> Node *
class Nodeclass Node
{{
public:public:
T thedata;T thedata;
Node * next_data;Node * next_data;
};};
first: 0x50F4
0x846c
ADT: NodeSize: X+32 bits
thedata: “apple”next_node: 0x846c
ADT: NodeSize: X+32 bits
thedata: “donut”next_node: 0x3120
0x3120
ADT: NodeSize: X+32 bits
thedata: “cashew”next_node: 0x4278
0x4278
ADT: NodeSize: X+32 bits
thedata: “tomato”next_node: 0x5610
0x5610
ADT: NodeSize: X+32 bits
thedata: “banana”next_node: 0x8458
0x8458
ADT: NodeSize: X+32 bits
thedata: “hat”next_node: NULL
NULL
Lets clean this up.
Nodethedata: “apple”
next_node:
Nodethedata: “donut”
next_node:
Nodethedata: “cashew”
next_node:
Nodethedata: “tomato”
next_node:
Nodethedata: “banana”
next_node:
Nodethedata: “hat”
next_node:
NULL
InsertingInserting
In mycontainer, we inserted new items at the In mycontainer, we inserted new items at the end of the list.end of the list.
Because it was easy!Because it was easy! However, random insertion into a linked list is However, random insertion into a linked list is
trivial.trivial.
InsertionInsertion
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?
Insertion IssuesInsertion Issues
When can insertion be a problem?When can insertion be a problem? Inserting at the end of the mycontainer is Inserting at the end of the mycontainer is
easy!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!
NULLFirst
Base case, list is emptyBase case, list is emptyWhere 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?
Nodethedata: “apple”
next_node:
NULLFirst
List is not emptyList is not emptyWhere 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?
Nodethedata: “apple”
next_node:
Nodethedata: “donut”
next_node:
NULLFirst
AlternativelyAlternatively
List is not emptyList is not emptyWhere 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?
Nodethedata: “apple”
next_node:
Nodethedata: “donut”
next_node:
RemovalRemoval
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?
NULLFirst
RemovalRemoval
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?
Nodethedata: “apple”
next_node:
Nodethedata: “donut”
next_node:
NULLFirst
AlternativelyAlternatively
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?
Nodethedata: “apple”
next_node:
Nodethedata: “donut”
next_node:
LogicLogic
Insertion and Removal don’t have a lot of cases Insertion and Removal don’t have a lot of cases but they need to be clearly understood.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 If we consider first and NULL as nodes, these cases may be easier to understand.cases may be easier to understand.
SearchingSearching
Start at first.Start at first. If first points somewhere, follow itIf first points somewhere, follow it
Check the current valueCheck the current valueFollow path to next node if it existsFollow path to next node if it exists
OutputtingOutputting
Start at first.Start at first. If first points somewhere, follow itIf first points somewhere, follow it
Print the current valuePrint the current valueFollow path to next node if it existsFollow path to next node if it exists
Insertion SortInsertion Sort
If we start from an empty list we can If we start from an empty list we can guarantee it is sorted by using invariants.guarantee it is sorted by using invariants.
Invariant:Invariant:Whenever we add an item, we place it in the Whenever we add an item, we place it in the
proper sorted location.proper sorted location.Using mathematical induction we can Using mathematical induction we can
demonstrate that this is true.demonstrate that this is true.
Insertion SortInsertion Sort
Therefore, we must change insert:Therefore, we must change insert:We need to find the node that precedes We need to find the node that precedes
us.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 Instead of saying where we want to insert, we need to search the list and discover where we need to search the list and discover where we belongbelong
Insertion SortInsertion Sort
In the list:In the list:
First -> 1 -> 15 -> 19 -> 22 -> 33 -> 102 -> 119 -> NULLFirst -> 1 -> 15 -> 19 -> 22 -> 33 -> 102 -> 119 -> NULL
If we want to insert 27?If we want to insert 27?If we want to insert 115?If we want to insert 115?If we want to insert 130?If we want to insert 130?If we want to insert 0?If we want to insert 0?
VectorVector
Vector is a built-in (via the STL) container.Vector is a built-in (via the STL) container.Requires “#include<vector>”Requires “#include<vector>”
Declaring a vector of int:Declaring a vector of int:vector<int> myintarray;vector<int> myintarray;
Vector is designed to work with various Vector is designed to work with various types via a technique called templating.types via a technique called templating.
Any fully-qualified ADT can be used with Any fully-qualified ADT can be used with vector.vector.
VectorVector#include<iostream>#include<iostream>#include<vector>#include<vector>
using namespace std;using namespace std;
int main()int main(){{
vector<int> mydata;vector<int> mydata;int userinput;int userinput;dodo{{
cin >> userinput;cin >> userinput;mydata.push_back(userinput);mydata.push_back(userinput);
} while(userinput>0);} while(userinput>0);int sum = 0;int sum = 0;for(int i=0;i<mydata.size();i++)for(int i=0;i<mydata.size();i++){{
sum += mydata[i];sum += mydata[i];}}cout << sum << endl;cout << sum << endl;
}}
Review SessionReview Session
On a piece of paper list the following:On a piece of paper list the following:Topics you think you know very wellTopics you think you know very wellTopics you are concerned about, uncertain of, Topics you are concerned about, uncertain of,
unclear about, etc.unclear about, etc.These will allow me to determine what to These will allow me to determine what to
focus on for the review session. focus on for the review session. Without this feedback, I’ll just answer Without this feedback, I’ll just answer
questions at the review session and won’t questions at the review session and won’t have anything to work from.have anything to work from.