Tirgul 3 Topics of this Tirgul: Lists Vectors Stack Queue.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
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.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
CMPT 225 Stacks.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder.
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.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
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.
TCSS 342, Winter 2005 Lecture Notes
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Stack Queue.
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.
Stacks, Queues, and Deques
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.”
Chapter 7 Stacks II CS Data Structures I COSC 2006
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
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 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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,
Click to edit Master text styles Stacks Data Structure.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Computing with C# and the .NET Framework
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
CSC 205 – Java Programming II
Stacks, Queues, and Deques
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Tirgul 3 Topics of this Tirgul: Lists Vectors Stack Queue

Abstract List Operations Create an empty list Test if the list is empty Provide access to elements at different positions in the list: –first –last –i-th element Insert a new element into the list Remove an element from the list Lookup an element by its contents Retrieve the contents of an element Replace the contents of an element Also useful: next(), previous()

Linked List with no Pointers 2 linked lists in one array, one for the occupied cells and one for the free cells. Instead of using pointers to the next node, each cell holds the data + the index of the next node in the list. When adding an object a cell is removed form the free list and it’s index is added to the occupied list. What is it good for ? –Garbage collection. –A solution for a language with no pointers. ( and there are such languages!)

Dynamic Arrays (Vectors) Many times you’ll want to use arrays as for implementing advanced data structures. There is one problem though, the size of an array is predefined… You’ll probably say, “My array is full? So what? Let’s just create a bigger array and copy everything to it.” But what about complexity? Looking at the cost of a single operation will not do, since it depends on the current state of the array. In order to evaluate our time efficiency we use “Amortized Analysis”.

Dynamic Arrays (Vectors) Let a be the initial size of array V Each time the array becomes full, we increase its size by c If the array is extended k times then n = a + ck The total number of basic operations is: ( a ) + ( a + c ) + ( a +2 c ) + … + ( a +( k –1) c ) = a * k + c (1+2+…+( k –1)) = a * k + c * k ( k –1)/2 = O ( k 2 ) = O ( n 2 ) We paid an amortized cost of n basic operations for each insert operation.

Dynamic Arrays (Vectors) Let a be the initial size of array V This time, each time the array becomes full, we will double its size. If the array is extended k times then n = a *2 k The total number of basic operations is: ( a ) + (2* a ) + (2 2 * a ) + … + (2 k * a ) = = O ( k ) = O ( n ) We paid an amortized cost of one basic operation for each insert operation. But what happens if we allow the array to shrink as well?

Stack A collection of items that complies to a Last-In-First-Out (LIFO) policy: void push(Object o) - adds the object o to the collection. Object pop() - returns the most recently added object (and removes it from the collection). Object top() - returns the most recently added object (and leaves the stack unchanged).

