Linked List Application

Joined
Nov 8, 2013
Messages
12
i need to find if a linked list is empty, return head and tail and to find the length of the linked list, i need the menu to let me choose as many times as i want from it, and not to exit automatically

#include<iostream>

using namespace std;

void create_node();
void check_if_node_empty();
void return_head();
void return_tail();
int length();
void display();

struct node{
char name[15];
int age;
float height;
node *next;
};
node *head= 0;

void main(){
int option;

cout<<"---MENU---"<<endl;
cout<<"1. Create Node"<<endl;
cout<<"2. Check if linked list is empty"<<endl;
cout<<"3. Return head"<<endl;
cout<<"4. Return tail"<<endl;
cout<<"5. Calculate linked list length"<<endl;
cout<<"6. Display"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Choose: ";
cin>>option;

switch (option){
case 1:create_node();break;
case 2:check_if_node_empty();break;
case 3:return_head();break;
case 4:return_tail();break;
case 5:length();break;
case 6:display();break;
case 7:exit(0);break;
default: cout<<"Choose from 1 to 4";break;
}

system("pause");
}
void create_node(){
node *temp;
temp = new node;

cout<<"Please enter the following details"<<endl;
cout<<"Please enter name: ";
cin>>temp->name;
cout<<"Please enter age: ";
cin>>temp->age;
cout<<"Please enter height: ";
cin>>temp->height;
temp->next = 0;

}
void check_if_node_empty(){
cout<<"Checking if linked list is empty...."<<endl;
}
void return_head(){
cout<<"Returning head...."<<endl;
}
void return_tail(){
cout<<"Returning tail...."<<endl;
}
int length(){
cout<<"Calculating linked list length...."<<endl;
int len = 0;
while(head != 0)
{
len++;
head = head->next;
}
return len;
}
void display(){
cout<<"Displaying...."<<endl;

node *temp;
temp = head;

// Display details for what temp points to
cout<<"Name : "<<temp->name<<endl;
cout<<"Age : "<< temp->age<<endl;
cout<<"Height : "<< temp->height<<endl;
cout<<endl;
}
 
I need lots of stuff too.

No one is going to sort through all your code and help you unless you demonstrate you've tried to fix the problem, and ask specific questions about the code and problems you are having.
 
i need to find if a linked list is empty, return head and tail and to find the length of the linked list, i need the menu to let me choose as many times as i want from it, and not to exit automatically

What approaches have you considered for these problems?

#include<iostream>

using namespace std;

Are you using C or are you using C++? It looks to me as though you are using C++, so my first hint is that you should probably take an object-oriented approach to solving this problem (I.E., you should use classes), if you know how.
 
Man this is all basic stuff and It's all in your text book if you only read it!

What approaches have you considered for these problems?



Are you using C or are you using C++? It looks to me as though you are using C++, so my first hint is that you should probably take an object-oriented approach to solving this problem (I.E., you should use classes), if you know how.

Use classes? LOL that's a good one given the basic questions they are asking for help with.
 
Use classes? LOL that's a good one given the basic questions they are asking for help with.

Since using classes (in C++) is arguably more basic of a topic than linked-lists, I don't see why you think that is such a bad approach. If using a class for this problem presents any difficulty, working with linked-lists should be deferred until the user has gained confidence with classes.

Additionally, since he clearly isn't writing this program out of a technical need, it is most likely that he's writing this in order to learn. The best way to learn would be for the user to do it the right way now, even though he can get away with doing it poorly, so that when a situation arises where doing it poorly isn't an option, it won't be his first shot at doing it correctly.

Glad to see you're capable of being helpful though:
Man this is all basic stuff and It's all in your text book if you only read it!

...My best guess is that you're not really much better at this than he is. Otherwise, you'd have found a more useful way to contribute.
 
Since using classes (in C++) is arguably more basic of a topic than linked-lists, I don't see why you think that is such a bad approach. If using a class for this problem presents any difficulty, working with linked-lists should be deferred until the user has gained confidence with classes.

Additionally, since he clearly isn't writing this program out of a technical need, it is most likely that he's writing this in order to learn. The best way to learn would be for the user to do it the right way now, even though he can get away with doing it poorly, so that when a situation arises where doing it poorly isn't an option, it won't be his first shot at doing it correctly.

Glad to see you're capable of being helpful though:


...My best guess is that you're not really much better at this than he is. Otherwise, you'd have found a more useful way to contribute.

cout<<"I dont do people's home work for them so go fist yourself!";

I mean jesus christ getting the program not to exit?
 
cout<<"I dont do people's home work for them so go fist yourself!";

I mean jesus christ getting the program not to exit?

...And clearly, you couldn't even if you wanted to, which is evident based on your criticism of classes being too complicated for a linked-list.

Why don't you stay out of matters you don't understand, so that the thread starter can receive the help he came here for? He's asking for help, not for people to tell him to read the book that he might not be able to afford or might not understand. So why don't you be a dear, and:
Code:
std::ofstream   nullout("/dev/null");
std::cout.rdbuf(nullout.rdbuf());
 
