Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-15
3
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 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