[C++ Help] When are linked lists used, exactly? And
Author |
Message |
Shinobu
|
Posted: Tue Sep 03, 2013 10:57 am Post subject: [C++ Help] When are linked lists used, exactly? And |
|
|
I'm really confused as to when I should use it. I'm actually kinda having trouble understanding how to use it though, but I'll try and figure that out myself.
Is there a time when I should actually use Data structures/linked lists, or should I not even bother with them?
By the way, the ebook I'm using says that some header files includes what I need in order to use NULL.
What header files does include it other than cstddef? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dreadnought
|
Posted: Tue Sep 03, 2013 3:14 pm Post subject: Re: [C++ Help] When are linked lists used, exactly? And |
|
|
Shinobu wrote:
Is there a time when I should actually use Data structures/linked lists, or should I not even bother with them?
I think you should definitely bother with them. Different data structures perform better in different scenarios, picking the one best suited to a particular problem you're working on can not only make your program more efficient but also easier to read (usually the correct data structure can fit into your code naturally.
Some examples:
Arrays allow you to access any item efficiently but for a linked list the time required to access an item depends on the position of that item in the list.
On the other hand, suppose your items must be ordered. To add or remove an item from some arbitrary position in an array can take a lot of time (you might have to move large portions of the array around) but for a linked list, the operation is easy.
Maybe you want to be able to search efficiently. Searching through an array or a linked list is not very efficient (you probably just try every item in sequence), however if you use a binary search tree then searching can take time proportional to the logarithm of the total number of items (for example you might need 10 checks to search through 1,000 items and 30 to check through 1,000,000,000).
Shinobu wrote:
By the way, the ebook I'm using says that some header files includes what I need in order to use NULL.
What header files does include it other than cstddef?
I believe it is also declared in stdlib.h but you can also just add the following line to your code.
|
|
|
|
|
|
Shinobu
|
|
|
|
|
Dreadnought
|
Posted: Tue Sep 03, 2013 11:38 pm Post subject: Re: [C++ Help] When are linked lists used, exactly? And |
|
|
I'm afraid I don't have a windows c++ compiler so I can't try out Windows I/O (and I'm not very familiar with it). In what way does the first example "not work"? What's the value of *userInput before the while loop? (If it doesn't have a value that might be your problem) |
|
|
|
|
|
Shinobu
|
Posted: Wed Sep 04, 2013 5:58 pm Post subject: Re: [C++ Help] When are linked lists used, exactly? And |
|
|
By not working, I meant it won't even get out of the loop lol.
For the first way, when I try to press a key, nothing happens. the program stays on the menu.
for the second way, the program actually continues.
for example, for the first way, i press 4. nothing happens.
for the second way, i press 4. the program detects that and closes the program.
I don't even know why it's like that
I'm pretty sure I didn't do anything wrong, but I could've lol |
|
|
|
|
|
Dreadnought
|
Posted: Wed Sep 04, 2013 7:55 pm Post subject: Re: [C++ Help] When are linked lists used, exactly? And |
|
|
Now that I look at it properly, there is a problem. You're using or instead of and.
Just think about it. Your looping while userInput is not 1 or userinput is not 2 or ...
So unless userInput is 1,2,3 and 4 at the same time you won't leave the loop.
In the second way you exit as soon as a key is pressed since you exit if userInput is not 1 or userInput is not 2 (so you won't leave when userInput is 1,2,3 and 4 at the same time. |
|
|
|
|
|
Shinobu
|
Posted: Wed Sep 04, 2013 8:45 pm Post subject: Re: [C++ Help] When are linked lists used, exactly? And |
|
|
Dreadnought @ September 4th 2013, 7:55 pm wrote: Now that I look at it properly, there is a problem. You're using or instead of and.
Just think about it. Your looping while userInput is not 1 or userinput is not 2 or ...
So unless userInput is 1,2,3 and 4 at the same time you won't leave the loop.
In the second way you exit as soon as a key is pressed since you exit if userInput is not 1 or userInput is not 2 (so you won't leave when userInput is 1,2,3 and 4 at the same time.
Oh yeah, messed up big time
I completely forgot that it should check for all of them.
When you talked about the second way, I actually Lol'd at how stupid I was
Thanks for the help Dreadnought! |
|
|
|
|
|
|
|