What is an algorithm?

Similar documents
Lecture 12 ADTs and Stacks

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

CSCA48 Term Test 1 Seminar

Student Number: Lab day:

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue.


CSC148 Week 2. Larry Zhang

Queues. Queue ADT Queue Implementation Priority Queues

Walk through previous lectures

Computer Science Fundamentals 107

CSC148H Week 3. Sadia Sharmin. May 24, /20

Basic Data Structures

Stack. Stack theory. Stack exercises. Algolab (index.html#chapters) Out[1]: Chapter 2.2: Data Structures - Stacks and.

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

LIFO : Last In First Out

Lecture 4 Stack and Queue

Priority Queue ADT. Revised based on textbook author s notes.

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Data structure and algorithm in Python

Name CPTR246 Spring '17 (100 total points) Exam 3

Basic Data Structures 1 / 24

csci 210: Data Structures Stacks and Queues

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CS 206 Introduction to Computer Science II

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

Stack and Queue. Stack:

Information Science 2

CSC148-Section:L0301

CMSC 132: Object-Oriented Programming II. Stack and Queue

Data Structures I: Linked Lists

PA3 Design Specification

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

Lecture 3: Stacks & Queues

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Scientific Programming. Data structures

Abstract Data Types Chapter 1

15. Stacks and Queues

Scientific Programming. Data structures

CS 206 Introduction to Computer Science II

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

util.py Mon Jul 12 21:42: : # util.py 2: # : # Licensing Information: Please do not distribute or publish solutions to this 4: #

PIC 16: Midterm. Part 1. In this part, you should not worry about why your class has the name that it does.

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Stack Abstract Data Type

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CSE 143. Lecture 4: Stacks and Queues

ONE MARKS QUESTION AND ANSWERS

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Stacks. Revised based on textbook author s notes.

Building Java Programs

Problem with Scanning an Infix Expression

Linked Lists, Stacks, Queues, and Priority Queues

Lecture 2: Implementing ADTs

Stacks and Queues. David Greenstein Monta Vista

Data Abstraction and Specification of ADTs

Stacks and Queues

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Apply to be a Meiklejohn! tinyurl.com/meikapply

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

Lecture 2: Stacks and Queues

Lecture Data Structure Stack

Foundations of Data Structures

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

CS 331 Midterm Exam 2

Download Python from Any version will do for this class

CS 1316 Exam 2 Summer 2009

Computer Science First Exams Pseudocode Standard Data Structures Examples of Pseudocode

Ashish Gupta, Data JUET, Guna

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

4.3 Stacks and Queues. Stacks. Stacks and Queues. Stack API

CSI33 Data Structures

CS 331 Fall 2017 Midterm Exam 2

15-110: Principles of Computing, Spring 2018

CS-141 Exam 2 Review April 4, 2015 Presented by the RIT Computer Science Community

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

STACKS AND QUEUES. Problem Solving with Computers-II

! Set of operations (add, remove, test if empty) on generic data. ! Intent is clear when we insert. ! Which item do we remove? Stack.

Your Topic Goes Here Queue

Data structure and algorithm in Python

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

infix expressions (review)

CS 331 Summer 2017 Final Exam

Transcription:

Announcements CS 142 Stacks & Queues Program 3 has been assigned due 10/6 by 11:55pm Program details on website 2 Creating new Data Structures We ve used objects to create new data types such as Point, Date, Rational, BankAccount, etc. Now we re using objects to create new data structures Linked lists are a dynamic data structure, allocating the needed memory when the program is initiated. Insertion and deletion node operations are easily implemented in a linked list. Modification to the head of the linked list is a special case. Linked lists are a very common data structure used in computer science. Traversing Thru a Linked List Traversal refers to the process of systematically visiting each node. Use an external reference that starts at the first node in the list As we visit each node, we move the reference to the next node by traversing the next reference. 3 4 1

Size Method Search Method Returns total number of Nodes in the list If list is empty, returns 0 mylist.search(17) #Returns True or False 5 6 2 step process again 1. Find the node Remove Method Remove Method 2. Remove node from the linked list Removing an Item from the Middle of the List Removing the First Node from the List 7 8 2

Ordered Linked List Runtimes of Methods What are the differences? How do we keep the list in order at all times? Method Linked List (Unordered) Linked List (Ordered) isempty O(1) O(1) Add O(1) Size Search Remove Method Append Insert Remove In (contains) Len Python List O(1) O(1) 9 10 Stacks Another new data structure! Commonly implemented using linked lists However, in Python, easier to use the built in list data structure, rather than a linked structure Stack Definition Very useful for maintaining particular orders of items List where you only have access to the end Insert or remove items from 1 end of the list Can only see contents of single item at end of the list Think of this as a Pez dispenser You load it from the top, and at any given time, you can only see the top pez and remove the top pez (you can t remove the bottom pez) Pushing an item onto the stack (loading a pez into dispenser) Popping: removing top item from stack Last item added is always first item popped back off LIFO = last in, first out 9/25/2014 11 9/25/2014 12 3

Applications for a Stack Typical Stack Methods Editing commands (undo function) Back button on your web browser Useful for designing algorithms to evaluate and translate expressions. Can provide a reversal characteristic. Stack()- creates a new stack that is empty. It needs no parameters and returns an empty stack. push(item) - adds a new item to the top of the stack. It needs the item and returns nothing. pop() - removes the top item from the stack. It needs no parameters and returns the item. The stack is modified. peek() - returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified. isempty() - tests to see whether the stack is empty. It needs no parameters and returns a boolean value. size() - returns the number of items on the stack. It needs no parameters and returns an integer. * The top item refers to the last item in the list 13 14 Stack.py Stack.py Using Linked List class Stack(object): def init (self): self.items = [] def isempty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items) class Stack(object): def init (self): self.head = None def isempty(self): return self.head == None def push(self, item): temp = Node(item) temp.setnext(self.head) self.head = temp def pop(self): if self.isempty(): raise IndexError( Empty stack.") else: temp = self.head.getdata() self.head = self.head.getnext() return temp * Code in Public directory def peek(self): return self.head.getdata() def size(self): current = self.head count = 0 while current!= None: count = count + 1 current = current.getnext() return count 15 16 4

Queue Data Structure Typical Queue Methods Def: ordered collection of items where the addition of new items happens at one end, called the rear, and the removal of existing items occurs at the other end, commonly called the front. FIFO, first-in first-out: queue == standing in a line Typically use a linked list to implement Common Applications: Computer laboratory has 30 computers networked with a single printer. When students want to print, their print tasks get in line with all the other printing tasks that are waiting. Operating systems use a number of different queues to control processes within a computer (scheduling operations, like when you re typing faster than the computer can output the keystrokes) Queue() - creates a new queue that is empty. It needs no parameters and returns an empty queue. enqueue(item) - adds a new item to the rear of the queue. It needs the item and returns nothing. dequeue() - removes the front item from the queue. It needs no parameters and returns the item. The queue is modified. isempty() - tests to see whether the queue is empty. It needs no parameters and returns a boolean value. size() - returns the number of items in the queue. It needs no parameters and returns an integer. 17 18 5