6 Replies - 778 Views - Last Post: 13 February 2012 - 06:03 PM Rate Topic: -----

#1 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

implementing own stack class

Posted 13 February 2012 - 05:25 PM

So i'm trying to implement my own Stack class with the usual pop, peek, push, and isEmpty. I'm having trouble on how to even start to implement this class generically. It's nothing so far but I have the layout.
public class myOwnStack<T extends Comparable<? super T>>{
   private Stack<T> stack;
   private int numberOfEntries;
   private Object stack1;
   public myOwnStack()
   {
       myOwnStack stack1 = new myOwnStack();
       numberOfEntries = 0;
   }
   public void push(T item)
   { 
   }
   public T pop()
   {
   }
   public T peek()
   {
   }
   public boolean isEmpty()
   {
       if(numberOfEntries == 0)
           return true;
       else
           return false;
   }
}


i'm guessing for the push, i need to add an item into the stack then increment numberofentries. For peek, i'm just checking what the top object is. and for pop, i'm just removing the peek which is the top object. How do i go about writing a the code for this? I just need help on 1 method and i can figure out the rest.

This post has been edited by mastapro: 13 February 2012 - 05:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: implementing own stack class

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: implementing own stack class

Posted 13 February 2012 - 05:36 PM

Though not the ultimate solution, it's useful to explore this task in steps. Start with an array of a finite length. For example, create an array of ints, int[] stack = new int[25]. Use a pointer to identify the 'top' of the stack, int pointer. Initially the pointer is = -1 and is incremented as items are pushed to the stack, decremented as items are popped. If the pointer = -1, the stack is empty, if pointer = stack.length - 1, the stack is full. How you handle a full stack is up to you. Do you throw an exception, discard the excess, etc? You can also use the pointer to determine how many items are on the stack.

Write one for objects.

After exploring that, move to an ArrayList<>.

There are several good, complete discussions of data structures on the web. Search 'java data structures'.
Was This Post Helpful? 0
  • +
  • -

#3 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: implementing own stack class

Posted 13 February 2012 - 05:46 PM

appreciate the help, i think i'm on my way to figuring it out so far. thanks

i think i'm using an exception for the peek and pop. i have to write my own class, how do u go about doing that? i have been searching forever and have no idea
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: implementing own stack class

Posted 13 February 2012 - 05:52 PM

Do you mean to throw the exception? You can always use a simple:
if ( conditionForException )
{
   throw new RuntimeException("Error message");
}

But I'm not sure that's what you meant. If you need something more complex (not sure why), you can check the Java tutorials on exception handling for ideas.
Was This Post Helpful? 0
  • +
  • -

#5 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: implementing own stack class

Posted 13 February 2012 - 05:58 PM

Ya i guess I have to make my own class, like for example
public T peek() throws EmptyException{
}


Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: implementing own stack class

Posted 13 February 2012 - 06:02 PM

I suggest you write the Stack, wring it out until you think you understand it backwards and forwards, and then add frills - especially ones you'll be learning - like exception handling.
Was This Post Helpful? 0
  • +
  • -

#7 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: implementing own stack class

Posted 13 February 2012 - 06:03 PM

alright thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1