Stack Implementation Using an Array class StackA { private int maxSize; private int[] items; private int top; public StackA(int size) { maxSize = size; items = new int[maxSize]; top = -1; } … // Stack operations }

Stack Implementation Using an Array public boolean isEmpty() { return (top == -1); } public void push(int item) { if (top >= maxSize-1) error(…); items[++top] = item; } public int pop() { if (isEmpty()) error(…); return items[top--]; } public int top() { if (isEmpty()) error(…); return items[top]; }

java.util.Stack This java class implements a last-in-first-out stack of objects. The hierarchy: Object Vector(cloneable) Stack It has five methods. –empty()-checks if the stack is empty. –peek()-returns the top object without removing it. –pop()-pops… –push()-pushes… –search()-search for item in the stack.

Stack example: delimiter check Legal delimiters: {,[,(,),],} Each opening delimiter must have a matching closing one. Proper nesting: –a{bc[d]e}f(g) OK –a{bc[d}e]f(g) incorrect We can perform this task easily using a stack!

Stack example: delimiter check // For all characters c of a string do: switch (c) { case '(', '[', '{': stack.push(c); break; case ') ', '] ', '} ': if (stack.isEmpty()) error(…); if (stack.pop() does not match c) error(…); default: break; } // When finished: if (!stack.isEmpty()) error(…);

Using Stacks to Eliminate Recursion Hanoi Tower, without recursion… push( Hanoi('s', 't', 'm', k) ) while ( stack not empty ) x = pop(); if ( x is of type Hanoi ) if ( x.discs > 1 ) push( Hanoi(x.middle, x.to, x.from, x.discs-1) ) push( Move(x.from, x.to) ) push( Hanoi(x.from, x.middle, x.to, x.discs-1) ) else push( Move(x.from, x.to) ) else // x is of type Move print( x.from + " -> " + x.to); Elements in stack are of two types: Hanoi(from, to, middle, discs) Move(from, to)

Example H(s,t,m,3)H(m,t,s,2) M(s,t) H(s,m,t,2) Top of stack H(m,t,s,2) M(s,t) H(t,m,s,1) H(s,t,m,1) M(s,m) H(m,t,s,2) M(s,t) H(t,m,s,1) M(s,m) M(s,t) s  t H(m,t,s,2) M(s,t) H(t,m,s,1) M(s,m) H(m,t,s,2) M(s,t) H(t,m,s,1)M(t,m) s  m

Example H(m,s,t,1) M(m,t) H(s,t,m,1) t  m H(m,t,s,2) M(s,t) s  t H(m,t,s,2) H(s,t,m,1) M(m,t) M(m,s) H(s,t,m,1) M(m,t) M(s,t) m  s m  t s  t

Queue A collection of items that complies to a First-In-First-Out (FIFO) policy: void enqueue(Object o) - adds the object o to the collection. Object dequeue() - returns the least recently added object (and removes it from the collection). Object front() - returns the least recently added object (and leaves the queue unchanged). Also called peek().

Queue Implementation using a (circular) array class QueueA { private int maxSize; private int[] items; private int front; private int back; private int numItems; public QueueA(int size) { maxSize = size; items = new int[maxSize]; front = 0; back = maxSize-1; numItems = 0; } … // Queue operations }

Queue Implementation using a (circular) array public boolean isEmpty() { return (numItems == 0); } public boolean isFull() { return (numItems == maxSize); } public void enqueue(int item) { if (isFull()) error(…); back = (back+1) % maxSize; items[back] = item; numItems++; }

Queue Implementation using a (circular) array public int dequeue() { if (isEmpty()) error(…); int temp = items[front]; front = (front+1) % maxSize; numItems--; return temp; } Public int peek() { if (isEmpty()) error(…); return items[front]; } Question: can we do without keeping track of numItems?

The master theorem Then 1.If f(n) = then T(n) = 2.If f(n) = then T(n) = 3.If f(n) = and a*f(n/b) ≤ c f(n) then T(n) = Let be constants, let f(n) be a function such that n ≥ 0 => f(n) ≥ 0 and let T(n) be defined on non-negative integers by the recurrence: T(n) = a * T(n/b) + f(n)

The master theorem ( class version ) Then 1.If a/bc log b a) then T(n) = Θ(n c ) 2.If a/bc = 1 (c = log b a)then T(n) = Θ(n c log b n) 3.If a/bc > 1 (c < log b a)then T(n) = Θ(log b a) Let be constants, and let T(n) be defined on non-negative integers by the recurrence: T(n) = aT(n/b) + n c

The master theorem T(n) = aT(n/b) + n c 1.If a/bc log b a) then T(n) = Θ(n c ) 2.If a/bc = 1 (c = log b a)then T(n) = Θ(n c log b n) 3.If a/bc > 1 (c < log b a)then T(n) = Θ(log b a) T(n) = a*T(n/b) + f(n) 1.If f(n) = then T(n) = 2.If f(n) = then T(n) = 3.If f(n) = and a*f(n/b) ≤ c f(n) then T(n) =