For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2005 > badly need help regarding stacks and queues..









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author badly need help regarding stacks and queues..
sicnarf0916

2005-11-26, 1:40 pm

Hi...I am new here..any kind hearted people here who will help me here.I have this assignment that i cant figure out what to do..Here you can read it..

Q2. Linked List

a. create a compile AStack class, and a AQueue class similar to the text
b. provide an application that will:
• instantiate an AStack object and an AQueue object
• create a property object for each data line read from the input file assign4.dat
• push it ti the stack if its is category 1 property; queue if its is category 2; ignore all others
• print a title called category 1 to an output file called outTwo.dat
• after printing a few blank lines to outTwo.dat, print a title called category 2 to outTwo.dat
• traverse the queue and print it to the same output file outTwo.dat
• print the total commission earned on this category of property to outTwo.dat

Note that for this question you should also print out and submit outTwo.dat. Have Fun!


I already compiled the AStack and AQueue..I just have to create an appplication for this one.I would appreciate all your help...

//here's the AStack and AQueue

public class AStack {

// Insert inner class ListNode here.
private class ListNode {
// Data fields
Object info;
ListNode link;

// Methods
// Constructors
public ListNode() {};

public ListNode(Object i) {info = i; link = null;}

public ListNode(Object i, ListNode l) {
info = i;
link = l;
}
}
// end ListNode

// Data fields
private ListNode top; // reference to
// top of stack

// Methods
/* push - pushes an item onto a stack
precondition : item is defined
postcondition: item is at the top of the stack
*/
public AStack() {
top = null;
}

public void push(Object item) {
//Allocate a new node, store item in it, and
// link it to old top of stack.
top = new ListNode(item, top);
}


/* pop - pops an item from a stack
precondition : stack is defined and is not empty
postcondition: returns item at top of stack and
removes it from the stack. Old second
stack element is at top of stack.
*/
public Object pop() {
Object item = p(); // Retrieve item at top

//Remove old top of stack
top = top.link; // Link top to second element

return item; // Return data at old top
}


/* p - retrieves an item from a stack
* precondition : stack is defined and is not empty
* postcondition: returns item at top of stack without
* removing it.
*/
public Object p() {
if (isEmpty())
throw new NullPointerException();
return top.info; //return top item
}


/* isEmpty - tests whether a stack is empty
* precondition : none
* postcondition: returns true if stack is empty;
* otherwise, returns false.
*/
public boolean isEmpty() {
return (top == null);
}

} // class AStack

//start of AQueue

public class AQueue {
// Data fields
private ListNode front; // reference to front of queue
private ListNode rear; // reference to rear of queue
private int size; // size of queue

class ListNode {

// Data fields
Object info; // data stored in the node
ListNode link; // link to next node

// Methods
// Constructors
public ListNode() {info = ""; link = null;}

public ListNode(Object i) {info = i; link = null;}

public ListNode(Object i, ListNode l) {
info = i;
link = l;
}
} // class ListNode

// Methods
// postcondition: Inserts item in a new node and resets
// rear to reference the new node. If the queue has
// 1 element, front references the new node also.
public void insert(Object item) {

if (isEmpty()) { //empty queue
// Link rear and front to only node.
rear = new ListNode(item);
front = rear;
}
else { //extend a non-empty queue
rear.link = new ListNode(item);
rear = rear.link; // Move rear to new node.
}

size++; //Increment queue size.
}


// postcondition: If the queue is not empty, returns its
// first element & front references new first element.
public Object remove() {
Object item = p(); // Retrieve first item.

// Remove first element
front = front.link; // Delete first node.
size--; // Decrement queue size.

return item;
}


// precondition : The queue has been created.
// postcondition: If the queue is not empty, returns its
// first element.
public Object p() {
if (isEmpty())
throw new NullPointerException();
return front.info;
}


// postcondition: Returns true if queue is empty;
// otherwise, returns False.
public boolean isEmpty() {
return (size == 0);
}


public int getSize() {
return size;
}

public String toString() {
String result = "";
ListNode next = front;

while (next != null) {
result = result + next.info + "\n";
next = next.link;
}
return result;
}





} // class AQueue

Thank You!!!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com