Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [C++ Help] When are linked lists used, exactly? And
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shinobu




PostPosted: 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
Sponsor
sponsor
Dreadnought




PostPosted: 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.
c:
#define NULL 0
Shinobu




PostPosted: Tue Sep 03, 2013 4:19 pm   Post subject: Re: [C++ Help] When are linked lists used, exactly? And

By example, I mean in code Razz
I guess I'll just stick with the cstddef header file for now then, since that's what was shown in the CPP book.

I'm also having
another problem.
I'm trying to get input using the getch function from conio header file.
I want the program to stop trying to get input when the user inputs a 1, 2, 3, or a 4.
This doesn't work:
c++:
while (*userInput != '1' || *userInput != '2' || *userInput != '3' || *userInput != '4')
        {
                *userInput = getch(); //Store the user's choice into variable userInput
        }

But this does:
c++:
while (1)
        {
                *userInput = getch(); //Store the user's choice into variable userInput
                if (*userInput != '1' || *userInput != '2' || *userInput != '3' || *userInput != '4')
                        break;
        }

Why is that? Sad

Thanks for helping me btw Smile
Dreadnought




PostPosted: 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




PostPosted: 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 Sad
I'm pretty sure I didn't do anything wrong, but I could've lol
Dreadnought




PostPosted: 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




PostPosted: 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 Embarassed
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 Rolling Eyes

Thanks for the help Dreadnought! Mr. Green
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: