Re: Problems with understanding linked lists
From: Alwyn (alwyn_at_blueyonder.co.uk)
Date: 01/21/05
- Next message: Gary Labowitz: "IDE and compiler for MAC"
- Previous message: Ntl New: "Excellent C++ Tutorial?"
- In reply to: Wouter van Teijlingen: "Problems with understanding linked lists"
- Next in thread: bhanu: "Re: Problems with understanding linked lists"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 21 Jan 2005 13:58:49 +0000
On Fri, 21 Jan 2005 13:25:41 +0100, Wouter van Teijlingen wrote:
> Hi all,
>
> I still have some problems with understanding linked lists. I do
> understand the principe of pointers. I'm not a die-hard coder, still a
> learning rookie.
>
> But these linked lists drive me nuts. I have a few questions. The
> complete source of my linked list is online at the following adress:
> http://woutert.nedlinux.nl/llist02.cpp.
>
> I have tried to comment (almost) everything in the program. Maybe
> someone could take look at it, because i also have some simple questions
> in it regarding the linked list. I hope you guys can take a look at it.
I've made some corrections that make it compile correctly. It seems to
work:
#include <iostream>
using namespace std;
class Money // Definition of the class Money
{
public:
int Euro; // Important value, holds all the values.
Money *pNext; // Pointer to the class itself.
};
Money *pHead = 0; // pHead is the pointer to the first value in the list, right?
void addTail(Money *pMoney) // This function provides the possibility to add
{ // a value to the end of the list.
pMoney->pNext = 0; // This makes pMoney point to pNext and gives it the value 0.
if (pHead == 0) // if pHead is zero, then give pHead the value of pMoney.
{
pHead = pMoney; // Why must this happen? Could some make this clear for me.
return;
}
// This while-statement gives me a hard-time.
// It checks if pCurrent is pointing to pNext.
// If so, then pCurrent will be shift further in to the list with help of:
// pCurrent = pCurrent->pNext; But why must this happen?
Money *pCurrent = pHead; // Makes a pCurrent pointer to Money and gives it the value of
while (pCurrent->pNext) // pHead. Could also be pMoney, because in the above if-statement
{ // i set pHead to the value of pMoney, right?
pCurrent = pCurrent->pNext; //
}
pCurrent->pNext = pMoney; // This also gives me a hard-time. My book says:
// ''Pointing the object to pMoney'', why must this be done?
}
Money *getData() // How the heck is this possible.
// This is a function. How can i make a function without a type? I know it's a pointer
// but this is strange.
{
Money *pMoney = new Money; // Makes a new money value!
cout << "Give your money!: ";
cin >> pMoney->Euro; // This makes it happen that int Euro is getting filled!
if (pMoney->Euro == 0) // When i choice zero, the pointer must be cleaned.
{
delete pMoney;
return 0;
}
pMoney->pNext=0; // Why must pNext being re-initialised with 0 again?
return pMoney; // Gives back pMoney...
}
void displayData(Money *pMoney)
{
cout << pMoney->Euro << endl; // Display the values that the user has given.
}
void Menu() // My menu, but... it doesn't work.
{
int choice;
Money *pMoney;
cout << "1. Give a value for the end of the linked list. \n"
<< "2. Give 0 to see all the values in the list. \n";
cin >> choice;
switch (choice)
{
case 1: while (pMoney = getData()) { addTail(pMoney); }
case 2: pMoney = pHead;
while (pMoney) { displayData(pMoney); pMoney = pMoney->pNext; }
}
}
int main()
{
/*When i try to compile i get the following error:
llist02.cpp: In function `void Menu(Money *)':
llist02.cpp:84: parse error before `{'
llist02.cpp:88: `return' with a value, in function returning void
I don't know why this happen. The menu function is correct. It does not
return anything, so it can be void. The menu is int. So i return 0 back to
system. Would be logical, but it isn't When i delete the zero, it still
isn't going to compile.
*/
Menu();
return 0;
}
I'm sure the code could be improved in all sorts of ways, but at least you
now have something to play with.
Alwyn
- Next message: Gary Labowitz: "IDE and compiler for MAC"
- Previous message: Ntl New: "Excellent C++ Tutorial?"
- In reply to: Wouter van Teijlingen: "Problems with understanding linked lists"
- Next in thread: bhanu: "Re: Problems with understanding linked lists"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|