CS1S467 GUI Programming LECTURE 11 Collections

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Building Java Programs
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Understanding Data Types and Collections Lesson 2.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Arrays and Collections Tonga Institute of Higher Education.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Week 15 – Monday.  What did we talk about last time?  Tries.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Building Java Programs
Computing with C# and the .NET Framework
18 Chapter Stacks and Queues
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Understanding Algorithms and Data Structures
Stacks and Queues.
CC 215 Data Structures Queue ADT
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
Stacks and Queues.
Building Java Programs
CST230: Applied Data Structures
Data Structures and Database Applications Queues in C#
HW-6 Deadline Extended to April 27th
Stacks and Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks and Queues.
Stacks, Queues, and Deques
Lesson Objectives Aims
CSC 143 Queues [Chapter 7].
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Abstract Data Type Abstract Data Type as a design tool
slides created by Marty Stepp
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Introduction to Data Structure
More Data Structures (Part 1)
Lecture 2: Stacks and Queues
Stacks and Queues: Concepts and Implementations
The Generic List<> Collection class
Fundaments of Game Design
Generics, Stack, Queue Based on slides by Alyssa Harding
Dynamic allocation (continued)
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks and Queues.
Data Structures & Programming
Presentation transcript:

CS1S467 GUI Programming LECTURE 11 Collections

Lecture Slides are unsuitable for learning how to program! You just get an illusion of learning! Programming is a craft. You have to do it to learn it. 2

Review of Lab Session Sorting arrays Searching through arrays

Coursework 1 Deadline: Friday 15, 23:59, Blackboard Extra lab sessions Read submission instructions Read the marking grid (load/save highscore? Chart used? Finding max and min values, ...) Don’t forget to include testing document installation documentation game rules document Extra lab sessions Wednesday 6 and 13, 13:00 to 15:00 J210 No lecture next Monday

Today Overview Queues & Stacks what data structure to use when What they are methods how to use examples

Separating Boys from Men Effective & appropriate use of data structures separates expert programmer from novice The expert programmer recognises that: organising data into appropriate data structures makes it easier to work with the data, using data abstraction makes it easier to come up with a good solution to programming problems.

Collections vs. Primitive Data Types Primitive Data Types: int, float, char, …. not objects, can’t "do" things (sorting, searching) simple to use, access and understand "Hybrid": arrays and structs "constructors", get & set methods almost objects Collections: List, ArrayList, LinkedList, Queues, Stacks, … objects, can "do" things (sorting, searching) more difficult to use, access and understand

Two Collection Groups Linear Indexed Lists, LinkedLists, Queues, Stacks, etc. fast sequential traverse slow random access Indexed ArrayLists, SortedLists, etc. slow sequential traverse fast random access

Collections: Overview (When to use What) Queue Stack LinkedList SortedList List ArrayList Expand/shrink Add/remove fast slow Single use y (y) Sorting x Depends on algorithm Searching Random access Sequential access Example print queue 'Undo' large data sorting database Guide on these and more Collections: http://msdn.microsoft.com/en-us/library/6tc79sx1.aspx

Queues & Stacks Stacks Queues Last-in-First-out (LiFo): data added and removed only from top of list used for expression evaluation function call handling 'Undo' functionality Queues First-in-First-out (FiFo): data are added at one end and removed from the other end of a list. prioritise operating system processes simulate events in the real world batch processing

Basic Stack Methods Push & Pop Peek just looking at topmost without removing

Other Stack Methods Clear Clone Contains CopyTo, ToArray completely empties the Stack Clone creates a copy Contains checks if an element is in the Stack (sequentially -> slow) CopyTo, ToArray copies into a 1D array Count (not a method, a property) amount of elements on the Stack Full list on: http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx

How to Use the Stack Class using System; using System.Collections; public class SamplesStack { public static void Main() // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push("World"); myStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine( "myStack" ); Console.WriteLine( "\tCount: {0}", myStack.Count ); Console.Write( "\tValues:" ); PrintValues( myStack ); } public static void PrintValues( IEnumerable mS ) { foreach ( Object obj in mS ) Console.Write( " {0}", obj ); }

Basic Queue Methods Enqueue & Dequeue Peek just looking at first in queue without removing

Other Queue Methods (Identical to Stack) Clear completely empties the Queue Clone creates a copy Contains checks if an element is in the Queue (sequentially -> slow) CopyTo, ToArray copies into a 1D array Count (not a method, a property) returns amount of elements in the Queue Full list on: http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx

How to Use the Queue Class using System; using System.Collections; public class SamplesQueue { public static void Main() // Creates and initializes a new Queue. Queue myQ = new Queue(); myQ.Enqueue("Hello"); myQ.Enqueue("World"); myQ.Enqueue("!"); // Displays the properties and values of the Queue. Console.WriteLine( "myQ" ); Console.WriteLine( "\tCount: {0}", myQ.Count ); Console.Write( "\tValues:" ); PrintValues( myQ ); } public static void PrintValues( IEnumerable mQ ) { foreach ( Object obj in mQ ) Console.Write( " {0}", obj ); }

Example using System; using System.Collections; public class Palindrome { public static void Main() bool isPalindrome=true; string ch; Stack myS = new Stack(); Console.WriteLine("Enter a word to check"); string wordToCheck = Console.ReadLine(); for (int x = 0; x < wordToCheck.Length; x++) myS.Push(wordToCheck.Substring(x, 1)); } int pos = 0; while (myS.Count > 0) ch = myS.Pop().ToString(); if (ch != wordToCheck.Substring(pos, 1)) isPalindrome = false; break; pos++; Example // Some variables we need // Create and initialise a new Stack // Ask user to enter a word to check // Push the word onto the stack, letter by letter // In effect the stack now holds the word BACKWARDS // Now compare (letter by letter) the word entered // to the reversed version on the Stack. If at any // point the letters are different, then the word is // not a palindrome.

Lists Lists are similar to arrays Either this way Or this way Lists have to be given a size before use: Either this way Or this way (automatic run-time size-adjustment) (explicit size definition)

Lists Lists are also dissimilar to arrays List are auto-initialised to zero List are not fixed in size. They can grow at run-time

Lists Lists are objects They have methods (already used the .Add() method) Example: Searching

Lists Lists are objects They have methods (already used the .Add() method) Example: Sorting Ah – and how to sort descending?

Lists Lists are objects They have methods (already used the .Add() method) Example: Inserting

Lists and structs Lists can store anything int, double, float, char, ..... arrays structs

Lists and structs Sorting a List of structs sort by what? needs a sort definition

Lists and structs More after Christmas break a 5 week project using Lists, structs and more visual components a record (or whatever) collection / database load, save, sort, search, ...

Reminder End of Lecture 11 Deadline: Friday 15, 23:59, Blackboard Read submission instructions Read the marking grid (load/save highscore? Chart used? Finding max and min values, ...) Don’t forget to include testing document installation documentation game rules document Extra lab sessions Wednesday 6 and 13, 13:00 to 15:00 J210 No lecture next Monday End of Lecture 11