Solved

Looking for help in creating a class to handle a stack

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.
Suggested Courses

627 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