...And clearly, you couldn't even if you wanted to, which is evident based on your criticism of classes being too complicated for a linked-list.

Why don't you stay out of matters you don't understand, so that the thread starter can receive the help he came here for? He's asking for help, not for people to tell him to read the book that he might not be able to afford or might not understand.

LOL. right dude.
There are countless C++ books available for free online. The answers to his questions are also available though a simple google search.

edit://Oh and arrays and lists were taught first in my college C++ classes before polymorphism. Shit that's the why it is in the text I used also, if my memory serves me right.
 
There are countless C++ books available for free online.

...and countless numbers of those aren't any good at all.

The answers to his questions are also available though a simple google search.

...and for whatever reason, he wasn't able to find those results. Perhaps he doesn't know what he's looking for or what search terms to use? Either way, he seemed to feel the need for personalized guidance, which is why he made a thread here.

Feel free to link him to those results you found, though, so that he'll have a good picture of what to look for next time.

edit://Oh and arrays and lists were taught first in my college C++ classes before polymorphism.

Static arrays probably were taught first, if following tradition. But working with linked-lists generally makes much more sense after students have had some treatment of classes.
 
Upon reading my post I did come off like a bit of an ass.
I am not justifying it but I once got flack from the guys here too when I asked seemingly basic programming questions.
My apologies everyone.
 
Thank you guys, i will give you all a more specific question. True i couldn't find anything on Google that could help me, and any tips on search terms would be more that appreciated. I am having problems in saving the node to the memory.

I try displaying it after typing it in but the program breaks, i am 100% sure that my logic behind the code of the display function is wrong, perhaps the code too.

So my question is: How do i save the node to memory and how can i see how many nodes are in my memory.

I have managed to exit the program, i just need a couple of coffees in me and a simple while loop sorted the problem out. Any help would be appreciated, even from you Grazehell :)
 
I try displaying it after typing it in but the program breaks, i am 100% sure that my logic behind the code of the display function is wrong, perhaps the code too.

So my question is: How do i save the node to memory and how can i see how many nodes are in my memory.

In your display function, you're referencing the pointer 'head'. However, there isn't anywhere in your code where you point 'head' at anything. This means that when you go to display whatever 'head' is pointing at, there's nothing there, so the program breaks.

The problem you are having is in your code for the create_node() function. I'll give you a hint. You created a node and you have a pointer 'temp' pointing at it, but you never do anything with that node. Somehow, you need to add that node you created to the list. Furthermore, in adding that node to the list, there are two cases to consider. One where the list is empty (head == 0) and one where the list is not empty.
 
Hey! Resident teaching assistant chiming in here.

This is pretty much one of the computer science 2 labs at my university, minus a few little tidbits. I’ve had a lot of experience answering this very question (as I’m sure you can imagine), so hopefully I can help you!

Anyways, the first thing I tell CS students when they’re struggling with a new concept is to stop writing code. I know it sounds silly, but writing code is about the worst thing you can do when you don’t fully understand what needs to happen to solve a problem like this. Instead, I have students draw pictures of what they think they’re code is doing, then we’ll go through it and see what its actually doing, and then finally we’ll compare that to what actually needs to happen.

Going through your code, it looks like you understand what linked lists are and how to traverse them, but are having a tough time linking nodes together. This is where a picture would come in super handy so let’s make one! (Unfortunately drawing is kinda hard over internet, so hopefully my simple text drawings will work for you!)

Here’s what a linked list looks like when a second node is added:

[node] -> [node]

Now let’s see what your create_node function is doing:

[node] [node]

As you can see, your create_node function, while creating nodes, is not linking them together. It’s just making a pointer to a “node” struct and then forgetting about it. So that’s one thing you need to fix.

The next problem is that you don’t track where the linked list is in memory. What I mean is, that you never assign your “head” pointer to the first node of your linked list. So when you go to traverse the linked list in your display function, head points to zilch, which is why it blows up.

TL;DR:
  • Assign your head pointer to beginning of linked list (ie: first node)
  • When creating a new node, assign it to the previous node’s “next” pointer.
 
Going through your code, it looks like you understand what linked lists are and how to traverse them...
Going through his comments, I'm not convinced he wrote that code... He's already got a declaration for node *head, but he's asking how to store his node. He's traversing the list in length() (well, sort of...), but he doesn't know how to traverse it in display(). He wrote a loop for the traversal, but it didn't occur to him to put one in the menu. He hasn't managed to write check_if_node_empty(), despite the fact that he's already doing exactly that in each iteration. The whole length() implementation looks very much like he copied a working version, but substituted in the wrong variable. And length() seems a little too good when you consider that it's completely untested (since he hasn't managed to construct a list yet). It's all very suspect.

Upon reading my post I did come off like a bit of an ass.
I didn't think so. Dogs is the one who started with the personal attacks. And when someone's trying to implement a linked list without a solid grasp of iteration, or even variable assignment, "go read your fucking textbook" is absolutely the right answer (and "make it more OO" is absolutely the wrong one...).
 
