Data Structures Data structures permit the storage of related data for use in your program. –Arrays.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
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.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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:
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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 Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Chapter 12: Data Structures
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Stacks.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
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.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Microsoft® Small Basic Stacks and Arrays Estimated time to complete this lesson: 1 hour.
Topic 3 The Stack ADT.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Grade 12 Computer Studies HG
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Stacks and Queues Introduction to Computing Science and Programming I.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
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,
Week 9 Data structures / collections. Vladimir Misic Week 9 Monday, 4:20:52 PM2 Data structures (informally:) By size: –Static (e.g. arrays)
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Understanding Data Types and Collections Lesson 2.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue.
Arrays and Collections Tonga Institute of Higher Education.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Click to edit Master text styles Stacks Data Structure.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
Introduction to Data Structure
Stacks, Queues, and Deques
Stacks.
Presentation transcript:

Data Structures Data structures permit the storage of related data for use in your program. –Arrays

Data Structures Dynamic data structures hold an unknown number of elements. –Arrays hold a specified number of elements. Generic type data structures hold data elements of different data types. –Arrays hold data of the same data type.

Data Structures Types of dynamic generic data structures are: –Stacks, queues, lists, trees

Stacks What is a stack?

Stacks A stack is similar in concept to a pile of plates, books, blocks, boxes, etc. –The first item put on the stack is on the bottom of the stack. –All items added to the stack are placed on top of the previous item. –The last item put on the stack is on the top of the stack.

Stacks Stacks are called Last-in First-out (LIFO) data structures. –The last plate put on the top of the stack is the first plate removed from the stack. –The first plate put on the top of the stack is the last plate removed from the stack.

Stacks Characteristics of stacks: –Data can only be place on the top of the stack. –Data can only be removed from the top of the stack. –Data can only be removed from the bottom of the stack if there is only one item on the stack. –Data can not be removed from the middle of the stack without first removing all items from the top.

Stack Example Create a stack with the following information: –Place the following numbers in a stack in this order: 5, 8, 10, 2, 4, 12 –Remove number 10 from the stack. –Put 3, 7, 1 on the stack. –Now remove 5 from the bottom of the stack and put it on the top of the stack.

Stack Behaviors The behavior of putting an item on the stack is called push. –Push 4 onto the stack. The behavior of removing and item from the stack is called pop. –Pop 4 from the stack.

Example What stack is exists after executing the following commands? –push(3) –push(6) –push(8) –push(1) –pop() –push(14)

Stack Implementation The ability to use a stack is not built into Java like arrays. –You can implement a stack using a list, a queue, or an array.

Stack Implementation A stack class can be written using arrays that will simulate a stack in your programs. –A restriction of using arrays to implement a stack is that the total size of the stack is limited. Why? Can you define a method that will resize the array to hold a stack of any size?

Stack Implementation Create a Stack class to hold strings. –Assume that the first element of the array is the bottom element on the stack. –How do we know the number of items on the stack? –How do we know if there is anything in the stack?

Stack Implementation Now that we have implemented the Stack class, how can we use it?

Stack Implementation How do we store data of any type into our stack?

Object Class Every class in Java is a child of the Object class. –The Object class is the most generic class in Java. –Therefore, we should be able to create a stack of type Object to store any data into the stack. This is not necessarily true. Primitive data types are not descendents of type Object.

Type Wrapper Classes Each primitive data type has a Type Wrapper class. –The type wrapper class for: double is Double, int is Integer, float is Float, boolean is Boolean.

Type Wrapper Classes Type wrapper classes allow you to manipulate primitive data types as objects of class Object. –The type wrapper class contains functions that manipulate the data as a class.

Type Wrapper Classes The primitive data type is not automatically a type wrapper version of the data type. –To define a version of the primitive data type as a type wrapper you need to create a new instance of the type wrapper. Integer newInteger = new Integer(98); Double newDouble = new Double(986.3);

Stack Implementation How do we redefine our Stack class to hold data of any data type?