Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 16 Stacks and Queues CSE 1322 4/26/2018."— Presentation transcript:

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

2 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

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

4 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

5 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

6 Stacks Operations 4/26/2018

7 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

8 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

9 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

10 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

11 Stack vs Queue 4/26/2018

12 Queues Operations 4/26/2018

13 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

14 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


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

Similar presentations


Ads by Google