Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Implement My Own Stack As A Linkedlist?

linked list stack

  • Please log in to reply
24 replies to this topic

#1 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 26 June 2012 - 10:45 PM

Hi guys. I haven't really used collections much so I'm having trouble with this simple assignment.

It's asking me to create a simple book records program that holds a stack of books. That stack has to be a linked list and I have to implement it myself instead of using java.util.Stack.

I'm not sure where to start. Can anyone help me out? I can easily do this if I was allowed to use arrays.

Assignment:

When the program runs, it (1) reads book records from the input file, bookrecs.txt,
into a stack (implemented by you), (2) displays book records from the stack, and (3)
allows the user to remove a book (The user will be asked to enter the book ID) from
the stack. After the removal of a book, the program needs to display book records
from the stack to verify that the specified book has been removed.


Edit: Also, if someone could explain how to implement it as Queue too because I have to do to the same exact thing, but using a queue.
  • 0

#2 mctim

mctim

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 26 June 2012 - 11:48 PM

Can you use deque? Otherwise what your gonna have to do is implement something called a node. Nodes are basically just a place to store one object and a pointer to the next node. Being that what you are creating is a stack each node will point to the node that is "on top of it." Think about that it may give you some help. Otherwise I'd say come back w/ some source code.
  • 0

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 26 June 2012 - 11:53 PM

Do you know what are Stack and Queue? If not, study them first -- Stack, Queue.

And, as this is a homework, you should post it in homework section.

For stack implementation in java, read these tutorials -- tutorial1, tutorial2 and tutorial3.
  • 0

#4 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 27 June 2012 - 12:12 AM

I get the push and pop part, but I don't get how I'm going to do this: (3)
allows the user to remove a book (The user will be asked to enter the book ID) from
the stack. Removing a record with a certain ID number. Do I keeping popping until I find the record with that ID?
  • 0

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 27 June 2012 - 12:24 AM

No, if you look at the Javadoc, look at the "Methods inherited from class java.util.Vector" section.
remove would be a nice one, or possibly removeElementAt if you know the index.
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 June 2012 - 12:35 AM

No, I think the logic will be like if the provided book-ID match with the top element id of the stack, you should remove that, otherwise not.

Another way is to search all the items in the stack to find and remove it.
  • 0

#7 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 27 June 2012 - 12:40 AM

Would I have to perform some sort of iteration on the stack to search for that record with the ID entered?
  • 0

#8 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 27 June 2012 - 12:51 AM

Stack has a search method. http://docs.oracle.c...va.lang.Object)

As long as your Book class overrided the equals method properly, it should find the books correctly.i
  • 0

#9 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 June 2012 - 12:52 AM

Yeah, that's right. For the second case, you need to iterate over the stack to find it out. You know the iteration using java array? I think you better use the ArrayList class.
  • 0

#10 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 27 June 2012 - 01:07 AM

So I would first create a stack like this:
Stack stack<Book> = new Stack<Book>();
//add all book records to stack:
while(..){
stack.push(book);
}
//find
stack.search(>>what would I put in here?<<);
//How can I say find an object with this ID? I've got the getID() method in book class.

So it says that I have to implement my own stack which means that I have to implement the push and pop by myself. If I create a new class called Stack and then use LinkedLists? I'm really lost right now. :blushing:
  • 0

#11 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 June 2012 - 01:09 AM

You will just ask user to input a id for a book. You can read an integer from console with this code --
Scanner in = new Scanner(System.in);
int num = in.nextInt();

Then you need to pass this num variable in search method. Then in the search method implementation in Stack class, you need to iterate over the items in the stack to find out the book with that given id.

Another way is that, you can implement a method, as example with signature 'boolean deleteEntryWithID(int id)', and you will iterate over the items in this method to find the item and delete it if found.

So it says that I have to implement my own stack which means that I have to implement the push and pop by myself. If I create a new class called Stack and then use LinkedLists? I'm really lost right now. :blushing:

I better suggest you to read all the links first I posted in my first comment. If you read those, you'll learn to create your own Stack class.
  • 0

#12 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 27 June 2012 - 01:12 AM

Okay, but what about LinkedList. My stack is going to be actually a linkedList, right? How do I do that?

I better suggest you to read all the links first I posted in my first comment. If you read those, you'll learn to create your own Stack class.

Reading now. But the first tutorial is using a vector instead of a linkedList? Can you also show me how to use a LinkedList too please?
  • 0





Also tagged with one or more of these keywords: linked list, stack

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download