CPS 100 6.1 Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
§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.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Data Structures and Algorithms (60-254)
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
 Balancing Symbols 3. Applications
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Topic 15 Implementing and Using Stacks
Stacks.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
TCSS 342, Winter 2005 Lecture Notes
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
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.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
CPS Data Structures revisited l Linked lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete, traverse,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
CompSci 100E 9.1 Stack: What problems does it solve?  Stacks are used to avoid recursion, a stack can replace the implicit/actual stack of functions called.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
CompSci 100e Program Design and Analysis II March 15, 2011 Prof. Rodger CompSci 100e, Spring20111.
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
April 27, 2017 COSC Data Structures I Review & Final Exam
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CPS Recurrences l Summing Numbers int sum(int n) { if (0 == n) return 0; else return n + sum(n-1); } l What is complexity? justification? l T(n)
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.
Programming Abstractions
Prefix notation in action
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Programming Abstractions
Stack: What problems does it solve?
Revised based on textbook author’s notes.
Chapter 15 Lists Objectives
Homework 4 questions???.
CSE 373: Data Structures and Algorithms
Algorithms and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
CSC 143 Queues [Chapter 7].
CSC 143 Stacks [Chapter 6].
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.
Stacks and Queues 1.
Topic 15 Implementing and Using Stacks
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
Data Structures & Programming
Presentation transcript:

CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked lists Collections supporting constant-time insertion l We’ve studied sets of strings as an abstraction with different concrete implementations (readset.cpp, code)  Sorted vectors, linked list, many linked lists, search trees  Different concrete implementations had different performance characteristics, but the client code did NOT change! l We studied the concrete implementations to understand efficiency  Abstractly, what are operations performed on set?  Need to view data abstractly, easier to be a client

CPS Stack: What problems does it solve? l Stacks are used to avoid recursion, a stack can replace the implicit/actual stack of functions called recursively l Stacks are used to evaluate arithmetic expressions, to implement compilers, to implement interpreters  The Java Virtual Machine (JVM) is a stack-based machine  Postscript is a stack-based language  Stacks are used to evaluate arithmetic expressions in many languages l Small set of operations: LIFO or last in is first out access  Operations: push, pop, top, create, clear, size  More in postscript, e.g., swap, dup, rotate, …

CPS Simple stack example tstack is a templated class, stores any type of value that can be assigned (like tvector )  Implemented simply using a vector, what does pop do? tstack s; s.push(2); s.push(3); s.push(1); cout << s.size() << endl; cout << s.top() << endl; s.pop(); cout << s.top() << endl; int val; s.pop(val); cout << val << endl;

CPS Templated class,.h ok,.cpp ugly l See tstack.h for example template class tstack { public: tstack( ); // construct empty stack const Type & top( ) const; // return top element bool isEmpty( ) const; // return true iff empty int size( ) const; // # elements void push( const Type & item ); // push item l But look at part of stack.cpp, class is templated (ugly?) template bool tstack ::isEmpty() const { return myElements.size() == 0; }

CPS Template class: implementation notes l A templated function or class isn’t code, per se, but template (or pattern) for generating the “real” code  The templated class or function is instantiated when an object is created, or a function called  The template code is instantiated for a particular type tvector a; // creates code int vector QuickSort(a, a.size()); // create function l Since not really code, header declaration needs access to.cpp implementation at compile time  Typically use #include “foo.cpp” in foo.h, then client code gets both.h and.cpp  Ok because not code, otherwise would cause problems at link time with duplicate function/class definitions

CPS Postfix, prefix, and infix notation l Postfix notation used in some HP calculators  No parentheses needed, precedence rules still respected * *  Read expression For number/operand: push For operator: pop, pop, operate, push l See postfix. cpp for example code, key ideas:  Read character by character, check state of expression  Note: putback character on stream, only last one read l What about prefix and infix notations, advantages?

CPS Prefix notation in action l Scheme/LISP and other functional languages tend to use a prefix notation (define (square x) (* x x)) (define (expt b n) (if (= n 0) 1 (* b (expt b (- n 1)))))

CPS Postfix notation in action l Practical example of use of stack abstraction l Put operator after operands in expression  Use stack to evaluate operand: push onto stack operator: pop operands push result l PostScript is a stack language mostly used for printing  drawing an X with two equivalent sets of code %! moveto rlineto moveto 100 –100 rlineto stroke showpage %! 100 – moveto rlineto stroke showpage

CPS Queue: another linear ADT l FIFO: first in, first out, used in many applications  Scheduling jobs/processes on a computer  Tenting policy?  Computer simulations l Common operations (as used in tqueue.h / tqueue.cpp )  Add to back, remove from front Called enqueue, dequeue, like s.push() and s.pop() Analog of top() is front() l Also used in level-order tree traversal, similar to pre-order without recursion but using stack  See code in treelevel.cpp

CPS Stack and Queue implementations l Different implementations of queue (and stack) aren’t really interesting from an algorithmic standpoint  Complexity is the same, performance may change (why?)  Use vector or linked list, any sequential structure l Linked list is easy for stack, where to add/remove nodes? l Linked list is easy for queue, where to add/remove nodes?  Use circular linked list, why? l Vector for queue is tricky, need ring buffer implementation, add but wrap-around if possible before growing  Tricky to get right (see tqueue.h, tqueue.cpp)

CPS Using linear data structures l We’ve studied vectors, stacks, queues, which to use?  It depends on the application  Vector is multipurpose, why not always use it? Make it clear to programmer what’s being done Other reasons? l Other linear ADTs exist  List: add-to-front, add-to-back, insert anywhere, iterate Alternative: create, head, tail (see Clist in tapestry) Linked-list nodes are concrete implementation  Deque: add-to-front, add-to-back, random access Why is this “better” than a vector? How to implement?

CPS James Gosling l “Invented” Java  First developer, originator, l Impetus for GPL, free software?  Stallman writes emacs, gosling writes C version, shares it, sells it, oops trouble with shared Stallman : “ Then he stabbed everyone in the back by putting copyrights on it, making people promise not to redistribute it and then selling it to a software-house. My later dealings with him personally showed that he was every bit as cowardly and despicable as you would expect from that history. “