Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Looking for help in creating a class to handle a stack

Posted on 2007-11-15
3
Medium Priority
?
240 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

721 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