Lecture 16 Stacks and Queues CSE 1322 4/26/2018.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
COMP 103 Linked Stack and Linked Queue.
Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
TCSS 342, Winter 2005 Lecture Notes
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
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.
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
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
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,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Object Oriented Programming in Java Habib Rostami Lecture 7.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Week 15 – Monday.  What did we talk about last time?  Tries.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Linked Data Structures
Elementary Data Structures
G64ADS Advanced Data Structures
QueueStack CS1020.
Week 4 - Friday CS221.
September 29 – Stacks and queues
Stacks.
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks, Queues, and Deques
Lecture 21 Stacks and Queues Richard Gesick.
Queues.
Topic 16 Queues "FISH queue: n.
Stacks, Queues, and Deques
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CSE 214 – Computer Science II Stacks
Queues FIFO Enqueue Dequeue Peek.
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.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Stacks and Queues.
ADT Queue (Array Implementation)
More Data Structures (Part 1)
Lecture 2: Stacks and Queues
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks.
Recall: stacks and queues
Building Java Programs
Stacks, Queues, and Deques
Lecture 7: Linked List Basics reading: 16.2
Lecture 9: Stack and Queue
Data Structures & Programming
Presentation transcript:

Lecture 16 Stacks and Queues CSE 1322 4/26/2018

Sample test questions Write a definition for a Node class that holds a number. Write a method that sums up all of the numbers held within a linked list and returns the sum 4/26/2018

Question 1 class Node { public int Data; public Node Next; } 4/26/2018

Question 2 int Sum(Node current) { int s = 0; while (current != null) s += current.Data; current = current.Next; } return s; int Sum(Node current) { if (current == null) return 0; else return current.Data + Sum(current.Next); } 4/26/2018

Stacks Implement a Last-in-First-Out (LIFO) behavior Push() to add an element Pop() to remove an element Peek() returns the top element without removing it from the stack 4/26/2018

Stacks Operations 4/26/2018

Stacks Add and remove from the same side of the internal data structure Easiest to add/remove from the front if it is internally implemented as a ___? Which data structure would you pick to implement a Stack? Why? In making your choice, consider the data structure, orientation, resizing overhead, Big O. Push Pop Peek double Linked list ArrayList array single Linked list 4/26/2018

C# - A Stack as a List public class MyStack { private List<object> list; public MyStack() list = new List<object>(); } public void push(object o) list.Add(o); public object pop() { int pos = list.Count - 1; object temp = list[pos]; list.RemoveAt(pos); return temp; } 4/26/2018

Methods in Stack class Object push(Object element) : Pushes an element on the top of the stack. Object pop() : Removes and returns the top element of the stack. An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty. Object peek() : Returns the element on the top of the stack, but does not remove it. boolean empty() : It returns true if nothing is on the top of the stack. Else, returns false. int search(Object element) : It determines whether an object exists in the stack. If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1. 4/26/2018

Queues Implement a first-in-first-out (FIFO) behavior Enqueue() to add an element Dequeue() to remove an element Add and remove from the opposite sides of the internal data structure 4/26/2018

Stack vs Queue 4/26/2018

Queues Operations 4/26/2018

Queues Which data structure would you pick to implement a Queue? Why? In making your choice, consider the data structure, orientation, resizing overhead, Big O. Enqueue Dequeue double Linked list ArrayList array single Linked list 4/26/2018

Java - A Queue as an Array public class ArrayQueue { private int DEFAULT_SIZE = 5; private Object[] elements; private int numElements; private int front, back; public ArrayQueueC() { elements= new Object[ DEFAULT_SIZE]; numElements=0; front=0; back=0; } public void enqueue(Object item) { numElements++; elements[back]=item; back++; if(back%DEFAULT_SIZE==0 && numElements>0) back=0; if(isFull()) increaseSize(); } . . . } 4/26/2018