Hello all,
I'd like, very much, to understand linked-lists.I've read many texts on the subject and tried many source codes but none of them seemed to fit my needs (one from daniweb is pretty close but it's in C) so I, as desperate as I can be, beg you for mercy.
I'd like to tell you not to suggest me to look for STL lists or to search the forums or even to buy a book on data structures.I've seen it all and I've got it all, belive me.I wouldn't come here if I'd have another option, but I don't.
I have a C++ exam next month and I understand pretty much everything except these damned linked-lists.Many slepless nights and even more headaches so I've decided to try this forum (since they all refused/were too lazy or there was some unnatural power that forbid them to help me on other forums).
I'm going for C++ singly-linked lists with structures and basic operations (add beginning, add end, display, remove node, remove list).I'll provide some code and I'd like you to continue with adding code and explanation (I'd really appreciate a good and detailed explanation because I need to understand them, not just to get the source code) so some other time maybe others can learn from this thread, that's why I called it Ultimate singly-linked lists.
STARTING
We need a structure with data ofcourse.
struct node
{
char name[10]; //this is a char array where we will store the name user will input
int age; //we are going to store the age here
node* p_next; //this is a pointer to the next node
}
Now we make a main and menu functions.I separated these two because when a user will finish his adding/deleting he might want to return to the menu for some more options.
int main()
{
menu(); //we call the menu function
return 0;
}
void menu()
{
node* p_beginning = new node; //we create a new pointer which will point to the beginning of the list, that is first node
p_beginning = NULL; // and we set it to point to nothing at the momment
int input;
do
{
cout << "MENU" << endl << endl;
cout << "[1] Add to the beginning" << endl;
cout << "[2] Add to the end" << endl;
cout << "[3] Display" << endl;
cout << "[4] Delete a node" << endl;
cout << "[0] Exit" << endl << endl;
cout << "Option: ";
cin >> input;
switch (input)
{
case 1:
{
beginning(); //we call a function where we will add a node to the beginning
}
case 2:
{
}
case 3:
{
}
case 4:
{
}
case 0:
{
return 0;
}
}
} while (input!= 0);
}
That's everything I understand.Now questions:
1.In the menu we created a new pointer called p_beginning which will (in the future) point to the first node or to the beginning of the node but now we set it to point to nothing -> why is that?
2.Is that everything or should we add some more lines to the code (stick to the stuff related to linked-lists)?
OK, can anyone now make similar stuff like I did.
Now our next job is to start adding the nodes, right? At the beginning.Now we will make a function "void beginning()" and I'm sure we'll have to add some parameters to the menu call function beginning().
So...
void beginning()
{
}
It doesen't matter if 15 people reply to this post about the beginning() function, more the better.It's good to see how one would make this by himself.So, can you help?