[go: up one dir, main page]

0% found this document useful (0 votes)
55 views2 pages

Source Code

1) The document describes an ArrayQueue class that implements a queue using an array as the underlying data structure. It includes methods like enqueue to add an item, dequeue to remove an item, and getFront to access the front item. 2) The ArrayStack class similarly implements a stack using an array, with methods like push to add an item and pop to remove the top item. 3) A QueueStackTester class contains a main method that demonstrates using the ArrayQueue and ArrayStack classes.

Uploaded by

Bharath Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views2 pages

Source Code

1) The document describes an ArrayQueue class that implements a queue using an array as the underlying data structure. It includes methods like enqueue to add an item, dequeue to remove an item, and getFront to access the front item. 2) The ArrayStack class similarly implements a stack using an array, with methods like push to add an item and pop to remove the top item. 3) A QueueStackTester class contains a main method that demonstrates using the ArrayQueue and ArrayStack classes.

Uploaded by

Bharath Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SOURCE CODE */

public Object getFront( )


//package com.maherfet .demo.queue; {
if( isEmpty( ) )
/** throw new RuntimeException( "ArrayQueue getFront" );
* Array-based implementation of the queue. return theArray[ front ];
* @author Durai Murugan }
*/
public class ArrayQueue { /**
private Object [ ] theArray; * Insert a new item into the queue.
private int currentSi ze; * @param x the item to insert.
private int front; */
private int back; public void enqueue( Object x )
{
private static final int DEFAULT_CAPACITY = 10; if( currentSize == theArray.length )
doubleQueue( );
/** back = increment( back );
* Construct the queue. theArray[ back ] = x;
*/ currentSize++;
public ArrayQueue( ) }
{
theArray = new Object[ DEFAULT_CAPACITY ]; /**
makeEmpty( ); * Internal method to increment with wraparound.
} * @param x any index in theArray's range.
* @return x+1, or 0 if x is at the end of theArray.
/** */
* Test if the qu eue is logically empty. private int increment( int x )
* @return <b>true</b> if empty, <code>false</code> otherwise. {
*/ if( ++x == theArray.length )
public boolean isEmpty( ) x = 0;
{ return x;
return currentSize == 0; }
}
/**
/** * Internal method to expand theArray.
* Make the queue logically empty. */
*/ private void doubleQueue( )
public void makeEmpty( ) {
{ Object [ ] newArray;
currentSize = 0;
front = 0; newArray = new Object[ theArray.length * 2 ];
back = -1;
} // Copy elements that are logically in the queue
for( int i = 0; i < currentSize; i++, front = increment( front ) )
/** newArray[ i ] = theArray[ front ];
* Return and remove the least recently inserted item
* from the queue. theArray = newArray;
* @return the least recently inserted item in the queue. front = 0;
*/ back = currentSize - 1;
public Object dequeue( ) }}
{ //package com.st.joesph.demo.queue;
if( isEmpty( ) )
throw new RuntimeException( "ArrayQueue dequeue" );
currentSize --;
/**
Object returnValue = theArray[ front ]; * Array-based implementation of the stack.
front = increment( front ); * @author Durai Murugan
return returnValue; */
} public class ArrayStack {

/** private Object [ ] theArray;


* Get the least recently inserted item in the queue. private int topOfStack;
* Does not alter the queue. private static final int DEFAULT_CAPACITY = 10;
* @return the least recently insert ed item in the queue.
/** /**
* Construct the stack. * Internal method to extend theArray.
*/ */
public ArrayStack( ) { private void doubleArray( ) {
theArray = new Object[ DEFAULT_CAPACITY ]; Object [ ] newArray;
topOfStack = -1;
} newArray = new Object[ theA rray.length * 2 ];
for( int i = 0; i < theArray.length; i++ )
/** newArray[ i ] = theArray[ i ];
* Test if the stack is logically empty. theArray = newArray;
* @return true if empty, false otherwise. } }
*/
public boolean isEmpty( ) { //package com.maherfet .demo.queue;
return topOfStack == -1;
} public class QueueStackTester {

/** public static void main(String[] args) {


* Make the stack logically empty.
*/ System.out.println("****************************");
public void makeEmpty( ) { System.out.println("Queue Example");
topOfStack = -1; System.out.println("****************************");
}
ArrayQueue aq = new ArrayQueue();
/**
* Get the most recently inserted item in the stack. aq.enqueue(new String("1"));
* Does not alter the stack. aq.enqueue(new String("2"));
* @return the most recently inserted item in the stack. aq.enqueue(new String("3"));
*/ aq.enqueue(new String("4"));
public Object top( ) {
if( isEmpty( ) ) System.out.println("Queue Elements -> 1, 2, 3, 4");
throw new RuntimeException( "ArrayStack top" ); System.out.println("Queue FIFO -> "+aq.getFront());
return theArray[ topOfStack ]; System.out.println("Queue removed element -> "+ aq.dequeue());
} System.out.println("Queue FIFO -> "+aq.getFront());

/** System.out.println("****************************");
* Remove the most recently inserted item from the stack. System.out.println("Stack Example");
*/ System.out.println("****************************");
public void pop( ) {
if( isEmpty( ) ) ArrayStack arrayStack = new ArraySt ack();
throw new RuntimeException( "ArrayStack pop" ); arrayStack.push(new String("a"));
topOfStac k--; arrayStack.push(new String("b"));
} arrayStack.push(new String("c"));
arrayStack.push(new String("d"));
/**
* Return and remove the most recently inserted item System.out.println("Stack Elements -> a, b, c, d");
* from the stack. System.out.println("Stack LIFO -> "+arrayStack .top());
* @return the most recently inserted item in the stack. arrayStack.pop();
*/ System.out.println("POP on Stack " );
public Object topAndPop( ) { System.out.println("Stack LIFO -> "+arrayStack.top());
if( isEmpty( ) )
throw new Run timeException( "ArrayStack topAndPop" ); }}
return theArray[ topOfStack -- ];
} OUTPUT Stack Example
****************************
/** E:\User\Lathadelvi \Experiment 1>java Stack Elements -> a, b, c, d
* Insert a new item into the stack. QueueStackTester Stack LIFO -> d
* @param x the item to insert. **************************** POP on Stack
*/ Queue Exam ple Stack LIFO -> c
public void push( Object x ) { ****************************
if( topOfStack + 1 == theAr ray.length ) Queue Elements -> 1, 2, 3, 4 E:\User\Lathadelvi \Experiment 1>
doubleArray( ); Queue FIFO -> 1
theArray[ ++topOfStack ] = x; Queue removed element -> 1
} Queue FIFO -> 2
****************************

You might also like