Results 1 to 4 of 4
- 10-31-2012, 04:30 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
how to is implementing both stack and queue?
hi all
help me plz. in this Q.
Write a program that is implementing both stack and queue AND than
1. Continuously get input from user until user wanted to stop.
2. For every input given by user, check if the input is an even or odd number. If the input is even, insert into a stack. But if the input is odd, insert into a queue.
3. Display the content of both stack and queue after completing all operations
my code.gif)
Java Code:class StackX { private int maxSize; // size of stack array private long[] stackArray; private int top; // top of stack private int size; //-------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; // set array size stackArray = new long[maxSize]; // create array top = -1; // no items yet } //-------------------------------------------------------------- public void push(long j) // put item on top of stack { stackArray[++top] = j; // increment top, insert item size++; } //-------------------------------------------------------------- public long pop() // take item from top of stack { size--; return stackArray[top--]; // access item, decrement top } //-------------------------------------------------------------- public long peek() // peek at top of stack { //-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } //-------------------------------------------------------------- public boolean isFull() // true if stack is full { return (top == maxSize-1); } //-------------------------------------------------------------- public int size() { return size; } //-------------------------------------------------------------- } // end class StackX // Queue.java // demonstrates queue // to run this program: C>java QueueApp //////////////////////////////////////////////////////////////// class Queue { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; //-------------------------------------------------------------- public Queue(int s) // constructor { maxSize = s; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; } //-------------------------------------------------------------- public void insert(long j) // put item at rear of queue { if(rear == maxSize-1) // deal with wraparound rear = -1; queArray[++rear] = j; // increment rear and insert nItems++; // one more item } //-------------------------------------------------------------- public long remove() // take item from front of queue { long temp = queArray[front++]; // get value and incr front if(front == maxSize) // deal with wraparound front = 0; nItems--; // one less item return temp; } //-------------------------------------------------------------- public long peekFront() // peek at front of queue { return queArray[front]; } //-------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return (nItems==0); } //-------------------------------------------------------------- public boolean isFull() // true if queue is full { return (nItems==maxSize); } //-------------------------------------------------------------- public int size() // number of items in queue { return nItems; } public long peekrear() { return queArray[rear]; } //-------------------------------------------------------------- } // end class Queue
Java Code:import java.util.Scanner; import java.util.*; public class sqapp { public static void main(String[] args) { Scanner input=new Scanner(System.in); Queue theQueue = new Queue(3); int x,ans; System.out.println("Enter Your Data : "); x=input.nextInt(); theQueue.insert(x); for(int i=0; i<2; i++){ System.out.print("Do you want to Enter more Data [1.yes - 2.no] : "); ans=input.nextInt(); if(ans==1){ System.out.print("Enter Data : "); x=input.nextInt(); } for(int x=0; x<10; x++){ if (x%2==0){ theQueue.insert(x); } else thestack.insert(x); } }
I tried but I did not get the required
- 10-31-2012, 04:33 PM #2
Re: how to is implementing both stack and queue?
Is that really how you indent your code? I really suggest fixing that before you do anything else.
What exactly is the problem? What does this code do? Have you stepped through it with a debugger or at least added some print statements to figure out what's going on?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-31-2012, 05:17 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Re: how to is implementing both stack and queue?
the output like this
enter your number
3
do you want to enter more data 1-yes-2-no
1
enter data
3 // if this number even insert into a stack. But if the input is odd, insert into a queue
do you want to enter more data 1-yes-2-no
1
enter data
4 // if this number even insert into a stack. But if the input is odd, insert into a queue
do you want to enter more data 1-yes-2-no
1
enter data
1 // if this number even insert into a stack. But if the input is odd, insert into a queue
do you want to enter more data 1-yes-2-no
1
enter data
2 // if this number even insert into a stack. But if the input is odd, insert into a queue
do you want to enter more data 1-yes-2-no
2
the stack is
4 2
the queue is
3 1
I hope you understand
- 10-31-2012, 06:06 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Re: how to is implementing both stack and queue?
i get the answer
.gif)
Java Code:import java.util.Scanner; public class evenStackoddQueue { public static void main (String[] args) { Scanner input = new Scanner(System.in); StackX S = new StackX(10); Queue qu = new Queue(10); System.out.println("\n ** WELCOME in Stack & Queue program\n \n Note: the maximum size is 10"); System.out.println(" \n\nEnter Number OR '0' to stop the program: "); int num = input.nextInt(); while( num != 0) { num = input.nextInt(); if( num != 0) // because '0' is used only to stop the program { if ( num%2 == 0) S.push(num); else qu.insert(num); } } System.out.println(""); System.out.println("The elements in the Stack:> "); while( !S.isEmpty() ) { long s = S.pop(); System.out.print(s); System.out.print(" "); } System.out.println(""); System.out.println(""); System.out.println("The elements in the Queue:> "); while( !qu.isEmpty() ) { long q = qu.remove(); System.out.print(q); System.out.print(" "); } System.out.println(""); System.out.println(""); } } // Stack class class StackX { public int maxSize; // size of stack array public int [] stackArray; public int top; // top of stack //-------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; // set array size stackArray = new int [maxSize]; // create array top = -1; // no items yet } //-------------------------------------------------------------- public void push(int j) // put item on top of stack { stackArray[++top] = j; // increment top, insert item } //-------------------------------------------------------------- public int pop() // take item from top of stack { return stackArray[top--]; // access item, decrement top } //-------------------------------------------------------------- public int peek() // peek at top of stack { return stackArray[top]; } //-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } //-------------------------------------------------------------- public boolean isFull() // true if stack is full { return (top == maxSize-1); } //-------------------------------------------------------------- public int size() { return top+1; } } // end class StackX // Queue Class class Queue { public int maxSize; public long[] queArray; public int front; public int rear; public int nItems; //-------------------------------------------------------------- public Queue(int s) // constructor { maxSize = s; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; } //-------------------------------------------------------------- public void insert(long j) // put item at rear of queue { if(rear == maxSize-1) // deal with wraparound rear = -1; queArray[++rear] = j; // increment rear and insert nItems++; // one more item } //-------------------------------------------------------------- public long remove() // take item from front of queue { long temp = queArray[front++]; // get value and incr front if(front == maxSize) // deal with wraparound front = 0; nItems--; // one less item return temp; } //-------------------------------------------------------------- public void display() { int index = front; while( index <= rear ) { long temp = queArray[index++]; System.out.print(temp); System.out.print(" "); } } //-------------------------------------------------------------- public long peekFront() // peek at front of queue { return queArray[front]; } //-------------------------------------------------------------- public long peekRear() // peek at rear of queue { return queArray[rear]; } //-------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return (nItems==0); } //-------------------------------------------------------------- public boolean isFull() // true if queue is full { return (nItems==maxSize); } //-------------------------------------------------------------- public int size() // number of items in queue { return nItems; } //-------------------------------------------------------------- } // end class QueueLast edited by noor22; 10-31-2012 at 06:22 PM.
Similar Threads
-
Implementing a stack as an array
By fam2315 in forum New To JavaReplies: 2Last Post: 06-22-2011, 03:55 PM -
Java Stack and Queue
By jeanjiang in forum New To JavaReplies: 3Last Post: 04-25-2011, 02:19 PM -
Implementing a Stack Using two Queues
By rhm54 in forum New To JavaReplies: 3Last Post: 12-01-2010, 10:28 AM -
Computation Complexities for a stack, queue, and map?
By daletron3030 in forum New To JavaReplies: 0Last Post: 03-18-2009, 06:13 AM -
Need help please – implementing a stack!
By sfe23 in forum New To JavaReplies: 0Last Post: 02-24-2009, 03:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks