开发者

Easy applications for learning Java Data Structures

开发者 https://www.devze.com 2022-12-22 03:00 出处:网络
I have been struggling to think of some decent uses for things like vectors and stacks. Since I find myself best able to remember things once i\'ve done something useful with it. I was after some shor

I have been struggling to think of some decent uses for things like vectors and stacks. Since I find myself best able to remember things once i've done something useful with it. I was after some short but useful applications you've found for several of the java data structures.

I'm not after any code samples, but more 开发者_JS百科things that stick in your mind as 'that was a really great use of a hashmap/linked list etc' - things that i could then go on to try myself.


"Usefulness" is a subjective term, but in any case, an intuitive way to learn data structures is to use them to simulate real-life activities.

Stack

Simulate a secretary that is shredding a bunch of documents. She has -- guess what? -- a stack of documents on her desk, and she shreds them one by one by picking the top document and feeding it into the shredder, repeating this until all documents are shredded.

Her boss would intermittently come over to her desk and put a new document to shred on top of her stack.

Circular doubly linked list

Simulate kids playing in the playground. The kids stand in a circle, then each kid would -- guess what? -- link up by holding hands with the kid to the left (with the left hand) and to the right (with the right hand).

Do "Eeny, meeny, miny, moe" around the circle, say starting from the youngest kid. The "it" kid would then have to leave the circle, and the gap is closed in the most natural way, i.e. by having the two kids around the gap link up.

Restart the "Eeny, meeny, miny, moe" from the gap. Go the opposite direction on a whim. Do this until one kid remains.

Map

Dog says woof. Cow says moo. Yeah, simulate that.


A good use for a Stack would be bracket matching. Write a small program that will parse some input and report back if the bracket syntax was correct (i.e. every open bracket has a corresponding closing bracket).


How about an RPN Calculator for Stacks? Vectors can be applied to almost any problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号