Data Structures In Java



Similar documents
Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

J a v a Quiz (Unit 3, Test 0 Practice)

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Linked Lists Linked Lists, Queues, and Stacks

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

COMPUTER SCIENCE. Paper 1 (THEORY)

Software Engineering Techniques

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Changing the Display Frequency During Scanning Within an ImageControls 3 Application

DATA STRUCTURE - STACK

Building Java Programs

Chapter 2 Introduction to Java programming

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Stacks. Linear data structures

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Cpt S 223. School of EECS, WSU

Chapter 3: Restricted Structures Page 1

CmpSci 187: Programming with Data Structures Spring 2015

Building Java Programs

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Introduction to Java

Algorithms and Data Structures

Statements and Control Flow

LINKED DATA STRUCTURES

Building Java Programs

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

The following program is aiming to extract from a simple text file an analysis of the content such as:

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Abstract Data Types. Chapter 2

Stacks. Data Structures and Data Types. Collections

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

CompSci-61B, Data Structures Final Exam

STACKS,QUEUES, AND LINKED LISTS

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Sample CSE8A midterm Multiple Choice (circle one)

Part I. Multiple Choice Questions (2 points each):

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Unit 6. Loop statements

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Introduction to Programming

Contents. 9-1 Copyright (c) N. Afshartous

CS170 Lab 11 Abstract Data Types & Objects

Software Development with UML and Java 2 SDJ I2, Spring 2010

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

DATA STRUCTURES USING C

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

DATA STRUCTURE - QUEUE

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

CSE 143, Winter 2013 Programming Assignment #2: HTML Validator Due Thursday, July 10, 2014, 11:30 PM

Introduction to Stacks

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

1 Hour, Closed Notes, Browser open to Java API docs is OK

CSC 551: Web Programming. Fall 2001

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

HW3: Programming with stacks

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Building a Multi-Threaded Web Server

Analysis of a Search Algorithm

Common Data Structures

Lecture J - Exceptions

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Abstraction and Abstract Data Types

Statements and Control Flow

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Topic 11 Scanner object, conditional execution

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

Practical Session 4 Java Collections

AP Computer Science Java Mr. Clausen Program 9A, 9B

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

St S a t ck a ck nd Qu Q eue 1

Object-Oriented Programming

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Inside the Java Virtual Machine

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Some Scanner Class Methods

Transcription:

Data Structures In Java In this section of notes you will learn about two common types of data structures: Queues Stacks Data structures in Java 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.

Data Structures To Be Covered Queues Stacks Characteristics: Both are lists The difference is in their behaviour Queues A list where additions occur only at one end of the list and deletions occur only at the other end. Front: Exit queue Back: Enter queue

Implementing Queues Head Front: Exit queue Back: Enter queue Stacks A list where additions and deletions are made at only one end of the list. Top of stack

Common Stack Operations Push Pop Peek Push Operation Adding an item to the top of the stack 10 Top of stack

Push Operation 7 has been added to the stack and this new item becomes the top of the stack. Before push After push 10 Top of stack 7 10 Top of stack Pop Operation Removing an item from the top of the stack 10 Top of stack

Pop Operation 10 has been removed and becomes the new top of the stack. Before pop After pop 10 Top of stack Top of stack Peek Operation Examine the item at the top of the stack without removing it 10 Top of stack

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); Example Using The Java Stack Driver IntegerWrapper Menu

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(); The Driver Class () 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)) else System.out.println("\t<== Top of stack"); s1.push(rapper);

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("Value of popped element: " + rapper.getnum()); The Driver Class () else break; System.out.println("Stack is empty: No elements to pop!"); // Push element case : System.out.print("Enter value of element to push onto stack: "); tempnum = Console.in.readInt(); s1.push(new IntegerWrapper(tempNum)); break;

Driver Class () case 3: rapper = (IntegerWrapper) s1.peek (); System.out.println( Element at the top of stack:" + rapper.getnum()); break; case : 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()); break; Driver Class (6) case : quitmenu = true; break; // End of switch // End of while System.out.println("Exiting program."); // End of main // End of class Driver

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; 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(": Push new object onto stack"); System.out.println("3: Peek at object at the top of stack but don't remove"); System.out.println(": Pop entire stack and view objects as they are popped"); System.out.println(": Quit program"); public int getselection () int menuselection; System.out.print("Enter menu selection: "); menuselection = Console.in.readInt(); return menuselection;