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

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
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.
CS Data Structures II Review COSC 2006 April 14, 2017
© 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.
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.
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
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
Chapter 7 Stacks II CS Data Structures I COSC 2006
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
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)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
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.
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.
Elementary Data Structures
QueueStack CS1020.
Week 4 - Friday CS221.
September 29 – Stacks and queues
Week 3 - Friday CS221.
Stacks.
Queues Queues Queues.
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queue, Deque, and Priority Queue Implementations
Stacks.
Linked Lists, Stacks and Queues Textbook Sections
Stacks, Queues, and Deques
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Lecture 21 Stacks and Queues Richard Gesick.
Queues.
Stacks, Queues, and Deques
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Lecture 7: Linked List Basics reading: 16.2
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.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Recall: stacks and queues
Stacks and Queues.
ADT Queue (Array Implementation)
More Data Structures (Part 1)
Stacks.
Fundaments of Game Design
Recall: stacks and queues
Stacks, Queues, and Deques
Lecture 7: Linked List Basics reading: 16.2
Lecture 16 Stacks and Queues CSE /26/2018.
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 first-in-last-out 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 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/List 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 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 Which data structure would you pick to implement a Stack? Why? In making your choice, consider the data structure, orientation, resizing overhead, Big O. Enqueue Dequeue double Linked list ArrayList/List 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