<?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[Klasse für Listenzugriff]]></title><description><![CDATA[<p>Hallo muss aus diesen implementierten Funktionen eine Klasse für einen Listenzugriff schreiben! Kann mir jemand dafür einen Ansatz geben?<br />
Ich kenne natürlich den Aufbau von Klassen, aber weiß nicht so recht wie ich das mit den structs und so machen soll!</p>
<p>Gruß Jens</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

// Ein Listenelement
struct IntListElem {
  IntListElem *next;    // Zeiger auf nächstes Element
  int value;            // Daten zu diesem Element
};

// Eine Liste
struct IntList {
  int count;            // Anzahl Elemente in der Liste
  IntListElem *first;   // Zeiger auf erstes Element der Liste
};

// Initialisiere eine Listenstruktur. Die Liste wird als Referenz,
// nicht als Wert übergeben, es kann also der Inhalt geändert werden
void empty_list (IntList &amp;l) {
  l.first = 0;  // 0 ist keine gueltige Adresse: Liste ist leer
  l.count = 0;
}

// erstes Vorkommen von x in Liste suchen
IntListElem* find_first_x (IntList &amp;l, int x) {
  for (IntListElem* p=l.first; p!=0; p=p-&gt;next)
    if (p-&gt;value==x) return p;

  return 0;
}

// erstes Vorkommen von x in Liste suchen
IntListElem* find_first_before_x (IntList &amp;l, int x) {
  for (IntListElem* p=l.first; p!=0; p=p-&gt;next)
    if ((p-&gt;next != 0) &amp;&amp; (p-&gt;next-&gt;value == x))
      return p;

  return 0;
}

// Fuege ein Element nach einem gegebenem ein
void insert_in_list_at_pos (IntList &amp;list,
                IntListElem * where,
                IntListElem * ins)
{
  if (where == 0)       // fuege am Anfang ein
  {
    ins-&gt;next = list.first;
    list.first = ins;
    list.count = list.count + 1;
  }
  else              // fuege nach where ein
  {
    ins-&gt;next = where-&gt;next;
    where-&gt;next = ins;
    list.count = list.count + 1;
  }
}

// Entferne ein Element nach einem gegebenem
// Liefere das entfernte Element zurueck
IntListElem *remove_from_list_after_pos (IntList &amp;list, IntListElem * where)
{
  IntListElem *p;       // das entfernte Element

  // where==0 dann entferne erstes Element
  if (where == 0)
  {
    p = list.first;
    if (p != 0)
    {
      list.first = p-&gt;next;
      list.count = list.count - 1;
    }
    return p;
  }

  // entferne Element nach where
  p = where-&gt;next;
  if (p != 0)
  {
    where-&gt;next = p-&gt;next;
    list.count = list.count - 1;
  };

  return p;
}

// eine Zahl am Anfang der Liste einfügen
void insert_in_list (IntList &amp;list, int x) {
  IntListElem* elem = new IntListElem;

  elem-&gt;value = x;

  insert_in_list_at_pos(list, 0, elem);
};

// erstes Vorkommen der Zahl x in der Liste löschen
IntListElem* remove_from_list(IntList &amp;list, int x) {
  // suche das Element vor x
  IntListElem* p = find_first_before_x(list, x);

  // wurde es gefunden?
  if (p != 0)
  {
    // normal das Element entfernen
    p = remove_from_list_after_pos(list, p);
  }
  else
  {
    // kein Element vor x kann auch heissen, dass x das erste Element ist
    if ((list.first != 0) &amp;&amp; (list.first-&gt;value == x))
      p = remove_from_list_after_pos(list, 0);
  };

  // Speicher für Element wieder freigeben
  delete p;

  return 0;
};

// Zugriff auf Anzahl der Elemente
int list_num (IntList&amp; l)
{
  return l.count;
};

// Liste ausgeben
void print_list (IntList&amp; l)
{
  cout &lt;&lt; list_num(l) &lt;&lt; &quot; Elemente: &quot;;

  cout &lt;&lt; &quot;(&quot;;
  for (IntListElem* p=l.first; p!=0; p=p-&gt;next)
    cout &lt;&lt; &quot; &quot; &lt;&lt; p-&gt;value;
  cout &lt;&lt; &quot; )&quot; &lt;&lt; endl;
}

// Listendaten entfernen
void discard_list (IntList&amp; l)
{
  IntListElem* p = l.first;

  while (p != 0)
  {
    IntListElem* next = p-&gt;next;
    delete p;
    p = next;
  };
};

int main () {
  IntList list;

  // Liste initialisieren (leeren)
  empty_list(list);

  for (int i=20; i &gt; 0; i = i - 1)
    insert_in_list(list, i);

  print_list(list);

  for (int i=0; i &lt; 15; i = i + 1)
    remove_from_list(list, i);

  print_list(list);

  discard_list(list);

  system(&quot;Pause&quot;);
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/129617/klasse-für-listenzugriff</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 06:58:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/129617.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 14 Dec 2005 23:10:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Klasse für Listenzugriff on Wed, 14 Dec 2005 23:10:43 GMT]]></title><description><![CDATA[<p>Hallo muss aus diesen implementierten Funktionen eine Klasse für einen Listenzugriff schreiben! Kann mir jemand dafür einen Ansatz geben?<br />
Ich kenne natürlich den Aufbau von Klassen, aber weiß nicht so recht wie ich das mit den structs und so machen soll!</p>
<p>Gruß Jens</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

// Ein Listenelement
struct IntListElem {
  IntListElem *next;    // Zeiger auf nächstes Element
  int value;            // Daten zu diesem Element
};

// Eine Liste
struct IntList {
  int count;            // Anzahl Elemente in der Liste
  IntListElem *first;   // Zeiger auf erstes Element der Liste
};

// Initialisiere eine Listenstruktur. Die Liste wird als Referenz,
// nicht als Wert übergeben, es kann also der Inhalt geändert werden
void empty_list (IntList &amp;l) {
  l.first = 0;  // 0 ist keine gueltige Adresse: Liste ist leer
  l.count = 0;
}

// erstes Vorkommen von x in Liste suchen
IntListElem* find_first_x (IntList &amp;l, int x) {
  for (IntListElem* p=l.first; p!=0; p=p-&gt;next)
    if (p-&gt;value==x) return p;

  return 0;
}

// erstes Vorkommen von x in Liste suchen
IntListElem* find_first_before_x (IntList &amp;l, int x) {
  for (IntListElem* p=l.first; p!=0; p=p-&gt;next)
    if ((p-&gt;next != 0) &amp;&amp; (p-&gt;next-&gt;value == x))
      return p;

  return 0;
}

// Fuege ein Element nach einem gegebenem ein
void insert_in_list_at_pos (IntList &amp;list,
                IntListElem * where,
                IntListElem * ins)
{
  if (where == 0)       // fuege am Anfang ein
  {
    ins-&gt;next = list.first;
    list.first = ins;
    list.count = list.count + 1;
  }
  else              // fuege nach where ein
  {
    ins-&gt;next = where-&gt;next;
    where-&gt;next = ins;
    list.count = list.count + 1;
  }
}

// Entferne ein Element nach einem gegebenem
// Liefere das entfernte Element zurueck
IntListElem *remove_from_list_after_pos (IntList &amp;list, IntListElem * where)
{
  IntListElem *p;       // das entfernte Element

  // where==0 dann entferne erstes Element
  if (where == 0)
  {
    p = list.first;
    if (p != 0)
    {
      list.first = p-&gt;next;
      list.count = list.count - 1;
    }
    return p;
  }

  // entferne Element nach where
  p = where-&gt;next;
  if (p != 0)
  {
    where-&gt;next = p-&gt;next;
    list.count = list.count - 1;
  };

  return p;
}

// eine Zahl am Anfang der Liste einfügen
void insert_in_list (IntList &amp;list, int x) {
  IntListElem* elem = new IntListElem;

  elem-&gt;value = x;

  insert_in_list_at_pos(list, 0, elem);
};

// erstes Vorkommen der Zahl x in der Liste löschen
IntListElem* remove_from_list(IntList &amp;list, int x) {
  // suche das Element vor x
  IntListElem* p = find_first_before_x(list, x);

  // wurde es gefunden?
  if (p != 0)
  {
    // normal das Element entfernen
    p = remove_from_list_after_pos(list, p);
  }
  else
  {
    // kein Element vor x kann auch heissen, dass x das erste Element ist
    if ((list.first != 0) &amp;&amp; (list.first-&gt;value == x))
      p = remove_from_list_after_pos(list, 0);
  };

  // Speicher für Element wieder freigeben
  delete p;

  return 0;
};

// Zugriff auf Anzahl der Elemente
int list_num (IntList&amp; l)
{
  return l.count;
};

// Liste ausgeben
void print_list (IntList&amp; l)
{
  cout &lt;&lt; list_num(l) &lt;&lt; &quot; Elemente: &quot;;

  cout &lt;&lt; &quot;(&quot;;
  for (IntListElem* p=l.first; p!=0; p=p-&gt;next)
    cout &lt;&lt; &quot; &quot; &lt;&lt; p-&gt;value;
  cout &lt;&lt; &quot; )&quot; &lt;&lt; endl;
}

// Listendaten entfernen
void discard_list (IntList&amp; l)
{
  IntListElem* p = l.first;

  while (p != 0)
  {
    IntListElem* next = p-&gt;next;
    delete p;
    p = next;
  };
};

int main () {
  IntList list;

  // Liste initialisieren (leeren)
  empty_list(list);

  for (int i=20; i &gt; 0; i = i - 1)
    insert_in_list(list, i);

  print_list(list);

  for (int i=0; i &lt; 15; i = i + 1)
    remove_from_list(list, i);

  print_list(list);

  discard_list(list);

  system(&quot;Pause&quot;);
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/942483</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/942483</guid><dc:creator><![CDATA[jensr]]></dc:creator><pubDate>Wed, 14 Dec 2005 23:10:43 GMT</pubDate></item><item><title><![CDATA[Reply to Klasse für Listenzugriff on Thu, 15 Dec 2005 07:14:33 GMT]]></title><description><![CDATA[<p>Schau dir doch mal den Header &lt;list&gt; an, der dürfte dir eventuell ein paar Ansätze geben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /> (Ansonsten würde ich aus den ganzen Funktionen lieber Methoden der Klasse 'InTList' machen, anstatt sie global zu definieren)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/942542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/942542</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 15 Dec 2005 07:14:33 GMT</pubDate></item></channel></rss>