C++ is Fun – Part 14 at Turbine/Warner Bros.! Russell Hanson.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Linked Lists.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
C++ is Fun – Part 10 at Turbine/Warner Bros.! Russell Hanson.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Linked List
C++ is Fun – Part 12 at Turbine/Warner Bros.! Russell Hanson.
Linked Lists
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
C++ is Fun – Part Nine at Turbine/Warner Bros.! Russell Hanson.
C++ is Fun – Part 13 at Turbine/Warner Bros.! Russell Hanson.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Trees. Trees Traversal Inorder  (Left) Root (Right) Preorder  Root (Left) (Right) Postorder  (Left) (Right) Root Root LeftRight.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Chapter 5 – Dynamic Data Structure Part 2: Linked List DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Working with Pointers An exercise in destroying your computer.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
C++ is Fun – Part 15 at Turbine/Warner Bros.! Russell Hanson.
Abstract Data Types and a review of C++ programming concepts CS 400/600 – Data Structures.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
1 CSE 2341 Object Oriented Programming with C++ Note Set #21.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
CSCE 210 Data Structures and Algorithms
Lists CS 3358.
List ADT & Linked Lists.
Exercise 1 From Lec80b-Ponter.ppt.
classes and objects review
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
CSCI 3333 Data Structures Linked Lists.
Data Structures and Algorithms IT12112
LinkedList Class.
Chapter 16-2 Linked Structures
Chapter 18: Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

C++ is Fun – Part 14 at Turbine/Warner Bros.! Russell Hanson

Syllabus 1) First program and introduction to data types and control structures with applications for games learning how to use the programming environment Mar ) Objects, encapsulation, abstract data types, data protection and scope April 1-3 3) Basic data structures and how to use them, opening files and performing operations on files – April ) Algorithms on data structures, algorithms for specific tasks, simple AI and planning type algorithms, game AI algorithms April Project 1 Due – April 17 5) More AI: search, heuristics, optimization, decision trees, supervised/unsupervised learning – April ) Game API and/or event-oriented programming, model view controller, map reduce filter – April 29, May 1 7) Basic threads models and some simple databases SQLite May 6-8 8) Graphics programming, shaders, textures, 3D models and rotations May Project 2 Due May 15 9) How to download an API and learn how to use functions in that API, Windows Foundation Classes May ) Designing and implementing a simple game in C++ May ) Selected topics – Gesture recognition & depth controllers like the Microsoft Kinect, Network Programming & TCP/IP, OSC June ) Working on student projects - June Final project presentations Project 3/Final Project Due June 12

Projects 2 Due in Class This Wednesday Please prepare a ~10 minute discussion (PowerPoint, or other) about your program, or your goal with your project, challenges you encountered along the way, how you overcame them, how they overcame you, etc. Share it with the class! 8)

Two dimensional viewing- transformation pipeline

Line clipping against a rectangular clip window

Line Clipping Algorithms

Linked Lists

// Specification file for the NumberList class #ifndef NUMBERLIST_H #define NUMBERLIST_H class NumberList { private: // Declare a structure for the list struct ListNode { double value; // The value in this node struct ListNode *next; // To point to the next node }; ListNode *head; // List head pointer public: // Constructor NumberList() { head = NULL; } // Destructor ~NumberList(); // Linked list operations void appendNode(double); void insertNode(double); void deleteNode(double); void displayList() const; }; #endif

