<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[anfänger - verkettete liste]]></title><description><![CDATA[<p>hallo ich wollte mal in c++ eine verkettete liste oder einen verketteten stapel halt sowas in der art programmieren, mein hauptaugenmerk sollte dabei nicht auf objektorientierung sondern speicherverwaltung liegen, ich hab jetz erst mal ein bisschen was gemacht und bevor ich mich in die katastrophe stürze hätte iczh gerne ein paar meinungen dazu:</p>
<pre><code>#ifndef ITEM_H
#define ITEM_H

class Item
{
public:
    int wert;
    Item* next;

public:
    Item();
    Item(int i);
};

Item::Item()
{
    wert= 0;
    next = NULL;
}
Item::Item(int i)
{
    wert = i;
    next = NULL;
}

#endif
</code></pre>
<pre><code>#include &quot;Item.h&quot;

class List
{
protected:
    Item* first;
    Item* last;

public:
    List();
    List(int i);
    ~List();

    void add(int i);
    void erase();
    void print();
};

List::List()
{
    first = NULL;
    last = first;
}
List::~List()
{
}
void List::add(int i)
{
    Item* x = new Item(i);
    x-&gt;next = first;
    first = x;
}

void List::erase()
{
    if(first != NULL)
    {
        Item* t = first;
        first = first-&gt;next;
        delete t;
        t = NULL;
    }
}

void List::print()
{
    Item* t = first;
    std::cout &lt;&lt; first-&gt;wert &lt;&lt; std::endl;
    while(t-&gt;next != NULL)
    {
        t = t-&gt;next;
        std::cout &lt;&lt; &quot; &quot; &lt;&lt; t-&gt;wert &lt;&lt; std::endl;
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/233909/anfänger-verkettete-liste</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 14:53:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/233909.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 10 Feb 2009 19:36:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to anfänger - verkettete liste on Tue, 10 Feb 2009 19:36:05 GMT]]></title><description><![CDATA[<p>hallo ich wollte mal in c++ eine verkettete liste oder einen verketteten stapel halt sowas in der art programmieren, mein hauptaugenmerk sollte dabei nicht auf objektorientierung sondern speicherverwaltung liegen, ich hab jetz erst mal ein bisschen was gemacht und bevor ich mich in die katastrophe stürze hätte iczh gerne ein paar meinungen dazu:</p>
<pre><code>#ifndef ITEM_H
#define ITEM_H

class Item
{
public:
    int wert;
    Item* next;

public:
    Item();
    Item(int i);
};

Item::Item()
{
    wert= 0;
    next = NULL;
}
Item::Item(int i)
{
    wert = i;
    next = NULL;
}

#endif
</code></pre>
<pre><code>#include &quot;Item.h&quot;

class List
{
protected:
    Item* first;
    Item* last;

public:
    List();
    List(int i);
    ~List();

    void add(int i);
    void erase();
    void print();
};

List::List()
{
    first = NULL;
    last = first;
}
List::~List()
{
}
void List::add(int i)
{
    Item* x = new Item(i);
    x-&gt;next = first;
    first = x;
}

void List::erase()
{
    if(first != NULL)
    {
        Item* t = first;
        first = first-&gt;next;
        delete t;
        t = NULL;
    }
}

void List::print()
{
    Item* t = first;
    std::cout &lt;&lt; first-&gt;wert &lt;&lt; std::endl;
    while(t-&gt;next != NULL)
    {
        t = t-&gt;next;
        std::cout &lt;&lt; &quot; &quot; &lt;&lt; t-&gt;wert &lt;&lt; std::endl;
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1661365</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1661365</guid><dc:creator><![CDATA[shisha]]></dc:creator><pubDate>Tue, 10 Feb 2009 19:36:05 GMT</pubDate></item><item><title><![CDATA[Reply to anfänger - verkettete liste on Wed, 11 Feb 2009 06:26:45 GMT]]></title><description><![CDATA[<p>Also an sich habe ich nichts auszusetzen. Bis auf eine kleine Sachen eventuell. Wenn du dann soweit bist, kannst du die Liste auch als Template machen, dann ist es auch möglich anstatt nur ints, wie jetzt, auch andere Sachen reinzupacken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1661497</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1661497</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Wed, 11 Feb 2009 06:26:45 GMT</pubDate></item><item><title><![CDATA[Reply to anfänger - verkettete liste on Wed, 11 Feb 2009 09:24:14 GMT]]></title><description><![CDATA[<p>Das</p>
<pre><code>t = NULL;
</code></pre>
<p>in erase() ist völlig überflüssig, da t dort sowieso &quot;out of scope&quot; geht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1661560</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1661560</guid><dc:creator><![CDATA[jencas]]></dc:creator><pubDate>Wed, 11 Feb 2009 09:24:14 GMT</pubDate></item><item><title><![CDATA[Reply to anfänger - verkettete liste on Wed, 11 Feb 2009 09:34:05 GMT]]></title><description><![CDATA[<p>shisha schrieb:</p>
<blockquote>
<p>...und bevor ich mich in die katastrophe stürze hätte iczh gerne ein paar meinungen dazu:</p>
</blockquote>
<p>Bitte benutz im Forum bei der Codedarstellung im Falle von C oder C++ Code die cpp-Tags (statt den code-Tags).</p>
<p>Jetzt aber erst einmal Anmerkungen:<br />
1. Trenne Implementierung/Deklaration (Header/Source)<br />
2. Verwende Initialisierungslisten bei Konstruktoren<br />
3. Datenkapselung ist sinnvoll (public Attribute würde ich meiden).<br />
4. Wenn Item fix zu List gehört, und nur dort verwendet werden kann, eignen sich innere, private Klassen eher.<br />
5. Ich würde lieber private statt protected verwenden...<br />
6. Sobald du mit Zeigern und dynamischer Speicherverwaltung hantierst, musst du in der Regel auch Kopierkonstruktor und Zuweisungsoperator implementieren, oder explizit verbieten. Zudem hat dein Destruktor auch aufzuräumen, wenn noch Elemente vorhanden sind.</p>
<p>Um dir mal einen Ansatz zu geben:</p>
<pre><code class="language-cpp">#ifndef LIST_H
#define LIST_H

// List.h
class List
{
  private:
    // Listenelement, da nur intern in Verwendung ist die öffentliche
    // Schnittstelle okay...
    class Item
    {
      public:
        int wert;
        Item* next;
        Item(int wert = 0); // Ich reduziere mal auf einen Konstruktor...
    }

    Item* first; // Einfach verkette Listen brauchen nur einen Zeiger

public:
    // Neben den Konstruktor/Destruktor sind in deinem Fall noch
    // Kopierkonstruktor und Zuweisungsoperator nötig!
    // Alternativ: private Deklarieren, ohne Implementierung!
    List();
    List(
       List const &amp; liste);
    List&amp; operator=(
       List const &amp; liste);
    ~List();

    void add(int i);
    void erase();
    void print();
};

#endif
</code></pre>
<pre><code class="language-cpp">// List.cpp
#include &quot;List.h&quot;

// *** Listenelement ***
List::Item::Item(  // Nun eine Verschachtelung tiefer (Da Item in List)
     int wert)
:    wert(wert),   // Initialisierungsliste
     next(NULL)
{
}

// *** Liste ***

List()
:   first(NULL)
{
}

List::~List()
{
    // Du musst hier auch aufräumen!
    clear();
}

List&amp; List::operator=(
    List const &amp; liste)
{
    // Man kann Zuweisungsoperatoren besser bauen (z.B. mittels swap)
    // Ich habe hier nur eine typische Anfängerimplementierung gewählt...
    if(this == &amp;liste)
        return *this;

    // Alle Elemente aus Original übertragen, ich nutze hierbei die add-Funktion
    clear();
    for(Item * element=liste.first; element!=null; element=element-&gt;next)
        add(element-&gt;wert);

    return *this;
}

List::List(
    List const &amp; liste)
:   first(NULL)
{
    // Alle Elemente aus Original übertragen
    for(Item * element=liste.first; element!=null; element=element-&gt;next)
        add(element-&gt;wert);
}

void List::clear()
{
    while(first) {
        Item * next = first-&gt;next;
        delete first;
        first = next;
    }
}

//... usw.
</code></pre>
<p>Code ist ungetestet. Aber noch weitere Hinweise:</p>
<p>7. Ausgaben und Datenhaltung würde ich grundsätzlich trennen, um den Code eher wiederverwerten zu können.<br />
8. Merk dir grundsätzlich: Zu jedem new gehört immer genau ein delete!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1661565</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1661565</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Wed, 11 Feb 2009 09:34:05 GMT</pubDate></item></channel></rss>