James Tam Data structures in Java Data Structures In Java In this section of notes you will learn about two common types of data structures: Queues Stacks.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CS Data Structures II Review COSC 2006 April 14, 2017
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.
James Tam CPSC 233: Introduction to Classes and Objects, Part I Attributes and methods Creating new classes References: Dynamic memory allocation and.
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.
James Tam Object-Oriented Principles in Java: Part II Issues associated with objects containing/composed of other objects: Composition, Reuse Issues associated.
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.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
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.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
James Tam Object-Oriented Principles in Java: Part I Encapsulation Information Hiding Implementation Hiding Creating Objects in Java Class attributes.
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.
James Tam Introduction To Data Structures This section introduces the concept of a data structure as well as providing the details of a specific example:
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Chapter 7 Stacks II CS Data Structures I COSC 2006
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.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 a simple.
Grade 12 Computer Studies HG
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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’
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Object-Oriented Programming Simple Stack Implementation.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
Introduction to Data Structures and Algorithms
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time 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.
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.
STACK Data Structure
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
David Stotts Computer Science Department UNC Chapel Hill.
Stacks as an Abstract Data Type CS1316: Representing Structure and Behavior.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Week 3 - Friday CS221.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Pointers and Linked Lists
Circular List removedNode General Case Single Node Case lastNode aNode
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
More Data Structures (Part 1)
Lecture 16 Stacks and Queues CSE /26/2018.
Chapter 5 Stack (part 1).
Lecture 16 Stacks and Queues CSE /26/2018.
A type is a collection of values
Presentation transcript:

James Tam Data structures in Java Data Structures In Java In this section of notes you will learn about two common types of data structures: Queues Stacks

James Tam Object-Oriented Principles in Java: Part II Data Structures: Description A composite type that has a set of basic operations that may be performed on instances of that type:  The type may be a part of the programming language e.g., arrays are a basic part of the Pascal language Some basic operations on arrays include: adding, deleting or modifying array elements.  The type may also be created by the programmer e.g. linked lists must be defined by the programmer in Pascal Some basic linked list operations include: creating a new list, adding, deleting and modifying nodes on that list.

James Tam Object-Oriented Principles in Java: Part II Data Structures To Be Covered Queues Stacks Characteristics: Both are lists The difference is in their behaviour

James Tam Object-Oriented Principles in Java: Part II Queues A list where additions occur only at one end of the list and deletions occur only at the other end. Front: Exit queueBack: Enter queue

James Tam Object-Oriented Principles in Java: Part II Implementing Queues Head Front: Exit queueBack: Enter queue

James Tam Object-Oriented Principles in Java: Part II Stacks A list where additions and deletions are made at only one end of the list. Top of stack

James Tam Object-Oriented Principles in Java: Part II Common Stack Operations Push Pop Peek

James Tam Object-Oriented Principles in Java: Part II Push Operation Adding an item to the top of the stack Top of stack

James Tam Object-Oriented Principles in Java: Part II Push Operation “7” has been added to the stack and this new item becomes the top of the stack Top of stack Top of stack 7 Before pushAfter push

James Tam Object-Oriented Principles in Java: Part II Pop Operation Removing an item from the top of the stack Top of stack

James Tam Object-Oriented Principles in Java: Part II Pop Operation “10” has been removed and “5” becomes the new top of the stack Top of stack Before pop Top of stack After pop

James Tam Object-Oriented Principles in Java: Part II Peek Operation Examine the item at the top of the stack without removing it Top of stack

James Tam Object-Oriented Principles in Java: Part II Java Implementation Of A Stack // It’s part of the legacy Java code but it still helps illustrate how the // implementation works. // Use of the Stack class requires the statement: import java.util.*; class Stack { public boolean empty (); public Object peek (); public Object pop (); public Object push (); public int search (Object o); }

James Tam Object-Oriented Principles in Java: Part II Example Using The Java Stack DriverIntegerWrapper Menu

James Tam Object-Oriented Principles in Java: Part II The Driver Class import tio.*; import java.util.*; class Driver { public static void main (String [] argv) { int i, noElements, tempNum; IntegerWrapper rapper; Stack s1; int menuSelection; boolean quitMenu = false; Menu m = new Menu (); System.out.print("Enter desired number of elements: "); noElements = Console.in.readInt(); Console.in.readChar();

James Tam Object-Oriented Principles in Java: Part II The Driver Class (2) s1 = new Stack (); System.out.println("Displaying elements in the order they were added..."); for (i = 0; i < noElements; i++) { rapper = new IntegerWrapper(); System.out.print("Value of element " + i + "..." + rapper.getNum()); if (i < (noElements-1)) System.out.println(); else System.out.println("\t<== Top of stack"); s1.push(rapper);

James Tam Object-Oriented Principles in Java: Part II The Driver Class (3) while (quitMenu != true) { m.displayMenu (); menuSelection = m.getSelection(); Console.in.readChar(); switch (menuSelection) { // Pop element case 1: if (s1.empty() == false) { rapper = (IntegerWrapper) s1.pop(); System.out.println(); System.out.println("Value of popped element: " + rapper.getNum()); System.out.println(); }

James Tam Object-Oriented Principles in Java: Part II The Driver Class (4) else { System.out.println(); System.out.println("Stack is empty: No elements to pop!"); System.out.println(); } break; // Push element case 2: System.out.println(); System.out.print("Enter value of element to push onto stack: "); tempNum = Console.in.readInt(); s1.push(new IntegerWrapper(tempNum)); break;

James Tam Object-Oriented Principles in Java: Part II Driver Class (5) case 3: rapper = (IntegerWrapper) s1.peek (); System.out.println(); System.out.println(“Element at the top of stack:" + rapper.getNum()); System.out.println(); break; case 4: System.out.println("Displaying elements in the order of the stack"); while (s1.empty() == false) { rapper = (IntegerWrapper) s1.pop(); System.out.println("\tValue of popped element: " + rapper.getNum()); } System.out.println(); break;

James Tam Object-Oriented Principles in Java: Part II Driver Class (6) case 5: quitMenu = true; break; } // End of switch } // End of while System.out.println("Exiting program."); } // End of main } // End of class Driver

James Tam Object-Oriented Principles in Java: Part II The IntegerWrapper Class class IntegerWrapper { private int num; public IntegerWrapper () {num = (int) (Math.random() * 100); } public IntegerWrapper (int no) { num = no; } public void setNum (int no) { num = no; } public int getNum () { return num; } }

James Tam Object-Oriented Principles in Java: Part II The Menu Class class Menu { public void displayMenu () { System.out.println("MENU OPTIONS"); System.out.println("1: Pop object off stack and display object"); System.out.println("2: Push new object onto stack"); System.out.println("3: Peek at object at the top of stack but don't remove"); System.out.println("4: Pop entire stack and view objects as they are popped"); System.out.println("5: Quit program"); } public int getSelection () { int menuSelection; System.out.print("Enter menu selection: "); menuSelection = Console.in.readInt(); return menuSelection; }