ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
Queues and Linked Lists
CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Data Structures and Algorithms (60-254)
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks, Queues, and Deques
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?
Stacks.
Queues.
Stacks, Queues & Deques CSC212.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Implementing and Using Stacks
Objectives of these slides:
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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’
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Welcome to CSCE 221 – Data Structures and Algorithms
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Algorithms and Data Structures Lecture VI
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
COSC160: Data Structures: Lists and Queues
Queues Rem Collier Room A1.02
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues 1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Stacks and Queues.
Stacks and Linked Lists
Presentation transcript:

ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Elementary Data Structures zUsed as programming tools (stacks & queues) - software (i.e. compilers) - used in the operations of a certain task zImplemented in CPU instructions - hardware

Stacks zDefinition Õ container of objects Õ uses LIFO principle for object operations Õ operations are push and pop zSample uses : Õ Internet browsers Õ Undo / Redo feature

Other Applications zParsing - check for matching parenthesis or brackets - aid for algorithms applied to complex data structures (traverse nodes of a tree and searching vertices of a graph)

Basic Stack Operations zpush(o) zpop() zInserts object o onto top of stack Input: object Output: none zRemoves the top object of stack and returns it to calling method Input: none Output: object

Related Stack Operations zisEmpty() zisFull() zReturns a boolean indicating if stack is empty. Input: none Output: boolean zReturns a boolean indicating if stack is full Input: none Output: boolean

Other Stack Operations zsize() : ztop() zReturns the number of objects in stack. Input: none Output: integer zReturns the top object of the stack. Input: none Output: object

An Array-Based Stack zCreate an array A of size N zStore elements of stack S in array A zLet t (integer) - index of the top element of stack S S t... N-1

Array Implementation zAlgorithm size() return t + 1 zAlgorithm isempty() return (t<0) zAlgorithm push(o) if size()=N then throw a STACKEMPTYEXCEPTION t=t+1 S[t]=o

Array Implementation zAlgorithm pop() if isempty() then throw a STACKEMPTYEXCEPTION e=S[t] S[t]=null t=t-1 return e zAlgorithm top() if isempty() then throw a STACKEMPTYEXCEPTION return S[t]

Class Problem zUsing the stack functions, make an algorithm that will determine that given a string x : a) has matching operands (i.e. (),{},[]) b) report an error if an operand is missing zExample : x = “(12xxs3e)”, correct x = “(sdfsdfs}”, error

Class Problem - Summary 1. Make an empty stack 2. Read characters until end of file 3. If the character is an open anything, push it in the stack 4. If it’s a close anything,then if stack is empty, report an error, otherwise, pop the stack 5. If popped symbol is not the corresponding opening symbol, then report an error 6. At end of file, if stack is not empty report an error

Stacks- other applications zRecursion - i.e. computing for N factorial zOperand parsing - evaluating an expression - i.e. ((4*5)+(6+2)/5)) - postfix notation A*B+C = AB*C+ zfunction calls - variables and routine position are saved

Stacks - Query zHow can the undo/redo function be implemented using stacks ?

QUEUES zA queue is a container of objects that are inserted and removed according to the FIFO principle. zOperations: Enqueue - insert an item at the rear of queue z Dequeue - remove an item from the front of the queue zSample uses : movie line printing queue

The Queue Abstract Data Type zenqueue(o) zdequeue() zInsert object o at the rear of the queue Input: object Output: none zRemove the object from the front of the queue and return it. Input: none Output: object

The Queue Abstract Data Type zsize() :Returns the number of objects in the queue. Input: noneOutput: object zisEmpty(): Return a boolean indicating if the queue is empty. Input: noneOutput:boolean zfront(): Return the front object of the queue. z Input: noneOutput: object

Array Implementation zCreate a queue using an array in a circular fashion zQueue consists of an N-element array Q and two integer variables: f : index of the front element r: index of the element after the rear one zConfigurations : “normal” “wrapped around”

Queue Implementation f r Array Q f is an index to a cell Q storing the first element, r is an index to the next available cell in Q

Queues- Example zPrint Jobs - All requests from workstations are enqueued to the print queue on a first come first served basis - The current (first) printjob is dequeued and sent to the printer for processing

Queue - Applications zI/O Request Handling File server - Workstation (print, data access,etc.) zTelephone Call Handling zHistory functions in applications

Limitations - Using Arrays zMaximum size needs to be predetermined zPotential wastage of allocated memory zNeed to handle overflow

Linked List ADT Head next next next null Each node stores a reference to an element and a reference, called next to another node

Singly Linked List Queue Implementation zNodes connected in a chain by links headtail zhead of the list - front of the queue tail of the list - rear of the queue

Removing at the Head headtail zadvance head reference headtail

Inserting at the Tail zcreate a new node headtail zchain it and move the tail reference headtail

Other Advantages zInsertions and deletions are easier compared to an array implementation when dealing with “lists”

Linked Lists - Query zIf a stack will be implemented using a linked list, is it better to assign the head or the tail as the top of the stack ? Why ?

Double-Ended Queues zData structure that allows insertion and deletion at the front and the rear end of the queue (pronounced as “deck”)

Doubly linked lists zA node in a doubly linked list is like a node in a singly linked list except that, in addition to the next link, it also has a prev link to the previous node in the list. zAdvantage over singly linked lists : deletions at the tail of the list can be done in constant time

Doubly-Linked Lists

Linked Lists - Query zWhat is the running time to search for an entry in a linked list ? Deleting from the tail ? Inserting from the head ?