CST230: Applied Data Structures

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
§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.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CHAPTER 7 Queues.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
 Balancing Symbols 3. Applications
1 Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
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’
Stacks And Queues Chapter 18.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright ©
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.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Linear Data Structures
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
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 4 The easy stuff.
September 29 – Stacks and queues
Program based on queue & their operations for an application
Queues Rem Collier Room A1.02
Chapter 15 Lists Objectives
Stacks and Queues.
Queues Queues Queues.
Data Structures Interview / VIVA Questions and Answers
Cinda Heeren / Geoffrey Tien
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
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.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues 1.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Fundaments of Game Design
Stacks, Queues, and Deques
LINEAR DATA STRUCTURES
DATA STRUCTURES IN PYTHON
Presentation transcript:

CST230: Applied Data Structures Chia Yuan Chuang "Cha" Adapted from course notes by Clifford A. Shaffer *

Stacks LIFO: Last In, First Out. Restricted form of list: Insert and remove only at front of list. Notation: Insert: PUSH Remove: POP The accessible element is called TOP. * *

Stack ADT public interface Stack<E> { /** Reinitialize the stack. */ public void clear(); /** Push an element onto the top of the stack. @param it Element being pushed onto the stack.*/ public void push(E it); /** Remove and return top element. @return The element at the top of the stack.*/ public E pop(); /** @return A copy of the top element. */ public E topValue(); /** @return Number of elements in the stack. */ public int length(); }; * *

Array-Based Stack Issues: Which end is the top? // Array-based stack implementation private int maxSize; // Max size of stack private int top; // Index for top private E [] listArray; Issues: Which end is the top? Where does “top” point to? What are the costs of the operations? * *

Linked Stack What are the costs of the operations? class LStack<E> implements Stack<E> { private Link<E> top; private int size; What are the costs of the operations? How do space requirements compare to the array-based stack implementation? * *

Expression Evaluation evaluate well-parenthesized expression main idea: Push open parenthesis ( to stack push numbers to stack push operators to different stack when close parenthesis encountered, pop operation, two numbers, and open paren. perform calculation, and push result stack should contain a single result after processing entire expression. Example: (((6+9) / 3) * (6 – 4))

Queues FIFO: First in, First Out Restricted form of list: Insert at one end, remove from the other. Notation: Insert: Enqueue Delete: Dequeue First element: Front Last element: Rear * *

Queue Implementation (1) If we stick to the requirement that the queue elements be at the beginning n elements of the array, then either enqueue or dequeue must be θ(n). Better is to let the elements “drift” within the array. Unfortunately, as items are added and removed, the queue “drifts” toward the end. Eventually, there will be no space to the right of the queue, even though there is space in the array. * *

Queue Implementation (2) By using the mod function, we can easily achieve the effect of a “circular” queue. This leaves one more issue. Where do the front and rear pointers go (point to the item? Or to the space before/after the item)? And, how do we distinguish a full from an empty queue? Given a fixed position for the front element (and its pointer), there are n-1 possible states for the queue (0 through n elements in the queue for an array of size n), but only n possible positions for rear. To solve this dilemma, we must either leave an empty slot in the queue, or use and external variable to determine if the queue is empty or not. * *

Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison Equality: sequential search Relative order: sorting Key: When describing what we are looking for, we don’t want to be required to describe the entire record, only one field of the record. If we already knew everything about the record, we probably wouldn’t need to look for it. * *

Records and Keys Problem: How do we extract the key from a record? Records can have multiple keys. Fundamentally, the key is not a property of the record, but of the context. Solution: We will explicitly store the key with the record. Can’t just compare records to each other. Need to extract the key. How? Could have an explicit key field? No. Explicit key method? No. Records can have multiple keys. Could the key method be type sensitive? No, multiple fields in the record can have the same type. There is a fundamental problem: They key is not a property of the record, it is a property of the context in which the record is being used. Key-Value pair. This is a traditional solution to this problem. Note that the key is overhead. But this becomes less of a problem as the relative size of the record gets large. * *

Dictionary ADT public interface Dictionary<Key, E> { public void clear(); public void insert(Key k, E e); public E remove(Key k); // Null if none public E removeAny(); // Null if none public E find(Key k); // Null if none public int size(); }; * *

Payroll Class // Simple payroll entry: ID, name, address class Payroll { private Integer ID; private String name; private String address; Payroll(int inID, String inname, String inaddr) { ID = inID; name = inname; address = inaddr; } public Integer getID() { return ID; } public String getname() { return name; } public String getaddr() { return address; } * *

Using Dictionary // IDdict organizes Payroll records by ID Dictionary<Integer, Payroll> IDdict = new UALdictionary<Integer, Payroll>(); // namedict organizes Payroll records by name Dictionary<String, Payroll> namedict = new UALdictionary<String, Payroll>(); Payroll foo1 = new Payroll(5, "Joe", "Anytown"); Payroll foo2 = new Payroll(10, "John", "Mytown"); IDdict.insert(foo1.getID(), foo1); IDdict.insert(foo2.getID(), foo2); namedict.insert(foo1.getname(), foo1); namedict.insert(foo2.getname(), foo2); Payroll findfoo1 = IDdict.find(5); Payroll findfoo2 = namedict.find("John"); UALdictionary stands for Unsorted Array-based List (implementation for a) dictionary. * *

Unsorted List Dictionary class UALdictionary<Key, E> implements Dictionary<Key, E> { private static final int defaultSize = 10; private AList<KVpair<Key, E>> list; // Constructors UALdictionary() { this(defaultSize); } UALdictionary(int sz) { list = new AList<KVpair<Key, E>>(sz); } public void clear() { list.clear(); } /** Insert an element: append to list */ public void insert(Key k, E e) { KVpair<Key,E> temp = new KVpair<Key,E>(k, e); list.append(temp); } Example implementation using the dictionary ADT. * *

Sorted vs. Unsorted List Dictionaries If list were sorted Could use binary search to speed search Would need to insert in order, slowing insert Which is better? If lots of searches, sorted list is good If inserts are as likely as searches, then sorting is no benefit. * *