Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University

Similar presentations


Presentation on theme: "Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University"— Presentation transcript:

1 Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University http://softuni.bg

2 Table of Contents 1.Stack (LIFO – last in, first out)  add(), pop(), peek(), toArray(), contains() and size() 2.Exercises on Stack 3.Queue (FIFO – first in, first out)  add(), remove(), poll(), peek(), toArray(), contains() and size() 4.Exercises on Queue 5.Summary 2

3 3 sli.do #8663 Questions

4 4 Stack Overview 2 10 5

5 5  Stack holds a stack of elements  size() – the number of elements in the Stack  peek() – checks the value of the last added element  pop() – returns the last element and removes it from the stack  add() – adds an element to the Stack  toArray() – converts stack to array  contains() – determines whether an element is in the stack Stack – LIFO Data Structure

6 2105 6 add() – Adds an element on top of the Stack 0 size(): 1 2 3 Stack

7 2 10 5 7 pop() – Returns the last element from the stack and removes it size(): 312 Stack

8 1 5 8 size(): 5 peek() – Returns the last element from the stack, but does not remove it

9 9  contains() – determines whether an element is present in the stack. Returns true if it is and false if it isn’t.  toArray() – Converts the stack to an Array. Retains order of elements. contains(), toArray()

10 01 2 5 3 210515 10 Stack – Overview of all operations size(): 5 -7 45 132 add() pop() peek() Stack

11 Working with Stacks Live Demo! 01 2 5 3 210515 size(): 5 -7 45 132 push() pop() peek() Stack

12 12 Queue Overview 2 10 5

13 13  Queue holds a queue of elements:  add() – adds an element at the end of the queue  remove() – removes the first element and returns it (ex if empty)  poll() – same as remove but returns null if queue is empty  size() – returns the number of elements in the queue  peek() – checks the value of the first element  toArray() – converts the queue to an array  contains() – checks whether an element is in the queue Queue – FIFO Data Structure

14 -315121 3 2 10 4 14 add() – Adds an element to the front of the queue 5 size(): Queue

15 4 2 3 15 5 size(): Queue -315121 remove() / poll() – Returns the first element from the queue and removes it

16 15 2 16 size(): Queue 121 peek() – Returns the first element from the queue without removing it

17 17  contains() - determines whether an element is present in the queue. Returns true if it is and false if it isn’t.  toArray() - Converts the queue to an Array. Retains order of elements. contains(), toArray()

18 5 -315 4 121 012 18 size(): Queue Queue – Overview of all operations 5 3 15 peek() remove() add()

19 Queue Live Demo 5 -315 4 121 012 size(): Queue 5 3 15 peek() remove() add()

20 Summary  Stack – LIFO data structure  The last element that is put in the stack is the first to come out  Queue – FIFO data structure  The first element that is put in the queue is the first to come out 20

21 ? ? ? ? ? ? ? ? ? Stacks, Queues https://softuni.bg/courses/java-fundamentals

22 License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International  Attribution: this work may contain portions from  "Fundamentals of Computer Programming with Java" book by Svetlin Nakov & Co. under CC-BY-SA licenseFundamentals of Computer Programming with JavaCC-BY-SA  "C# Part I" course by Telerik Academy under CC-BY-NC-SA licenseC# Part ICC-BY-NC-SA  "C# Part II" course by Telerik Academy under CC-BY-NC-SA licenseC# Part IICC-BY-NC-SA 22

23 Free Trainings @ Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software University @ Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software University @ YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg


Download ppt "Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University"

Similar presentations


Ads by Google