• No results found

Data Structures In Java

N/A
N/A
Protected

Academic year: 2021

Share "Data Structures In Java"

Copied!
11
0
0

Loading.... (view fulltext now)

Full text

(1)

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

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,

(2)

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

Queues

A list where additions occur only at one end of the list and

deletions occur only at the other end.

(3)

James Tam

Object-Oriented Principles in Java: Part II

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.

(4)

James Tam

Object-Oriented Principles in Java: Part II

Common Stack Operations

Push

Pop

Peek

Push Operation

Adding an item to the top of the stack

5 4 2 5

(5)

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.

5 4 2 5 10 Top of stack 5 4 2 5 10 Top of stack 7

Before push

After push

Pop Operation

Removing an item from the top of the stack

5 4 2 5

(6)

James Tam

Object-Oriented Principles in Java: Part II

Pop Operation

“10” has been removed and “5” becomes the new top of the

stack.

5 4 2 5 10 Top of stack

Before pop

5 4 2 5 Top of stack

After pop

Peek Operation

Examine the item at the top of the stack without removing it

5 4 2 5

(7)

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

Example Using The Java Stack

Driver

IntegerWrapper

(8)

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

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

(9)

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

}

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;

(10)

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;

Driver Class (6)

case 5: quitMenu = true; break; } // End of switch } // End of while System.out.println("Exiting program."); } // End of main

(11)

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; } }

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;

References

Related documents

Introduction to linear data structures- Arrays, String, representation &amp; Operations, Linked List: Representation of linked list in memory, different implementation of

You may wonder why another cursor to step through the data structure, but when you traverse a linked list, maybe a cursor in use is at an important position before traversal and

Therefore dynamic SCPC delivers the same dynamic bandwidth allocation functionality of the iDirect Shared Service, while dramatically reducing required return link

Specialization: Digital design(System design,RTL and Verification), Verilog Main business: Design, Research, Offshore projects. Description: ARF-Design works in two domains of

Import javaio Java program to implement a Singly Linked List public class LinkedList Node head head of list Linked list Node This inner class is.. The list gives access to the

The last node points to NULL Declaring a Linked list In C language a linked list would be implemented using structure and pointers struct LinkedList int data.. Creating nodes

In fact, the next assignment will ask you to implement &#34;standalone&#34; linked list functions that operate on provided linked lists, outside the context of a class?. ● This is

Dalam hal ini perjanjian internasional yang menjadi objek kajian adalah Mutual Commitments antara Pemerintah Liberia dan United Nations Peacebuilding Commision dalam