190 likes | 296 Views
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.
E N D
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 placed 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 exists after executing the following commands? • push(3) • push(6) • push(8) • push(1) • pop() • 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 • 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?