Going through his comments, I'm not convinced he wrote that code... He's already got a declaration for node *head, but he's asking how to store his node. He's traversing the list in length() (well, sort of...), but he doesn't know how to traverse it in display(). He wrote a loop for the traversal, but it didn't occur to him to put one in the menu. He hasn't managed to write check_if_node_empty(), despite the fact that he's already doing exactly that in each iteration. The whole length() implementation looks very much like he copied a working version, but substituted in the wrong variable. And length() seems a little too good when you consider that it's completely untested (since he hasn't managed to construct a list yet). It's all very suspect.

Feel free to think that, as it may or may not be the case. That isn't the topic, though. He didn't come here and create a thread asking 'did I write this code'? He came here asking for help with his assignment, and as long as he's willing to ask the right questions, we should be willing to give the right answers.

I didn't think so. Dogs is the one who started with the personal attacks. And when someone's trying to implement a linked list without a solid grasp of iteration, or even variable assignment, "go read your fucking textbook" is absolutely the right answer (and "make it more OO" is absolutely the wrong one...).

...And here we have yet another person who'd rather cause problems than help the thread starter. Have you figured out yet that this is a help thread? I don't think you read that conversation either, otherwise you'd have noticed that the personal attacks started when Grazehell told me to 'go fist myself'.

'Go read your textbook' is not the right answer. Otherwise, he wouldn't be here asking for help. If he had a textbook and reading it was working for him, he wouldn't be having any problems at all. Pedagogy tells us that not everybody can learn effectively from a textbook. If you disagree with that, then there isn't anything else for you to contribute here, since somebody has already told him to go read a book. If you're aware that some people struggle with books, then feel free to demonstrate that by either contributing (feel free to use rawrski's post as a model for what that looks like) or at minimum, not harassing the thread starter.

Though I feel it could be too late at this point anyways. You guys have probably scared him away by now.
 
That isn't the topic, though. He didn't come here and create a thread asking 'did I write this code'?
This is central to the topic. Without an actual question (beyond the one printed on his assignment), the code he's written is all we've got to figure out what he understands and what he doesn't. And without that, we've got no starting point for an explanation, so whether he wrote it or not is pretty important.

'Go read your textbook' is not the right answer. Otherwise, he wouldn't be here asking for help.
You keep saying he's asking for help, but he didn't ask us a thing. He just gave us his homework. There are literally thousands of first-year CS students around the internet at the moment, posting their homework on forums and hoping that someone will do it for them, and nothing this guy said suggested he was any different. I've dealt with a lot of CS students, and I've never met one who's actually interested in learning who made it as far as linked lists without learning how to "save something to memory".

I'm happy enough to give answers, no matter how simple the question, as long as they demonstrate a little effort. But the people who sit through eight weeks of CS lectures and still come out with questions about how to use loops are never the ones who do. They're usually the ones who were spoon-fed solutions to the last seven assignments by people on internet forums, and never bothered to understand the basics themselves. The best advice you can give them is to start paying attention in class. I really do love helping people out with this stuff, but I save it for the ones who give a shit.

the personal attacks started when Grazehell told me to 'go fist myself'.
...My best guess is that you're not really much better at this than he is. Otherwise, you'd have found a more useful way to contribute.
...And honestly, I think yours was far more offensive.
 
Slightly off topic, but is Accelerated C++ still considered a good reference for C++? Any other recommendations for references?
 
The best available C++ reference is still Stroustrup's The C++ Programming Language, recently revised with information on C++11. Accelerated is still an okay learning guide, but terribly outdated. You should hit the ground running on C++11/14, not on C++98 (which is what Accelerated covers).

If you're a competent programmer in other imperative languages, you can start with The C++ Programming Language to serve as a reference guide, but consider also grabbing Stroustrup's recent A Tour of C++ if you want something that's a little lighter and read through that first. By the time you've finished both books, Meyers' Effective C++11/14 should (hopefully) be out and give you some more meat to chew on in terms of best practices with C++11/14.
 
So my question is: How do i save the node to memory and how can i see how many nodes are in my memory.

To store the information that makes up a node, you'll want to first allocate a node:

Code:
node *pNode = new node();

At that point, "pNode" points at a brand-new node. It's not initialized, so you can start poking data into it. For example, if you want that node to represent something that's got an age of 100, just set it:

Code:
pNode->age = 100;

If you want to know how many nodes you have in memory, you've got two choices. One would be to manage a counter; whenever you allocate a new node, increment that counter. When you delete a node, decrement the counter. Or, you could find the first node, walk to the next node in your list, and then increment a counter as you go along. When you reach the end of the list, the counter shows you the number of nodes you have in your list.

Slightly off topic, but is Accelerated C++ still considered a good reference for C++? Any other recommendations for references?
I don't know how that book is received, but I can tell you that thread crapping is still considered rude.
 
Back
Top