Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Stacks, Queues, and Linked Lists
Data Structures ADT List
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
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.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS Data Structures II Review COSC 2006 April 14, 2017
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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’
Data structures Abstract data types Java classes for Data structures and ADTs.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
UNIT-II Topics to be covered Singly linked list Circular linked list
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.
Computing with C# and the .NET Framework
Stacks and Queues.
Stacks.
Stack and Queue APURBO DATTA.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Abstract Data Types (ADT)
Stacks and Queues 1.
Abstract Data Types (ADT)
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
LINEAR DATA STRUCTURES
Presentation transcript:

Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example: Array of integers Heterogeneous Collection –Contains elements of differing types –Example: The Java Stack Abstract Data Type –A data structure with a well-defined interface –Examples: stack, queue, list, tree, graph

Stacks/Queues/Dynamic lists Stack Operations –push, pop, isEmpty(), isFull(), look(), reset() –look() is sometimes called peek() Queue Operations –add(), remove(), isFull(), isEmpty(), reset() Tree Operations –insert(), remove(), find(), traverse(), bfs(), dfs() Key: a well defined interface to abstract the data structure Key: We can change the data structure without effecting users

Implementation Possibilities Use generic built-in methods –Advantage: standard data type supported by Java –Disadvantage: lose flexibility Using Arrays –Advantages: fast, native Java data structure –Disadvantage: The size must be known in advance Dynamic storage –Advantage: The structure can grow and shrink –Disadvantage: Executes more slowly

A Stack Example public static void main( String args[] ) { Stack stack = new Stack(); String string = "Hello this is a test string"; System.out.println("String: " + string); for (int k = 0; k < string.length(); k++) stack.push(new Character( string.charAt(k))); Object obj = null; String reversed = ""; while (!stack.isEmpty()) { obj = stack.pop(); reversed = reversed + o.toString(); } System.out.println("Reversed: " + reversed); } Reverse a String

Java Generic Classes Vector Vector vec = new Vector (); vec.addElement("alpha"); rec.addElement("beta"); System.out.println(vec.elementAt(0)); Stack Stack stk = new Stack (); stk.push("alpha"); stk.push("beta"); System.out.println(stk.pop()); LinkedList List link = new LinkedList (); link.add(new PhoneRecord("Roger", "(541) ")); for (PhoneRecord pr : theList) System.out.println(pr); Java has many other generic interfaces and classes –Examples: Set, Map, Hashtable

Stack of doubles with Arrays public class Stack { int top; double[] data; public Stack(int size) { data = new double[size]; top = -1; } public void push(double x) throws Exception { if (!isFull() { data[++top] = value; } else throw new Exception(); } public double pop() { if (!isEmpty()) return data[top--]; else throw new StackException(); } public boolean isEmpty() {return (top==-1); } public boolean isFull() {return (top==data.length-1);} public double peek() throws Exception() { if (!isEmpty()) {return data[top];} else throw new StackException(); } }

Algorithm: Evaluating InfixExpressions Instantiate two stacks: operator, operand Remove white space from the string expression Break string expression into tokens (delimeters = "+-/*()") WHILE more tokens exist, get nextToken SWITCH nextToken Left paren: push '(' on operator stack ): WHILE top of operator stack !='(' THEN CALL Eval() Pop the matching ( + or –: WHILE top of operator stack is '+', '-', '*', or '/' THEN CALL eval() push nextToken to operator stack * or /: WHILE top of operator() stack is '*' or '/' THEN CALL eval() push nextToken to operator stack DEFAULT: Convert to double & push nextToken to operand stack WHILE operator stack is not empty THEN CALL eval() Result is on top of operand stack Note: eval() method pop two operands and one operator, calculate, and push result to operand stack

Example Expression Algorithm push 1 to operand push '+' to operator push 2 to operand push '*' to operator push 16 to operand Call Eval: pop 16, pop 2, pop '*', 2*16->32, push 32 to operand push '/' to operator push 4 to operand Call Eval: pop 4, pop 32, pop '/,' 32/4->8, push 8 to operand Call Eval: pop 8, pop 1, pop '+,' 1+8->9, push 9 to operand Result is at top of operand stack (9) Evaluate: "1+2*16/4"

Example Expression Algorithm push 3 to operand, push '+' to operator, push '(' to operator push 5 to operand, push '+' to operator, push 6 to operand push '/' to operator, push 2 to operand Call Eval: pop 2, pop 6, pop '/', 6/2->3, push 3 to operand Call Eval: pop 3, pop 5, pop '+', 5+3->8, push 8 to operand pop '(' push '*' to operator, push 3 to operand Call Eval: pop 3 pop 8, 8*3->24, push 24 to operand push '/' to operator, push 2 to operand Call Eval: pop 2, pop 24, 24/2->12, push 12 to operand push '-' to operator, push 4 to operand Call Eval: pop 4, pop 12, 12-4->8, push 8 to operand Call Eval: pop 8, pop 3, 3+8->11, push 11to operand Result is at top of operand stack (11) Evaluate: "3+(5+6/2)*3/2-4"

Queue Concepts using Arrays A circular array –We store data in consecutive locations –When we reach the bottom, we then wrap around to the top Example –Goal: Store x at next location in an array, data –Assume: we stored the last element at offset j –Java Code: j = (j+1)%data.length; data[j] = x; data j Question: if x=3.5, where is it stored? What if j=2?

Queues with an array implementation Instance variables –int head, int tail, double data[] –We add to the tail and remove from the head –We update the tail on insertion, and update the head on removal. How do we check if the queue is full? –Answer: Check if the next tail location equals the head How do we remove an item –Answer: Extract from the head and increment the pointer. If the head and tail pointers become equal, we set both to minus one.

Using dynamic storage Class Node { Object info; Node next; public Node(Object data) { info=data; next=null; } } node1 = new Node("alpha"); node1.next = new Node("beta"); node1.next.next = new Node("gamma"); "alpha""beta""gamma" X

Doubly linked Doubly linked node class Node { String info; Node next; Node previous; } Tree nodes class Node {int info; Node[] next; }

Doubly Linked Class Node { Object info; Node next; public Node(Object data) { info=data; next = prev =null; } } Node node1 = new Node("alpha"); Node node2 = new Node("gamma"); Node node3 = "new Node("gamma"); node1.next = node2; node2.next = node3; node3.prev = node2; node2.prev = node1; "alpha""beta" "gamma" X X X