Stacks and Queues. Stack ADT סוג של מערך מוגבל מהר מאוד ולוקחים מעט זכרון שימוש ב LIFO – LIFO (Last In, First Out) lists. –אפשר להוסיף רק בסוף הרשימה.

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 and Queues. Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Data Structures & Algorithms
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Stacks.
Queues.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Data Structures from Cormen, Leiserson, Rivest & Stein.
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:
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.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה משתנים יחד דוגמאות : עובד : שם, טלפון,
Objectives of these slides:
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
Stacks and Queues Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
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’
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Arrays and Collections Tonga Institute of Higher Education.
Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky
מחסנית ותור Stacks and Queues. מחסנית Stack מחסנית - Stack ADT סוג של מערך מוגבל מהיר מאוד ותופס מעט זיכרון שימוש ב LIFO – LIFO (Last In, First Out)
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.
מחסנית ותור Stacks and Queues. מחסנית Stack מחסנית - Stack ADT סוג של מערך מוגבל מהיר מאוד ותופס מעט זיכרון שימוש ב LIFO – LIFO (Last In, First Out)
Computer Engineering Rabie A. Ramadan Lecture 6.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
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.
18 Chapter Stacks and Queues
G64ADS Advanced Data Structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
QueueStack CS1020.
September 29 – Stacks and queues
Chapter 15 Lists Objectives
Chapter 12: Data Structures
Stacks and Queues.
Stack and Queue APURBO DATTA.
מחלקות classes.
Data Structures and Database Applications Queues in C#
Building Java Programs
Principles of Computing – UFCFA3-30-1
Web Services אוספים - Collections ליווי מקצועי : ארז קלר
Lecture 21 Stacks and Queues Richard Gesick.
מחסנית ותור Stacks and Queues.
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.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Abstract Data Type Abstract Data Type as a design tool
Lecture 2: Stacks and Queues
Lecture 16 Stacks and Queues CSE /26/2018.
Abstract Data Type (ADT)
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Programming
Presentation transcript:

Stacks and Queues

Stack ADT סוג של מערך מוגבל מהר מאוד ולוקחים מעט זכרון שימוש ב LIFO – LIFO (Last In, First Out) lists. –אפשר להוסיף רק בסוף הרשימה PUSH –אפשר להוריד רק מסוף הרשימה POP

פעולות הבסיסיות : השימוש של STACK הוא LIST עם מגבלות –אפשר להוסיף רק לראש הרשימה – PUSH סוג של INSERT – POP סוג של REMOVE – PEEK דרך לראות את הערך בראש הרשימה – הדרך היחידה לראות ערכים בלי להוריד אותם !

Push and Pop Primary operations: Push and Pop Push – Add an element to the top of the stack Pop – Remove the element at the top of the stack A top empty stack top push an elementpush another A B pop A

דוגמא של POP Module Module1 Sub Main() Dim test As New Stack() Dim i As Integer For i = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) For i = 1 To test.Count Dim num As Integer = test.Pop() Console.WriteLine(num) Next End Sub End Module

דוגמא של PEEK Module Module1 Sub Main() Dim test As New Stack() Dim i As Integer For i = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) For i = 1 To test.Count Dim num As Integer = test.Peek() Console.WriteLine(num) Next End Sub End Module

להמציא מחדש את הגלגל Public Class CStack Private index As Integer Private list As New ArrayList() Public Sub New() index = -1 End Sub Public Function Count() As Integer Return list.Count() End Function Public Sub Push(ByVal val As Object) list.Add(val) index += 1 End Sub Public Function Pop() As Object Dim obj As Object = list.Item(index) list.RemoveAt(index) index -= 1 Return obj End Function Public Function Peek() As Object Return list.Item(index) End Function End Class

שימוש ב MAIN ( אותו דבר ) Sub Main() Dim test As New CStack() Dim i As Integer For i = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) For i = 1 To test.Count Dim num As Integer = test.Pop() Console.WriteLine(num) Next End Sub

Queue ADT סוג אחר של מערך מוגבל מהר מאוד, ולוקחים מעט זכרון שימוש ב FIFO – FIFO (First In, First Out) lists. –אפשר להוסיף רק בסוף הרשימה Enqueue –אפשר להוריד רק התחלת הרשימה Dequeue

דוגמא Module Module1 Sub Main() Dim queue As New Queue Dim i As Integer For i = 1 To 5 queue.Enqueue(i) Next For i = 1 To queue.Count Console.WriteLine(queue.Dequeue()) Next End Sub

תרגיל : איך בונים QUEUE? Public Class CStack Private index As Integer Private list As New ArrayList() Public Sub New() index = -1 End Sub Public Function Count() As Integer Return list.Count() End Function Public Sub Enqueue(ByVal val As Object) ?? End Sub Public Function Dequeue() As Object ??? End Function Public Function Peek() As Object Return list.Item(0) End Function End Class

תרגיל : לחשב מחיר על בסיס LIFO וגם FIFO QUEUE ל FIFO STACK ל LIFO יש לבנות מבנה עם מחיר וכמות תכניס לתוך STACK ו QUEUE תחשב את המחיר לפי הפונקציות DEQUEUE ( ל QUEUE) ו POP ( ל STACK)

איך מתחילים ?? Structure Stock Dim Amount As Integer Dim Price As Decimal End Structure Module Module1 Sub Main() Dim List1 As New Queue() Dim List2 As New Stack() Dim temp As Stock temp.Amount = 10 temp.Price = 5.5 List1.Enqueue(temp) List2.Push(temp) temp.Amount = 50 temp.Price = 8.5 List1.Enqueue(temp) List2.Push(temp) temp = List1.Peek() Console.WriteLine("What's the cost? " & temp.Price) temp = List2.Peek() Console.WriteLine("What's the cost? " & temp.Price) End Sub End Module