Results 1 to 4 of 4
Thread: Stack implementation
- 10-05-2009, 01:23 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Stack implementation
Hi I am trying to write a program about learning stacks. My req's are:
Write a stack class with the methods:
peek
size
isEmpty
toString
I also write a driver class to test the stack. The driver should create a stack of Strings then read in a series of strings from input. If the string starts with an @ symbol, then it is a command otherwise it is pushed into the stack. The commands are:
@POP
@PEEK
@SIZE
@DISPLAY
@QUIT (end the program and exit loop)
My problem is not with the stacks but with seperating the strings from the commands. Im not quite sure how to differentiate between the two. My thoughts are to tokenize it somehow but Im not sure how. Here is my code so far:
import java.util.*;
public interface StackADT<T>
{
public void push(T element);
public T pop();
public T peek();
public int size();
public boolean isEmpty();
public String toString();
}
import java.util.*;
public class Driver
{
public static void main(String[] args)
{
int quit = 0;
String input;
Stack stack = new Stack();
System.out.println("Enter a name or one of the following commands:");
System.out.println("@POP @PEEK @SIZE @DISPLAY @QUIT");
do
{
System.out.println("Please enter a command: ");
Scanner in = new Scanner(System.in);
input = in.nextLine();
if (input == "@POP")
System.out.println("The value is: " + stack.pop());
else
if (input == "@PEEK")
System.out.println("The value is: " + stack.peek());
else
if (input == "@SIZE")
System.out.println("The value is: " + stack.size());
else
if (input == "@DISPLAY")
System.out.println("The value is: " + stack.toString());
else
if (input == "@QUIT")
quit = 1;
else
stack.push(input);
}
while (quit != 1);
}
}
- 10-05-2009, 09:07 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You should use the equals method to compare strings instead of ==.
- 10-05-2009, 03:55 PM #3
before comparing using equals() use trim() to trim the space
Ramya:cool:
- 10-07-2009, 12:59 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Help on Stack Implementation
By danver_2009 in forum New To JavaReplies: 1Last Post: 02-16-2009, 08:12 AM -
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12-19-2008, 11:58 PM -
Need little Help In Calculater Implementation..
By realahmed8 in forum New To JavaReplies: 6Last Post: 12-18-2008, 01:39 AM -
Graph DPS and BFS implementation
By hey in forum New To JavaReplies: 1Last Post: 01-09-2008, 09:19 PM -
Help with recursive implementation
By toby in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks