Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))

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
Queues and Linked Lists
Data Structure HKOI training /4/2010 So Pak Yeung.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Stacks, Queues & Deques CSC212.
Data Structures from Cormen, Leiserson, Rivest & Stein.
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.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
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’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Foundation of Computing Systems Lecture 3 Stacks and Queues.
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.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
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.
STACK Data Structure
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks and Queues. DCS – SWC 2 Stacks A stack is an abstract data structure, with some special properties: –Insertion and deletion is only allowed at.
CSE 1342 Programming Concepts
Queues Chapter 4.
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.
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
Chapter 15 Lists Objectives
Data Structures and Algorithms
Stacks and Queues CMSC 202.
Queues Queues Queues.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Priority Queues - Harvey B. Mackay
Queues Chapter 4.
i206: Lecture 11: Stacks, Queues
Pointers and Linked Lists
i206: Lecture 10: Lists, Stacks, Queues
Data Structures and Database Applications Stacks in C#
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Java Collections Framework
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
ITEC 2620M Introduction to Data Structures
Stacks and Queues.
Lesson 6. Types Equality and Identity. Collections.
Dynamic Sets (III, Introduction)
CSE 214 – Computer Science II Stacks
Data Structures and Algorithms
Cs212: Data Structures Computer Science Department Lecture 7: 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.
Priority Queues - Harvey B. Mackay
CS6045: Advanced Algorithms
Abstract Data Types and Stacks
Mutators for compound data Stack Queue
Python: Stacks and Queues (as an Array)
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
Stack Implementations
Presentation transcript:

Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3

Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 new () 4

Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 new X () 4

Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist X () 1 2 3 new () 4

Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 new () 4

Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4))) (set! (tail new) (tail mylist)) (set! (tail mylist) new)) mylist () 1 2 3 4

Stacks and Queues stacks: last-in-first-out (LIFO) queues: first-in-first-out (FIFO)

Stacks (make-stack) makes a new empty stack (push thing stack) returns a new stack with thing on top of stack (pop stack) returns a stack like stack, but without its top element (top stack) returns the top element of stack (empty? stack) Is there anything on the stack ?

Contract for Stacks (top (push thing stack)) = thing (pop (push thing stack)) = stack (empty? (make-stack)) = #t (empty? (push thing stack)) = #f

Implement Stacks with Lists push = pair pop = tail top = head empty? = null? All operations are O(1) time

Queues (make-queue) makes a new empty queue (insert thing queue) returns a new queue with thing as last element (delete queue) returns a queue like queue, but without its first element (head queue) returns the first element of queue (empty? queue) tests emptiness

Implementation of Queues as Lists head = head delete = tail empty? = null? Insert: (method ((thing <object>) (q <list>)) (append q (list thing))) Insert is O(n) time

q () 1 2 3

(define <queue> <pair>) (define (empty-queue? <function>) (method ((q <queue>)) (null? (head q)))) (define (make-queue <function>) (method () '(()))) (define (queue-head <function>) (method ((q <queue>)) (if (empty-queue? q) (error ”Cannot take head of empty queue") (head (head q)))))

(define (insert <function>) (method ((x <object>) (q <queue>)) (bind (((new <list>) (list x))) (if (empty-queue? q) (set! (head q) new) (set! (tail (tail q)) new)) (set! (tail q) new)) q)) (define (delete <function>) (method ((q <queue>)) (error "Cannot delete from empty queue") (set! (head q) (tail (head q))))

Priority Queues (define-class <entry> (<object>) (key <integer>) (data <object>)) (define <priority-queue> <list>) ;;make a new queue entry with key n and data d (define (make-entry <function>) (method ((n <integer>) (d <object>)) (make <entry> key: n data: d)))

To insert: (define (insert-1 <function>) (method ((e <entry>) (q <priority-queue>)) (cond ((null? q) (list e)) ((< (key e) (key (head q))) (pair e q)) (else: (pair (head q) (insert-1 e (tail q))))))) To insert: (set! *q* (insert-1 e *q*)) Insert: O(n) Delete: O(1)

(define insert-2 (method ((e <entry>) (q <priority-queue>)) ;check if new element should go at head of list (if (or (null? q) (< (key e) (key (head q)))) (pair e q) ;no: find element that it goes immediately after (bind-methods ((find-place ((q <priority-queue>)) (if (or (null? (tail q)) (< (key e) (key (second q)))) q (find-place (tail q))))) (bind ((pq (find-place q)) (le (pair e (tail pq)))) ;link new element in (set! (tail pq) le) ;return original list q)))))

Alternatively: Next time: Insert: add at head O(1) Delete: search for min O(n) Next time: Insert: O(log n) Delete: O(log n)