// Implementation file for the NumberList class #include // For cout and NULL #include "NumberList.h" using namespace std; //************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //************************************************** void NumberList::appendNode(double num) { ListNode *newNode; // To point to a new node ListNode *nodePtr; // To move through the list // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode at end. { // Initialize nodePtr to head of list. nodePtr = head; // Find the last node in the list. while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node. nodePtr->next = newNode; } //************************************************** // displayList shows the value * // stored in each node of the linked list * // pointed to by head. * //************************************************** void NumberList::displayList() const { ListNode *nodePtr; // To move through the list // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr points to a node, traverse // the list. while (nodePtr) { // Display the value in this node. cout value << endl; // Move to the next node. nodePtr = nodePtr->next; } //************************************************** // The insertNode function inserts a node with * // num copied to its value member. * //************************************************** void NumberList::insertNode(double num) { ListNode *newNode; // A new node ListNode *nodePtr; // To traverse the list ListNode *previousNode = NULL; // The previous node // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (!head) { head = newNode; newNode->next = NULL; } else // Otherwise, insert newNode { // Position nodePtr at the head of list. nodePtr = head; // Initialize previousNode to NULL. previousNode = NULL; // Skip all nodes whose value is less than num. while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If the new node is to be the 1st in the list, // insert it before all other nodes. if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else // Otherwise insert after the previous node. { previousNode->next = newNode; newNode->next = nodePtr; } //************************************************** // The deleteNode function searches for a node * // with num as its value. The node, if found, is * // deleted from the list and from memory. * //************************************************** void NumberList::deleteNode(double num) { ListNode *nodePtr; // To traverse the list ListNode *previousNode; // To point to the previous node // If the list is empty, do nothing. if (!head) return; // Determine if the first node is the one. if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is // not equal to num. while (nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If nodePtr is not at the end of the list, // link the previous node to the node after // nodePtr, then delete nodePtr. if (nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } //************************************************** // Destructor * // This function deletes every node in the list. * //************************************************** NumberList::~NumberList() { ListNode *nodePtr; // To traverse the list ListNode *nextNode; // To point to the next node // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr is not at the end of the list... while (nodePtr != NULL) { // Save a pointer to the next node. nextNode = nodePtr->next; // Delete the current node. delete nodePtr; // Position nodePtr at the next node. nodePtr = nextNode; } // Implementation file for the NumberList class #include // For cout and NULL #include "NumberList.h" using namespace std; //************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //************************************************** void NumberList::appendNode(double num) { ListNode *newNode; // To point to a new node ListNode *nodePtr; // To move through the list // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode at end. { // Initialize nodePtr to head of list. nodePtr = head; // Find the last node in the list. while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node. nodePtr->next = newNode; } //************************************************** // displayList shows the value * // stored in each node of the linked list * // pointed to by head. * //************************************************** void NumberList::displayList() const { ListNode *nodePtr; // To move through the list // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr points to a node, traverse // the list. while (nodePtr) { // Display the value in this node. cout value << endl; // Move to the next node. nodePtr = nodePtr->next; } //************************************************** // The insertNode function inserts a node with * // num copied to its value member. * //************************************************** void NumberList::insertNode(double num) { ListNode *newNode; // A new node ListNode *nodePtr; // To traverse the list ListNode *previousNode = NULL; // The previous node // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (!head) { head = newNode; newNode->next = NULL; } else // Otherwise, insert newNode { // Position nodePtr at the head of list. nodePtr = head; // Initialize previousNode to NULL. previousNode = NULL; // Skip all nodes whose value is less than num. while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If the new node is to be the 1st in the list, // insert it before all other nodes. if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else // Otherwise insert after the previous node. { previousNode->next = newNode; newNode->next = nodePtr; } //************************************************** // The deleteNode function searches for a node * // with num as its value. The node, if found, is * // deleted from the list and from memory. * //************************************************** void NumberList::deleteNode(double num) { ListNode *nodePtr; // To traverse the list ListNode *previousNode; // To point to the previous node // If the list is empty, do nothing. if (!head) return; // Determine if the first node is the one. if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is // not equal to num. while (nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If nodePtr is not at the end of the list, // link the previous node to the node after // nodePtr, then delete nodePtr. if (nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } //************************************************** // Destructor * // This function deletes every node in the list. * //************************************************** NumberList::~NumberList() { ListNode *nodePtr; // To traverse the list ListNode *nextNode; // To point to the next node // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr is not at the end of the list... while (nodePtr != NULL) { // Save a pointer to the next node. nextNode = nodePtr->next; // Delete the current node. delete nodePtr; // Position nodePtr at the next node. nodePtr = nextNode; }

