Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-15
3
212 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AWS and Java and Rest - newbie question (Is this an authentication issue) 4 68
mockito example issue 8 64
custom annotations 9 33
Spring Framework HTTPSession management 1 24
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

863 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

28 Experts available now in Live!

Get 1:1 Help Now