Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-15
3
201 Views
Last Modified: 2013-11-23
Looking for help in creating a class to handle a stack from scratch without using java.util*

This is what I have so far..

public interface Stack
{
      public boolean stackEmpty();
      public boolean stackFull();
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      public void pushStack(Object x);
      // Adds to head (top) of stack if not full
      {
            if (this.sFull()) ... ;
            Else
            {
                                                Node temp = new Node(x, head);
                  Head = temp;
            }
      }

      public Object popStack();
      // remove top of  the stack if not empty
{
            if (this.sEmpty()) ... ;
            Else
            {
                  Node temp = head;
                  head = head.getNext();
                  return temp.getContents();
            }

      public Object peekStack();


}
      
public class ContigStack implements Stack
{
      public static final int SIZE = 1000;
      private int max;
      private Object data[];
      private int head;
      private int tail;
}

Am I on the right track ? How would I impletent a pop and push?

Many Thanks
Steve
0
Comment
Question by:Stephen Manderson
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20293277
on the right track:

public interface Stack
{
      public boolean stackEmpty();
      public boolean stackFull();
      public void pushStack(Object x);
      public Object popStack();
      public Object peekStack();


}
     
public class ContigStack implements Stack
{
      public static final int SIZE = 1000;
      private int max;
      private Object data[];
      private int head;
      private int tail;

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      public void pushStack(Object x)
      {
            if (this.sFull()) throw new RuntimeException("Stack Full");
            Node temp = new Node(x, head);
            Head = temp;
      }
     
      public Object popStack()
      {
          if (this.sEmpty()) return null;
          Node temp = head;
          head = head.getNext();
          return temp.getContents();
      }
     
}
0
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 20293473
Hi Objects thanks for the reshuffle, would the following stackpeek be right to just show the head without altering? lastly how would I extend this to handle a stack of characters?

public interface Stack
{
      public boolean stackEmpty();
      public boolean stackFull();
      public void pushStack(Object x);
      public Object popStack();
      public Object peekStack();


}
     
public class ContigStack implements Stack
{
      public static final int SIZE = 1000;
      private int max;
      private Object data[];
      private int head;
      private int tail;

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      public void pushStack(Object x)
      {
            if (this.stackFull()) throw new RuntimeException("Stack Full");
            Node temp = new Node(x, head);
            Head = temp;
      }
     
      public Object popStack()
      {
          if (this.stackEmpty()) return null;
          Node temp = head;
          head = head.getNext();
          return temp.getContents();
      }

      public Object peekStack()
      {
       if (this.stackEmpty()) return null;
       head = head.getNext();
      }
}
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20293575
     public Object peekStack()
      {
       if (this.stackEmpty()) return null;
       return head.getContents();
      }

Use the Character class with your existing class for qa stack or characters
eg.
Stack stack = new ContigStack();
char c = 'c';
stack.pushStack(new Character(c));
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now