# Is having trouble with data structures common for beginners? [closed]

I'm taking my second course on Java. We are getting into data structures. I have done an assignment on a linked list, and now a stack. I had a hard time with the linked list. The stack gave me a little trouble, but was much easier.

Should I be worried about having a hard time with these algorithms and data structures? I just feel like I didn't really grasp it.

I think, you must not accept not understanding these things, because they are really fundamental. That being said, your not understanding them is nothing to feel bad about. You can explain a linked list to a child. So if your teacher failed to explain them to you, it is as much their fault. So you shouldn't spend time worrying, but rather try to find people, who can explain it to you. Often a fellow student is a far better teacher than a full-time academic.

## Think of Trains

Imagine, you have a set of railway carriages, where each carriage has enough capacity, to contain one piece of data. Each carriage has some sort of hook at it's end, which can be attached to another carriage's front.
This in fact gives you a linked list:

• the empty list: the train containing no carriages (and therefore carrying no data)
• adding an element: add a new carriage containing the element in front of the train and hook it to the rest of the train
• removing an element: find the carriage containing the element. Remove it (you might need a crane here :)), hook the carriage before with the carriage after.
• replacing an element: find the carriage containing the old element. Exchange the old element with the new element.
• inserting an element right after another: find the carriage containing the element after which you want to insert. Insert a new carriage after it, which is hooked accordingly (we don't want the train to fall apart) and put the the new element into it.

In contrast to that, you could think of an array as a train with a given number of carriages, that cannot be rearranged in any way. All you can do is to change the data within them. This model also explains a lot of the problems arrays have:

• If you want to insert one element before another, you will have to move all the following elements to the next carriage.
• If you want to remove one element, you will need to move all the following elements one carriage to the front.
• If you need a train with more carriages, you will have to construct a new one, because you can't just prepend a carriage. On the other hand, finding carriages in an array is much easier, because you can simply number them permanently (their order will never change).

As for the stack: A "stack" is less a data structure, than an idea. The idea of the stack is, that it acts much like a stack of books. You can only put books on top of the stack and you can only ever take the top book off the stack (at least if the books are sufficiently heavy).
That being said, a linked list can be used as a stack, if you think of the data in the carriages as books, and the book in the first most carriage as the top of the stack.

So I hope this helped you. Maybe it didn't. Maybe you're more of a visual type. In that case, I suggest you find somebody, who's good at giving visual explanations and explain it to you. It won't take long, but it will absolutely be worth it.

It's ok to struggle with this now. But merely accepting it, is not an option in the long run.

• Cool answer! Made me realize that one could teach basic data structures to kids through a flash/other game with a series of goals to complete. The same idea of trains can also help kids grasp binary numbers, if each train car is 2x larger and smaller than its neighbor.
– Job
Commented Oct 3, 2011 at 1:34
• Ok, now that you've got that, let's move on to data structures with multiple links. So the train has a hitch on it's side, which can hook onto another set of cars. But the rail for those cars kind of slides along next to the first track... Commented Oct 3, 2011 at 13:43

I wouldn't say that you "should be worried" about it, but the simple fact that you acknowledge your weak points shows that you know exactly where to study harder. I think you will be well served by that attitude and will be OK in the long run.

To quote my fav CSCI teacher:

``````"Panic, but panic early."
``````

Data Structures sounds hard, right? To me it does, it sounds abstract and a bit complex and most of all... important!

Data Structures is a vital course. And it's common to struggle, but keep going! As long as you eat your Wheaties and keep at it, you will reach the rainbow with a `bag` filled with `generic items` underneath.

• Panic early would imply dropping out? After data structures there are also math requirements, algorithms, OS, AI, compilers, theory of computation ... it does not get any mother-freaking easier, but maybe the fact that one is no longer a freshman or a sophomore does help even though stuff is getting harder.
– Job
Commented Oct 3, 2011 at 1:40

Very good points in other answers, just one note to add: IMO linked lists may be more difficult than e.g. stacks for a lot of people because they build on indirection (expressed via references / pointers). And these underlying concepts may be hard to grasp.

Data Structures was the first "hard" class I took; we used Fortran 77 instead of Java, but the concepts are largely the same.

It took me a week longer than my classmates to grok the concept of a linked list; I biffed the assignment, but after a couple of slightly frustrating sessions with my professor, it finally clicked (literally; I heard a "click" in my head when I finally understood).

Everyone has trouble somewhere in their CS curriculum (unless they're freaks). If you understand where your weaknesses are and how to address them, you really have nothing to worry about.

• Dont Panic, keep working and Wait for the click...
– user25446
Commented Oct 3, 2011 at 8:04

Did you have trouble understanding the linked list, or just trouble with your implementation?

It's not unusual for a new programmer to have difficulty there, because it may be the first time you have to think about what it really means when you write:

``````list.head = null;
Element e = new Element(...);
``````

I got all twisted up in ALGOL/W on the same exercise, because I didn't quite understand the language semantics. A year later, I could barely remember why I had difficulty.

There are bound to be some areas of software development you find harder than others. Whether it's certain algorithms, or certain design patterns or certain procedures will vary from person to person. I find that I have to use something on a real program before I fully understand it.

I'd be more worried if someone claimed to know it all and had never experienced problems learning something.

Personally I never seemed to have problems with linked lists, but then I worked on a program for 8 years that used them everywhere so I was working with them on a daily basis. As long as you know where to find the information you need to refresh your memory and know the areas where you have "trouble" you should be OK.

• I also think it would help if I could interact with a program that uses these data structures as well. I'm at the stage to where I am asking myself, why would these be useful? Commented Oct 2, 2011 at 17:35

I had trouble with calculus and had to take it a second time. The second time I discovered I was smart but the first math teacher was basically useless :)

You are going to find a lot of people in IT who can't communicate well, even teachers. On the other hand some people in IT are truly great writers and master communicators.

Sometimes outside reading can really help. Computer books vary hugely in quality. Get on Amazon and see what book people like actually liked.

Good luck.