I’m trying to create a Stack to take a string and add each of the strings characters to it, but I was told it would be far more efficient use a LinkedList. How would I use a LinkedList to create and manipulate a stack?
An example would be very appreciated!
Ok, the problem is that you’re not using
Firstat all. Try the following:Because you never use
Firstit’s alwaysnull– so your loop never runs at all! Instead of using that at all, just use the build inisEmpty()function.Edit: Of course, you don’t really need those functions at all – the following will work fine:
Now this is still a bit unsafe – you can go one step further by using the following:
That way it’s a bit safer, and returns
Characters instead ofObjects– andCharacterscan be implicitly cast tochar🙂