<?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[Verkettete Listen.]]></title><description><![CDATA[<p>Hi Community!</p>
<p>Ich habe ein Problem mit einer Verketteten Liste. Die Aufgabe ist es, eine csv-Datei einzulesen und in eine verkettete Liste zu speichern. Einlesen kann das Programm, jedoch liest es komischerweise den letzten Eintrag zweimal ein. Mein zweites Problem ist für euch wahrscheinlich lächerlich aber: Wie kann ich die Elemente dann der Reihe nach ausgeben. In meinem Programm gibt es nur den letzten Eintrag aus, diesen dafür mehrmals.</p>
<p>Hier mal mein Code:</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

struct CSVData
{
	char * data;
	CSVData *next;
};

typedef struct CSVData CSV;

CSV *daten = NULL;

int main(int argc, char *argv[])
{
	CSV *node, *old;
	FILE *fptr = NULL;
	char input[255];

	fptr = fopen(&quot;db.csv&quot;, &quot;r&quot;);

	do
	{
		fgets(input, 255, fptr);
		getchar();	
		if(input)
		{
			CSV *node = new CSV;
			printf(&quot;tt.%s &quot;, input);
			node-&gt;data = input;
			node-&gt;next = daten;
			daten = node;
			printf(&quot;ttt.%s&quot;, daten-&gt;data);
		}
	}while(!feof(fptr));
	fclose(fptr);

	printf(&quot;a.%s&quot;, daten-&gt;data);
	daten = daten-&gt;next;
	printf(&quot;b.%s&quot;, daten-&gt;data);

	#ifdef WIN32
		printf(&quot;\n&quot;);
    		system(&quot;PAUSE&quot;);
	#endif

	return 0;
}
</code></pre>
<p>Die csv-Datei ist Standard, sprich in der Form &quot;string;string;string;...&quot;</p>
<p>Ich bitte um Verbesserungen wenn möglich. Danke schonmal in voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/169864/verkettete-listen</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 21:21:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/169864.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 09 Jan 2007 18:22:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Verkettete Listen. on Tue, 09 Jan 2007 18:22:52 GMT]]></title><description><![CDATA[<p>Hi Community!</p>
<p>Ich habe ein Problem mit einer Verketteten Liste. Die Aufgabe ist es, eine csv-Datei einzulesen und in eine verkettete Liste zu speichern. Einlesen kann das Programm, jedoch liest es komischerweise den letzten Eintrag zweimal ein. Mein zweites Problem ist für euch wahrscheinlich lächerlich aber: Wie kann ich die Elemente dann der Reihe nach ausgeben. In meinem Programm gibt es nur den letzten Eintrag aus, diesen dafür mehrmals.</p>
<p>Hier mal mein Code:</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

struct CSVData
{
	char * data;
	CSVData *next;
};

typedef struct CSVData CSV;

CSV *daten = NULL;

int main(int argc, char *argv[])
{
	CSV *node, *old;
	FILE *fptr = NULL;
	char input[255];

	fptr = fopen(&quot;db.csv&quot;, &quot;r&quot;);

	do
	{
		fgets(input, 255, fptr);
		getchar();	
		if(input)
		{
			CSV *node = new CSV;
			printf(&quot;tt.%s &quot;, input);
			node-&gt;data = input;
			node-&gt;next = daten;
			daten = node;
			printf(&quot;ttt.%s&quot;, daten-&gt;data);
		}
	}while(!feof(fptr));
	fclose(fptr);

	printf(&quot;a.%s&quot;, daten-&gt;data);
	daten = daten-&gt;next;
	printf(&quot;b.%s&quot;, daten-&gt;data);

	#ifdef WIN32
		printf(&quot;\n&quot;);
    		system(&quot;PAUSE&quot;);
	#endif

	return 0;
}
</code></pre>
<p>Die csv-Datei ist Standard, sprich in der Form &quot;string;string;string;...&quot;</p>
<p>Ich bitte um Verbesserungen wenn möglich. Danke schonmal in voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206493</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206493</guid><dc:creator><![CDATA[SeniiX]]></dc:creator><pubDate>Tue, 09 Jan 2007 18:22:52 GMT</pubDate></item><item><title><![CDATA[Reply to Verkettete Listen. on Tue, 09 Jan 2007 19:56:26 GMT]]></title><description><![CDATA[<p>Moin.<br />
Benutz doch zum Ausgeben der verketteten Liste auch eine while-Schleife. Oder meinst Du was anderes mit Elemente ausgeben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206557</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206557</guid><dc:creator><![CDATA[eisherz]]></dc:creator><pubDate>Tue, 09 Jan 2007 19:56:26 GMT</pubDate></item><item><title><![CDATA[Reply to Verkettete Listen. on Tue, 09 Jan 2007 19:59:11 GMT]]></title><description><![CDATA[<p>Nein wir meinen schon dasselbe. Ich habe es zuerst mit einer While-Schleife versucht, bin jedoch gescheitert und habe es daher vereinfacht, sprich ohne While-Schleife versucht. Müsste doch ebenso gehen oder nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206561</guid><dc:creator><![CDATA[SeniiX]]></dc:creator><pubDate>Tue, 09 Jan 2007 19:59:11 GMT</pubDate></item><item><title><![CDATA[Reply to Verkettete Listen. on Tue, 09 Jan 2007 21:05:02 GMT]]></title><description><![CDATA[<p>So, nun habe mein Programm auf Vordermann gebracht. Hier nochmals der Code.</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;iostream&gt;

using namespace std;

struct CSVData
{
	char *data;
	struct CSVData *next;
};

void printstruct(struct CSVData*);

struct CSVData *link0r = NULL;

int main()
{
	FILE *f = fopen(&quot;db.csv&quot;, &quot;r&quot;);

	while(!feof(f))
	{
		char *input = new char;
		fscanf(f,&quot;%s&quot;,input);
		printf(&quot;1\n&quot;);
		struct CSVData *node = new struct CSVData;
		printf(&quot;2\n&quot;);
        cout &lt;&lt; &quot;node-addr.: &quot; &lt;&lt; node &lt;&lt; endl &lt;&lt; &quot;input-addr.: &quot; &lt;&lt; &amp;input &lt;&lt; endl;
        getchar();
        strcpy(node-&gt;data,input);
        printf(&quot;3\n&quot;);
        cout &lt;&lt; node-&gt;data &lt;&lt; endl;
        delete input;
		node-&gt;next = link0r;
		printf(&quot;4\n&quot;);
		link0r = node;
		cout &lt;&lt; link0r-&gt;data &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;
	};

    cout &lt;&lt; &quot;bla&quot; &lt;&lt; link0r-&gt;data;

    //printstruct(link0r);

    #ifdef WIN32
		printf(&quot;\n&quot;);
		system(&quot;PAUSE&quot;);
	#endif
	return 0;
}

void printstruct(struct CSVData *anfang) 
{
   struct CSVData *zeiger = anfang;

   printf(&quot;||=====================================&quot;
   &quot;==================||\n&quot;);

   while(zeiger != NULL) {
      printf(&quot;%s\n&quot;,
         zeiger-&gt;data);
         printf(&quot;|-----------------------------------&quot;
                &quot;----------------------|\n&quot;);
         zeiger=zeiger-&gt;next;
   }
}
</code></pre>
<p>Jetzt hab ich nurmehr das Problem mit der Ausgabe. Die Prozedur printstruct sollte den Inhalt der verketteten Listen ausgeben, tut es aber nicht. Irgendwelche konstruktiven Vorschläge? Wenn möglich noch heute - Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206635</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206635</guid><dc:creator><![CDATA[SeniiX]]></dc:creator><pubDate>Tue, 09 Jan 2007 21:05:02 GMT</pubDate></item><item><title><![CDATA[Reply to Verkettete Listen. on Tue, 09 Jan 2007 21:12:13 GMT]]></title><description><![CDATA[<p>Ein paar Spontanitäten:<br />
1. Du benutzt inkonsistent C und C++ (scanf ist C, besser cin, new ist C++)<br />
2. Du allokierst EIN Zeichen für input, liest aber dann eine ganze Zeichenkette ein -&gt; potentieller Absturz<br />
3. Du kopierst input mit strcpy (wieder C statt C++) in ein Datum für das kein Speicher allokiert wurde (node-&gt;data ist ein nackter Zeiger, kein Array)<br />
4. &quot;Tut es aber nicht&quot; ist keine Beschreibung des Fehlers. Das Programm sollte eigentlich garnicht bis dahin kommen. Falls doch -&gt; Glück</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206641</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Tue, 09 Jan 2007 21:12:13 GMT</pubDate></item><item><title><![CDATA[Reply to Verkettete Listen. on Tue, 09 Jan 2007 21:25:34 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10648">@LordJaxom</a>:<br />
Was ist eigentlich das entsprechende C++ Equivalent zu <em>strcpy</em> (mal davon abgesehen, dass man sowieso <em>std::string</em> nehmen sollte^^).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206657</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206657</guid><dc:creator><![CDATA[CodeFinder]]></dc:creator><pubDate>Tue, 09 Jan 2007 21:25:34 GMT</pubDate></item><item><title><![CDATA[Reply to Verkettete Listen. on Tue, 09 Jan 2007 21:56:00 GMT]]></title><description><![CDATA[<blockquote>
<p>1. Du benutzt inkonsistent C und C++ (scanf ist C, besser cin, new ist C++)<br />
2. Du allokierst EIN Zeichen für input, liest aber dann eine ganze Zeichenkette ein -&gt; potentieller Absturz<br />
3. Du kopierst input mit strcpy (wieder C statt C++) in ein Datum für das kein Speicher allokiert wurde (node-&gt;data ist ein nackter Zeiger, kein Array)<br />
4. &quot;Tut es aber nicht&quot; ist keine Beschreibung des Fehlers. Das Programm sollte eigentlich garnicht bis dahin kommen. Falls doch -&gt; Glück</p>
</blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10648">@LordJaxom</a>:<br />
Zu 1. Ja, dass ich inkonsistent C und C++ benutze ist mir klar und by the way: ich benutze fscanf (nicht scanf) - weiß nicht obs da eine C++ Alternative gibt oder ob fscanf eh schon C++ ist.<br />
Zu 2. Ich habe mal versucht char *input = new char[255] aber dann stürzte das Programm ab. Speicherzugriffsfehler.</p>
<p>Könntest du mir (oder jemand anderes) bitte den Code ausbessern? Stehe wirklich an.</p>
<p>PS.: Dass ich inkonsistent C und C++ verwende liegt daran, weil ich die letzten 2 Jahre in der Schule nur C gelernt habe und heuer einen neuen Lehrer bekommen habe und daher die Mischung. Sorry ich weiß, dass es nicht gut ist so zu programmieren. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206679</guid><dc:creator><![CDATA[SeniiX]]></dc:creator><pubDate>Tue, 09 Jan 2007 21:56:00 GMT</pubDate></item><item><title><![CDATA[Reply to Verkettete Listen. on Wed, 10 Jan 2007 08:45:54 GMT]]></title><description><![CDATA[<p>Hallo SeniiX,</p>
<p>z. B. so:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;deque&gt;

// A struct to hold the words in a line.
struct Line
{
	std::deque&lt; std::string &gt; words;
};

int main()
{
	// A double ended  queue for an arbitrary
	// number of lines in the text-file
	std::deque&lt; Line* &gt; lines;

	// Open the file and and check its open
	std::ifstream ifile( &quot;test.txt&quot; );
	if ( ifile.is_open() )
	{
		// As long as we did not reach the end of
		// the file, read the file line by line
		while ( !ifile.eof() )
		{
			std::string temp;
			std::getline(ifile, temp);

			// Now, split each line in the substrings
			// separated by the character ';' and store
			// them in a new Line structure
			Line* line = new Line;
			std::basic_string&lt; char &gt;::size_type front = 0, back = 0;
			while ( back != temp.npos )
			{
				// Get the next separating character
				back = temp.find( &quot;;&quot;, front );
				// Get the substring and store it in the Line struct
				line-&gt;words.push_back( temp.substr( front, back - front ) );
				// Make the position one post the separating
				// caracter the new begin of the next search
				front = back + 1;
			}
			// store the new Line in the lines queue
			lines.push_back( line );
		}

		// The file is not needed anymore, so close it
		ifile.close();
	}
	else
		std::cout &lt;&lt; &quot;Unable to open file!!&quot; &lt;&lt; std::endl;

	// Write the input to the screen
	int count = 1;
	for ( std::deque&lt; Line* &gt;::iterator i = lines.begin(); i != lines.end(); ++i, ++count )
	{
		std::cout &lt;&lt; &quot;Words in line &quot; &lt;&lt; count &lt;&lt; ':' &lt;&lt; std::endl;
		for ( std::deque&lt; std::string &gt;::iterator j = (*i)-&gt;words.begin(); j != (*i)-&gt;words.end(); ++j )
			std::cout &lt;&lt; *j &lt;&lt; std::endl;
		std::cout &lt;&lt; std::endl;
	}

	// Clean up
	for (std::deque&lt; Line* &gt;::iterator i = lines.begin(); i != lines.end(); ++i)
		delete *i;
}
</code></pre>
<p>Das verwendet anstatt einer Liste eine &quot;double ended queue&quot; aus der STL, aber die kannst Du ja leitcht ersetzen...</p>
<p>Viele Grüße<br />
Knecht</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1206826</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1206826</guid><dc:creator><![CDATA[Knecht]]></dc:creator><pubDate>Wed, 10 Jan 2007 08:45:54 GMT</pubDate></item></channel></rss>