DOC PREVIEW
UE CS 215 - Lecture 18

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 18Project 4-1Project 4-2OutlineWhat Is a Linked List?Node Class 1Node Class 2Node ConstructorUsing Nodes 1Using Nodes 2Using Nodes 3Using Nodes 4Another Use for const 1Another Use for const 2Another Use for const 3Another Use for const 4Linked-List Toolkitlist_length 1list_length 2Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 1Lecture 18No files today.Reminders: Project 3 due today by 4:30pm. Homework 7 due on Wednesday.Project 4 posted. Due the Wednesday after spring break. However, please note that the topic of this project will be on the written midterm exam that is on Wednesday before spring break.Questions?Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 2Project 4Project 4 is to implement a text-based Minesweeper game as specified.Required use of SweeperCell class that is provided. No changes to this class are allowed.Dynamic allocation of grid to user-specified size during construction.No reallocation during operations, but need destructor, copy constructor, and assignment operator.Extra credit opportunities.Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 3Project 4Recommended order of completion:Constructor – allocate grid without placing bombsWrite – will display covered gridGetRows, GetColumns, GetBombs – accessorsConstructor, PlaceBomb – place bombs (without index checking)Uncover, Mark, Unmark – without index checking, can now check if bomb placement worked by uncovering all the cellsRemoveBomb – without index checkingGameWon – can replace random construction temporarily with fixed construction to testMain program game – separate from test driver program, without exception handlingIndex checking for Uncover, Mark, Unmark, PlaceBomb, RemoveBomb, exception handling in main program gameDestructor, copy constructor, assignment operator= – need to do all three at onceExtra creditMonday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 4OutlineWhat is a linked list?Node structure for linked listsIn-line constructorUsing nodesAnother use for constLinked-list toolkitCompute length of a linked listMonday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 5What Is a Linked List?A linked list is a sequence of items arranged one after another, with each item connected to the next one by a link. The last link is a special "end of list" marker.The links impose an order on the items. Thus like an array or vector, we can talk about the position of an item.12.1 14.6 9.3 -4.8Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 6Node StructureTo implement a linked list, we use a node structure that has a data item and a next link that points to the next node as fields. (Note: the textbook creates a node class with operations that are the same as direct access.) To make this structure flexible, a typedef value_type is used for the type of the data item.struct node { typedef double value_type; value_type data; node *next; // Note: using node okay here};3.14data nextMonday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 7Node StructureIt is convenient to provide a constructor for structs, so that the fields are always initialized. For the node struct, we can have a default data value that is the default value of the type, and a default link of 0.The constructor has two received parameters that are the initial values of the attributes. The default data value is value_type(), which is a call to the default constructor of the type. The default next link value is 0 (not NULL).Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 8Node ConstructorSince the node struct is small and only has a constructor, we will in-line the constructor body directly in the struct definition.struct node { typedef double value_type; // CONSTRUCTOR node(const value_type & init_data = value_type(), node *init_link = 0) { data = init_data; next = init_link; } // note: do not need ';' here // FIELDS value_type data; node *next;};Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 9Using NodesPrograms usually do not declare actual node variables. Instead, linked lists are accessed and manipulated using one or more pointers to nodes. When a class object is allocated using new, a constructor is called automatically.Typically, there is a pointer to the first node in the list (the head) called the head pointer. Often there is a pointer to the last node in the list (the tail) called the tail pointer.node *head_ptr;node *tail_ptr;Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 10Using NodesNew nodes are allocated dynamically.node *p = new node; // default constructionAccessing the data in a node requires dereferencing the pointer, then selecting the field.cout << (*p).data << endl;Doing this is so common, there is special syntax for it:cout << p->data << endl;p->data = -4.8;Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 11Using NodesTo create a linked list, the nodes are hooked up to each other.// make q's node points to p's nodenode *q = new node (14.6, p); // explicit-value // construction // create some more nodesnode *r = new node (12.1), *t = new node (9.3);// make r's node points to q's noder->link = q; // insert t's node between q's node and p's nodeq->link = t; t->link = p;Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 12Using Nodes12.1 14.6 9.3 -4.8r q t pHere is a picture of the resulting linked list. The head pointer is r. Generally, there will not be a pointer variable pointing to each node.Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 13Another Use for constWhen pointers are passed as parameters to functions, sometimes we would like to prevent the function from using the pointer to change the pointee. This can be done by using the const keyword in the following way:void Function (const node *ptr);The meaning of const here is that ptr cannot be used to change the node it points to. E.g., the following would not be allowed:ptr->data = 1.2;Monday, February 21 CS 215 Fundamentals of Programming II - Lecture 18 14Another Use for constThis use of const does not mean the node cannot be changed via a different pointer variable. The const only applies to the


View Full Document

UE CS 215 - Lecture 18

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 18
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 18 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 18 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?