Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Data Structures Data structures permit the storage of related data for use in your program. –Arrays."— Presentation transcript:

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

2 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.

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

4 Stacks What is a stack?

5 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.

6 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.

7 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.

8 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.

9 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.

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

11 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.

12 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?

13 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?

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

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

16 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.

17 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.

18 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.

19 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);

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


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

Similar presentations


Ads by Google