Data Structures Intro2CS – week 11 1. Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

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  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
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,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Stacks And Queues Chapter 18.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
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.
Linked Data Structures
Stacks II David Lillis School of Computer Science and Informatics
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Week 4 - Friday CS221.
September 29 – Stacks and queues
Chapter 15 Lists Objectives
CS 1114: Implementing Search
Week 3 - Friday CS221.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks and Queues.
HW-6 Deadline Extended to April 27th
Stacks and Queues.
Basic Data Types Queues
Building Java Programs
i206: Lecture 11: Stacks, Queues
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
i206: Lecture 10: Lists, Stacks, Queues
Queues.
Stacks and Queues.
Lesson Objectives Aims
Data Structures – Stacks and Queus
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
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.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks and Queues.
More Data Structures (Part 1)
Lecture 2: Stacks and Queues
Stacks and Queues CSE 373 Data Structures.
Generics, Stack, Queue Based on slides by Alyssa Harding
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
CSCS-200 Data Structure and Algorithms
Stacks and Queues.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
DATA STRUCTURES IN PYTHON
Data Structures & Programming
Presentation transcript:

Data Structures Intro2CS – week 11 1

Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: – Push inserts an item into the stack – Pop will remove and return an item (if the stack isn’t empty). The item that is returned: the last item inserted (that was not already removed) – Is_empty will return “True” if every item that has been inserted, was also removed. “False” otherwise LIFO (last-in-first-out) 2

Example 3

A simple implementation 4

Using stacks Stacks are useful data structures. They can be used to solve interesting algorithmic problems. Example that you’ve encountered: Matching brackets. Suppose we want to check if an expression has well balanced brackets. – Brackets may be of several types. [ [ ( ) ] ] {( )} balanced [ } not balanced [(]) not balanced [()not balanced 5

Some helper functions: The main idea: Opening brackets are placed in the stack, waiting to be “closed”. Closing brackets are matched to the last opened ones that weren’t closed (at the top of the stack) 6

7

Linked Lists 8

Python lists in memory Python lists are represented as continuous blocks in memory. If we want to make the block size larger, we have to re-allocate memory and copy the contents. Many times Python does this for us behind the scenes 9 HELLOWORLD HELLOWORL

Linked Lists Have many blocks instead of one Chain them: remember address of the next one Now we can easily add blocks 10 H L L EO W

Linked Lists 11 next data next data next data next data head None

How to work with linked lists To access the data in the linked list: But this is cumbersome. 12 head.data head.next.data head.next.next.data … next data next data next data next data head None

How to work with linked lists Instead, we usually work with a moving reference 13 cur = head print(cur.data) cur = cur.next print(cur.data) … next data next data next data next data head None cur

Iterating over lists So in order to print a linked list: 14 cur = head while cur!=None: print(cur.data) cur = cur.next

Inserting into a list Inserting before the head: 15 next data next data next data next data head None next data new_node = Node("new val", head) head = new_node Or simply write: head = Node(“new val”, head)

Inserting into a list Inserting at position cur: 16 next data next data next data next data head None next data cur.next = Node(“some val”,cur.next) cur Notice that insertion takes O(1), while in python lists (arrays) it takes O(n)

Removing from a list Removing the first item: 17 next data next data next data next data head None head = head.next

Removing from a list Removing the node after position cur: What if we want to remove the node at position cur? 18 next data next data next data next data head None cur.next = cur.next.next cur

Some list operations are more costly. How do we get to the node before the current one? How quickly can we get to the k’th node in the list? How quickly can we print the list in reverse? – What if we only have O(1) memory to use? 19

A stack using a linked list 20

Improving the stack class To easily convert the stack into strings: 21

Recursion and linked lists Recursion is often very natural on linked lists. They have a “recursive” structure: If you remove the head of a linked list, you have a shorter list. Example: – Let’s compute the size of our stack recursively. 22

Queues 23

The Queue ADT A container with 3 basic actions: – enqueue(item) – dequeue() – is_empty() Semantics: FIFO (first-in-first-out) – “enqueue” inserts an item – “dequeue” will return an item (if the queue isn’t empty). The item that is returned: the first item inserted (that was not already removed) – Is_empty will return “True” if every item that has been inserted, was also removed. “False” otherwise. 24

Implementing the Queue Insertion requires all items to be “pushed” forward. So enqueue actually runs in O(n) where n is the number of items in the queue. Not efficient. 25

Implementation with a linked list 26 Where new items will be inserted. We will keep track of the size of the list to avoid having to count it every time.

27

Let’s also add an iterator to the Queue class, so that we can run over it with loops. (The iterator here doesn’t remove items) We create the iterator using a generator: 28

General Graphs Nodes (Objects) can have more than one reference. Example: think of implementing a Class “Person” Each Person has a list of friends, which are also of the same “Person” class. A “Social Network” 29 Joe Jill John Jack Jen

Extra – Another Queue Implementation 30

An efficient implementation using Python lists The idea: hold start index & size of current data. Enqueue at end (at location start+size). Dequeue at start. Let index “loop around”. end = (start+size)%len(list) 31 OWORLDHELL start end

32

33