// Implementation file for the NumberList class #include // For cout and NULL #include "NumberList.h" using namespace std; //************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //************************************************** void NumberList::appendNode(double num) { ListNode *newNode; // To point to a new node ListNode *nodePtr; // To move through the list // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode at end. { // Initialize nodePtr to head of list. nodePtr = head; // Find the last node in the list. while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node. nodePtr->next = newNode; } //************************************************** // displayList shows the value * // stored in each node of the linked list * // pointed to by head. * //************************************************** void NumberList::displayList() const { ListNode *nodePtr; // To move through the list // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr points to a node, traverse // the list. while (nodePtr) { // Display the value in this node. cout value << endl; // Move to the next node. nodePtr = nodePtr->next; } //************************************************** // The insertNode function inserts a node with * // num copied to its value member. * //************************************************** void NumberList::insertNode(double num) { ListNode *newNode; // A new node ListNode *nodePtr; // To traverse the list ListNode *previousNode = NULL; // The previous node // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (!head) { head = newNode; newNode->next = NULL; } else // Otherwise, insert newNode { // Position nodePtr at the head of list. nodePtr = head; // Initialize previousNode to NULL. previousNode = NULL; // Skip all nodes whose value is less than num. while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If the new node is to be the 1st in the list, // insert it before all other nodes. if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else // Otherwise insert after the previous node. { previousNode->next = newNode; newNode->next = nodePtr; } //************************************************** // The deleteNode function searches for a node * // with num as its value. The node, if found, is * // deleted from the list and from memory. * //************************************************** void NumberList::deleteNode(double num) { ListNode *nodePtr; // To traverse the list ListNode *previousNode; // To point to the previous node // If the list is empty, do nothing. if (!head) return; // Determine if the first node is the one. if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is // not equal to num. while (nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If nodePtr is not at the end of the list, // link the previous node to the node after // nodePtr, then delete nodePtr. if (nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } //************************************************** // Destructor * // This function deletes every node in the list. * //************************************************** NumberList::~NumberList() { ListNode *nodePtr; // To traverse the list ListNode *nextNode; // To point to the next node // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr is not at the end of the list... while (nodePtr != NULL) { // Save a pointer to the next node. nextNode = nodePtr->next; // Delete the current node. delete nodePtr; // Position nodePtr at the next node. nodePtr = nextNode; } // Implementation file for the NumberList class #include // For cout and NULL #include "NumberList.h" using namespace std; //************************************************** // appendNode appends a node containing the * // value pased into num, to the end of the list. * //************************************************** void NumberList::appendNode(double num) { ListNode *newNode; // To point to a new node ListNode *nodePtr; // To move through the list // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode at end. { // Initialize nodePtr to head of list. nodePtr = head; // Find the last node in the list. while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node. nodePtr->next = newNode; } //************************************************** // displayList shows the value * // stored in each node of the linked list * // pointed to by head. * //************************************************** void NumberList::displayList() const { ListNode *nodePtr; // To move through the list // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr points to a node, traverse // the list. while (nodePtr) { // Display the value in this node. cout value << endl; // Move to the next node. nodePtr = nodePtr->next; } //************************************************** // The insertNode function inserts a node with * // num copied to its value member. * //************************************************** void NumberList::insertNode(double num) { ListNode *newNode; // A new node ListNode *nodePtr; // To traverse the list ListNode *previousNode = NULL; // The previous node // Allocate a new node and store num there. newNode = new ListNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (!head) { head = newNode; newNode->next = NULL; } else // Otherwise, insert newNode { // Position nodePtr at the head of list. nodePtr = head; // Initialize previousNode to NULL. previousNode = NULL; // Skip all nodes whose value is less than num. while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If the new node is to be the 1st in the list, // insert it before all other nodes. if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else // Otherwise insert after the previous node. { previousNode->next = newNode; newNode->next = nodePtr; } //************************************************** // The deleteNode function searches for a node * // with num as its value. The node, if found, is * // deleted from the list and from memory. * //************************************************** void NumberList::deleteNode(double num) { ListNode *nodePtr; // To traverse the list ListNode *previousNode; // To point to the previous node // If the list is empty, do nothing. if (!head) return; // Determine if the first node is the one. if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { // Initialize nodePtr to head of list nodePtr = head; // Skip all nodes whose value member is // not equal to num. while (nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } // If nodePtr is not at the end of the list, // link the previous node to the node after // nodePtr, then delete nodePtr. if (nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } //************************************************** // Destructor * // This function deletes every node in the list. * //************************************************** NumberList::~NumberList() { ListNode *nodePtr; // To traverse the list ListNode *nextNode; // To point to the next node // Position nodePtr at the head of the list. nodePtr = head; // While nodePtr is not at the end of the list... while (nodePtr != NULL) { // Save a pointer to the next node. nextNode = nodePtr->next; // Delete the current node. delete nodePtr; // Position nodePtr at the next node. nodePtr = nextNode; }

Russells-MacBook-Pro-2:Chapter 17 russell$ cat Pr17-1.cpp // This program demonstrates a simple append // operation on a linked list. #include #include "NumberList.h" using namespace std; int main() { // Define a NumberList object. NumberList list; // Append some values to the list. list.appendNode(2.5); list.appendNode(7.9); list.appendNode(12.6); list.displayList(); return 0; } Russells-MacBook-Pro-2:Chapter 17 russell$ g++ Pr17-1.cpp NumberList.cpp Russells-MacBook-Pro-2:Chapter 17 russell$./a.out Class Exercise: Append nodes to LinkedList

$./4out Here are the initial values: Now deleting the node in the middle. Here are the nodes left Now deleting the last node. Here are the nodes left. 2.5 Now deleting the only remaining node. Here are the nodes left.

Recall, Project 2 due this Wednesday

Boost Libraries

$ g++ fig23_02.cpp fig23_02.cpp:5:17: error: regex: No such file or directory fig23_02.cpp: In function ‘int main()’: fig23_02.cpp:11: error: ‘regex’ was not declared in this scope fig23_02.cpp:11: error: expected `;' before ‘expression’ fig23_02.cpp:20: error: ‘smatch’ was not declared in this scope fig23_02.cpp:20: error: expected `;' before ‘match’ fig23_02.cpp:23: error: ‘match’ was not declared in this scope fig23_02.cpp:23: error: ‘expression’ was not declared in this scope fig23_02.cpp:24: error: ‘regex_constants’ has not been declared fig23_02.cpp:24: error: ‘regex_search’ was not declared in this scope

// Demonstrating regular expressions. #include using namespace std; // allows use of features in both std and std::tr1 int main() { // create a regular expression regex expression( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" ); // create a string to be tested string string1 = "Jane's Birthday is \n" "Dave's Birthday is \n" "John's Birthday is \n" "Joe's Birthday is ”; // create a std::tr1::smatch object to hold the search results smatch match; // match regular expression to string and print out all matches while ( regex_search( string1, match, expression, regex_constants::match_not_eol ) ) { cout << match.str() << endl; // print the matching string // remove the matched substring from the string string1 = match.suffix(); } // end while } // end of function main

Validating User Input with Regex & Boost

Book.h, Book.cpp, and SharedPtrExample.cpp are in the Google Drive

Function objects in the STL

Ende