Data Structures & Programming

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

CSE Lecture 12 – Linked Lists …
Tic tac toe v1.2 Made by Rogonow XX PC: X YOU: O The PC-player with mark X goes first. After the PC, you place the mark O at the position of a green oval.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 4 Summation.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Tic tac toe XX PC: X YOU: O The PC-player with mark X goes first. After the PC, you place the mark O at the position of a green circle. If you succeed.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
CSCE 210 Data Structures and Algorithms
Pointers and Dynamic Arrays
Programming Circular Linked List.
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
Priority Queues (Heaps)
Chapter 8 – Arrays and Array Lists
Lecture 7-2 : STL Iterators
Vectors Holds a set of elements, like an array
Doubly Linked List Review - We are writing this code
Dr. Bernard Chen Ph.D. University of Central Arkansas
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
CISC181 Introduction to Computer Science Dr
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Programming Abstractions
Multi-dimensional Array
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.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Lecture 7-2 : STL Iterators
Linked Lists.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 18: Linked Lists.
Popping Items Off a Stack Lesson xx
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 9 One-Dimensional Arrays
Linked Lists.
Programming Abstractions
Doubly Linked List Implementation
CMSC 341 Lists 3.
CMSC 341 Lists 3.
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 16 Section 6.2 Thu, Mar 1, 2007
Lecture 8-2 : STL Iterators and Algorithms
Linked Lists.
Managing 2D Arrays Simple Dynamic Allocation
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
ICOM 4015 Advanced Programming
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
ICOM 4015 Advanced Programming
Abstract Data Types Stacks CSCI 240
21 Data Structures.
Data Structures & Programming
Data Structures & Programming
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

Data Structures & Programming Finishing up Arrays and Linked Lists Golnar Sheikhshab

Chapter 3 outline Arrays Linked List Recursion // next lecture one-dimensional // covered before two-dimensional Linked List Singly Linked List // covered before Doubly Linked List // covered before Circular Linked List Recursion // next lecture

Arrays - two dimensional //Array of arrays - also called matrix sometimes const int max_row_num = 10; const int max_col_num = 20; int A[max_row_num][max_col_num]; // dynamic array - allocating memory int** B = new int*(max_row_num); for (int i=0; i< max_row_num; i++ ) B[i] = new int[max_col_num]; // dynamic array -releasing memory delete [] B[i]; delete [] B;

Vectors instead of Arrays Using STL Vectors instead of arrays is the current norm vector<int> A; // one dimensional vector<vector<int>> B; // two dimensional You have to include the header: #include <vector>

2D arrays and positional games

Tic-Tac-Toe #include <iostream> // I/O definitions using namespace std; // make std:: accessible const int X = 1, O = -1, EMPTY = 0; // possible marks int board[3][3]; // playing board int currentPlayer; // current player (X or O) void clearBoard() { // clear the board for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) board[i][j] = EMPTY; // every cell is empty currentPlayer = X; // player X starts }

Tic-Tac-Toe (2) void putMark(int i, int j) { // mark row i column j board[i][j] = currentPlayer; // mark with current player currentPlayer = -currentPlayer; // switch players } int getWinner() { // who wins? (EMPTY means tie) if (isWin(X)) return X; else if (isWin(O)) return O; else return EMPTY;

Tic-Tac-Toe (3) bool isWin(int mark) { // is mark the winner? int win = 3*mark; // +3 for X and -3 for O return ((board[0][0] + board[0][1] + board[0][2] == win) // row 0 || (board[1][0] + board[1][1] + board[1][2] == win) // row 1 || (board[2][0] + board[2][1] + board[2][2] == win) // row 2 || (board[0][0] + board[1][0] + board[2][0] == win) // column 0 || (board[0][1] + board[1][1] + board[2][1] == win) // column 1 || (board[0][2] + board[1][2] + board[2][2] == win) // column 2 || (board[0][0] + board[1][1] + board[2][2] == win) // diagonal || (board[2][0] + board[1][1] + board[0][2] == win)); // diagonal }

Tic-Tac-Toe (4) void printBoard() { // print the board for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { switch (board[i][j]) { case X: cout << "X"; break; case O: cout << "O"; break; case EMPTY: cout << " "; break; } if (j < 2) cout << "|"; // column boundary if (i < 2) cout << "\n-+-+-\n"; // row boundary

Tic-Tac-Toe (5) int main() { // main program clearBoard(); // clear the board putMark(0,0); putMark(1,1); putMark(0,1); // add the marks putMark(0,2); putMark(2,0); putMark(1,2); putMark(2,2); putMark(2,1); putMark(1,0); printBoard(); // print the final board int winner = getWinner(); if (winner != EMPTY) // print the winner cout << " " << (winner == X ? 'X' : '0') << " wins" << endl; else cout << " Tie" << endl; return EXIT_SUCCESS; }

Tic-Tac-Toe (output) X|X|O -+-+- X|O|O X|O|X X wins

Circularly Linked List A cursor instead of header front back front

CircleList c++ implementation class CircleList { public: CircleList(); ˜CircleList(); bool empty() const; const Elem& front() const; const Elem& back() const; void advance(); void add(const Elem& e); void remove(); private: CNode* cursor; }; typedef string Elem; class CNode { private: Elem elem; CNode* next; friend class CircleList; };

CircleList c++ implementation (2) CircleList::CircleList() : cursor(NULL) { } CircleList::˜CircleList() { while (!empty()) remove(); } bool CircleList::empty() const { return cursor == NULL; } const Elem& CircleList::back() const { return cursor−>elem; } const Elem& CircleList::front() const { return cursor−>next−>elem; } void CircleList::advance() { cursor = cursor−>next; }

CircleList c++ implementation (3) void CircleList::add(const Elem& e) { CNode* v = new CNode; v−>elem = e; if (cursor == NULL) { v−>next = v; cursor = v; } else { v−>next = cursor−>next; cursor−>next = v; }

CircleList c++ implementation (4) void CircleList::remove() { CNode* old = cursor−>next; if (old == cursor) cursor = NULL; else cursor−>next = old−>next; delete old; } back front

Before we move on to recursion ... The most important topics so far: general C++ coding generic programing (how to use templates) how to make and use interfaces how to use arrays and linked list (adding, removing, detecting if it's empty, constructing, and destructing relevant classes)

Reading material Chapter 3 except for recursion