 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Georg Madler Guest
|
Posted: Fri Dec 17, 2004 4:09 pm Post subject: Verkettete Liste - wie das erste Element speichern? |
|
|
Hallo! Folgendes (Anfänger-)Problem:
Ich habe eine einfach verkettete Liste aus z.B. 5 Elementen. Nun brauche ich
ja das erste Element in der Liste um die Elemente einzeln durchzugehen, ich
versuche also einen Pointer auf das erste Element zu bekommen. (Siehe
Sourcecode unten). Sobald ich aber noch ein Element hinzufüge ändert sich
auch mein "Anfang"-Pointer und zeigt auf das neue Element. Wie speichere ich
also in so einem Fall einen Zeiger auf das allererste Element in der Liste
ab? Danke!
class Point {
public:
Point New();
void SetCounter(int count);
private:
Point *next;
int counter;
};
Point Point::New() {
this->next = new Point;
return *this->next;
}
void Point::SetCounter(int count) {
this->counter = count;
}
int main()
{
Point *point = new Point;
Point *Anfang;
//Speichere Pointer auf erstes Element ->Problem!
Anfang = point;
for(int i=1;i<=5;i++) {
point->SetCounter(0);
*point = point->New();
}
return EXIT_SUCCESS;
}
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Gerd Orfey Guest
|
Posted: Fri Dec 17, 2004 11:29 pm Post subject: Re: Verkettete Liste - wie das erste Element speichern? |
|
|
Georg Madler wrote:
| Quote: | Hallo! Folgendes (Anfänger-)Problem:
Ich habe eine einfach verkettete Liste aus z.B. 5 Elementen. Nun
brauche ich ja das erste Element in der Liste um die Elemente einzeln
durchzugehen, ich versuche also einen Pointer auf das erste Element
zu bekommen. (Siehe Sourcecode unten). Sobald ich aber noch ein
Element hinzufüge ändert sich auch mein "Anfang"-Pointer und zeigt
auf das neue Element. Wie speichere ich also in so einem Fall einen
Zeiger auf das allererste Element in der Liste ab? Danke!
class Point {
public:
Point New(); |
Hier würde ich einen Zeiger auf Point zurückgegeben.
Das macht alles ein bisschen einfacher.
| Quote: | Point *New();
void SetCounter(int count);
private:
Point *next;
int counter;
};
Point Point::New() {
this->next = new Point;
return *this->next;
}
|
Bei mir wäre das:
Point *Point::New(){
next = new Point();
return next;
}
Ist doch einfacher, oder?
Deine Version ist aber, soweit ich das sehen kann trotzdem
nicht falsch.
Beide Versionen legen ein neues Objekt von Typ Point auf
dem Heap an und speichern seine Adresse im Zeiger 'next' des
jeweils aktuellen Objekts.
| Quote: | void Point::SetCounter(int count) {
this->counter = count;
}
int main()
{
Point *point = new Point;
|
Objekt der Klasse Point auf dem Heap angelegt und
Zeiger point darauf gerichtet.
Speicher für einen Zeiger auf ein Objekt vom Typ Pointer
bereitgestellt. Dem Zeiger ist keinem Objekt zugewiesen, d.h. er
ist unitialisiert und zeigt irgendwohin.
| Quote: |
//Speichere Pointer auf erstes Element ->Problem!
Anfang = point;
|
Der Zeiger "Anfang" zeigt jetzt auf dasselbe Objekt, auf das
point zeigt.
| Quote: | for(int i=1;i<=5;i++) {
|
C++ - Programmierer würden schreiben
for(int i=0;i<5;i++){
Indexe sind in C++ üblicherweise 0-basiert. Wenn man das
einheitlich macht, braucht man sich nur zu merken, daß die
Anzahl der Element immer um eins höher ist als der größte Index.
Wenn man es unterschiedlich macht, kommt man mit Sicherheit
irgendwann mal durcheinander und muss dann Glück haben, damit
es schon beim debuggen kracht und nicht erst beim Kunden.
| Quote: | point->SetCounter(0);
*point = point->New();
|
Das würde bei mir heissen:
point = point->New();
Beim erstenmal zeigt point jetzt auf Anfang->next.
Beim zweitenmal auf Anfang->next->next.
usw.
Da du "Anfang" nicht veränderst zeigt "Anfang" weiter auf das
erste Element. point dagegen zeigt immer auf ein neues Element
Also kannst du "Anfang" auch nach der Schleife noch verwenden
um z.B. durch deine verkettete Liste zu laufen.
Wieso meintest du, daß etwas falsch sei?
| Quote: | }
return EXIT_SUCCESS;
}
|
Gruß
Gerd
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Sat Dec 18, 2004 11:14 am Post subject: Re: Verkettete Liste - wie das erste Element speichern? |
|
|
Georg Madler wrote:
| Quote: | Hallo! Folgendes (Anfänger-)Problem:
Ich habe eine einfach verkettete Liste aus z.B. 5 Elementen. Nun brauche
ich ja das erste Element in der Liste um die Elemente einzeln
durchzugehen, ich versuche also einen Pointer auf das erste Element zu
bekommen. (Siehe Sourcecode unten). Sobald ich aber noch ein Element
hinzufüge ändert sich auch mein "Anfang"-Pointer und zeigt auf das neue
Element. Wie speichere ich also in so einem Fall einen Zeiger auf das
allererste Element in der Liste ab? Danke!
class Point {
public:
Point New();
void SetCounter(int count);
private:
Point *next;
int counter;
};
Point Point::New() {
this->next = new Point;
return *this->next;
}
|
Das stimmt nicht. Du erzeugst einen neuen Punkt mit new, dann gibst du eine
Kopie dieses Punktes aus der Funktion zurück. Das ist bestimmt nicht das,
was du wolltest. Du mußt einen Zeiger oder eine Referenz zurückgeben, also
z.B:
(das "this->" kannst du übrigens weglassen)
Point& Point::New() {
next = new Point;
return *next;
}
oder:
Point* Point::New() {
next = new Point;
return next;
}
Im zweiten Fall kannst du auch unten den Adressoperator weglassen.
| Quote: | void Point::SetCounter(int count) {
this->counter = count;
}
int main()
{
Point *point = new Point;
Point *Anfang;
//Speichere Pointer auf erstes Element ->Problem!
Anfang = point;
for(int i=1;i<=5;i++) {
point->SetCounter(0);
*point = point->New();
|
Hier erzeugst du mit point->New() einen neuen Punkt und weist ihn dann dem
alten Punkt zu. Damit ist zeigt point immer noch auf den alten Punkt, aber
dieser ist zu einer Kopie des neuen Punkts geworden. Die Adresse des neuen
Punkts hast du damit auch noch gleich verloren.
Was du statt dessen willst, ist die Adresse dem Zeiger selbst zuweisen,
also:
point = &point->New();
Oder, falls du oben New so änderst, daß es einen Zeiger zurückgibt:
point = point->New();
| Quote: | }
return EXIT_SUCCESS;
}
|
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Georg Madler Guest
|
Posted: Tue Dec 21, 2004 10:13 am Post subject: Re: Verkettete Liste - wie das erste Element speichern? |
|
|
Zuerst mal Dank an euch beide für die Hilfe, mittlerweile habe ich
meine Probleme mit euren Korrekturen bzw. Verbesserungsvorschlägen
lösen können.
On Sat, 18 Dec 2004 00:29:11 +0100, "Gerd Orfey" <gerda.orfey (AT) gmx (DOT) net>
wrote:
| Quote: | Da du "Anfang" nicht veränderst zeigt "Anfang" weiter auf das
erste Element. point dagegen zeigt immer auf ein neues Element
Also kannst du "Anfang" auch nach der Schleife noch verwenden
um z.B. durch deine verkettete Liste zu laufen.
Wieso meintest du, daß etwas falsch sei?
|
Bei mir war eben das Problem, daß "Anfang" nach der Schleife nicht
mehr auf das erste Element zeigte, sondern auf das zuletzt in der
Schleife erzeugte...
Danke nochmal!
Georg
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Georg Madler Guest
|
Posted: Sat Dec 25, 2004 6:22 pm Post subject: Re: Verkettete Liste - wie das erste Element speichern? |
|
|
up tight to hold it all together.
Place breast side up in a large metal roasting pan.
Bake in 325° oven covered for 2 hours.
Remove cover, stick a cooking thermometer deep into one of the
baby?s buttocks and cook uncovered till thermometer reads 190°,
about another hour.
Pro-Choice Po-Boy
Soft-shelled crabs serve just as well in this classic southern delicacy.
The sandwich originated in New Orleans, where an abundance of abortion clinics
thrive and hot French bread is always available.
2 cleaned fetuses, head on
2 eggs
1 tablespoon yellow mustard
1 cup seasoned flour
oil enough for deep frying
1 loaf French bread
Lettuce
tomatoes
mayonnaise, etc.
Marinate the fetuses in the egg-mustard mixture.
Dredge thoroughly in flour.
Fry at 375° until crispy golden brown.
Remove and place on paper towels.
Holiday Youngster
One can easily adapt this recipe to ham, though as presented,
it violates no religious taboos against swine.
1 large toddler or small child, cleaned and de-headed
Kentucky Bourbon Sauce (see index)
1 large can pineapple slices
Whole cloves
Place him (or ham) or her in a large glass baking dish, buttocks up.
Tie with butcher string around and across so that he looks like
he?s crawling.
Glaze, then arrange pineapples and secure with cloves.
Bake uncovered in 350° oven till thermometer reaches 160°.
Cajun Babies
Just like crabs or crawfish, babies are boiled alive!
You don?t need silverware, the hot spicy meat comes off in your hands.
6 live babies
1 lb. smoked sausage
4 lemons
whole garlic
2 lb. new potatoes
4 ears corn
1 box salt
crab boil
Bring 3 gallons of water to a boil.
Add sausage, salt, crab boil, lemons and garlic.
Drop potatoes in, boil for 4 minutes.
Corn is added next, boil an additional 11 minutes.
Put the live babies into the boiling water and cover.
|
|
| Back to top |
|
 |
Georg Madler Guest
|
Posted: Sat Dec 25, 2004 10:38 pm Post subject: Re: Verkettete Liste - wie das erste Element speichern? |
|
|
¼ cup vegetable oil
2 large onions
bell pepper
celery
garlic
½ cup red wine
3 Irish potatoes
2 large carrots
This is a simple classic stew that makes natural gravy,
thus it does not have to be thickened.
Brown the meat quickly in very hot oil, remove and set aside.
Brown the onions, celery, pepper and garlic.
De-glaze with wine, return meat to the pan and season well.
Stew on low fire adding small amounts of water and
seasoning as necessary.
After at least half an hour, add the carrots and potatoes,
and simmer till root vegetables break with a fork.
Cook a fresh pot of long grained white rice.
Pre-mie Pot Pie
When working with prematurely delivered newborns (or chicken) use sherry;
red wine with beef (buy steak or roast, do not pre-boil).
Pie crust (see index)
Whole fresh pre-mie; eviscerated, head, hands and feet removed
Onions, bell pepper, celery
½ cup wine
Root vegetables of choice (turnips, carrots, potatoes, etc) cubed
Make a crust from scratch - or go shamefully to the frozen food section
of your favorite grocery and select 2 high quality pie crusts (you
will need one for the top also).
Boil the prepared delicacy until the meat starts to come off the bones.
Remove, de-bone and cube; continue to reduce the broth.
Brown the onions, peppers and celery.
Add the meat then season, continue browning.
De-glaze with sherry, add the reduced broth.
Finally, put in the root vegetables and simmer for 15 minutes.
Allow to cool slightly.
Place the pie pan in 375 degree oven for a few minutes so bottom crust is not soggy,
reduce oven to 325.
Fill the pie with stew, place top crust and with a fork, seal the crusts together
then poke holes in top.
Return to oven and bake for 30 minutes, or until pie crust is golden brown.
Sudden Infant Death Soup
SIDS: delicious in winter, comparable to old fashioned Beef and Vegetable Soup.
Its free, you can sell the
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|