300 likes | 321 Views
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:
E N D
CS 240: Data Structures Tuesday, June 17th Vector, Linked List
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).
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.
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
Back to lists… You will being doing this for a grade on Tuesday.
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 *
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
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
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.
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?
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!
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
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
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
Removal • Removal a node has similar questions. • What happens to who pointed to us? • What happened to who we pointed to?
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
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
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.
Searching • Start at first. • If first points somewhere, follow it • Check the current value • Follow path to next node if it exists
Outputting • Start at first. • If first points somewhere, follow it • Print the current value • Follow path to next node if it exists
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.
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
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?
Linked List • Let us finish up writing code for Linked List.
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
Current Location • Error handing: • int geterror() • Insertion and Removal • bool insertHere(int newvalue) • int removeHere()
New functionality • Searching • int howManyOf(int searchfor) • int & operator [](int index)
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.
#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
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.