<?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[Umwandlung von C Code]]></title><description><![CDATA[<p>Ich habe folgendes Problem: Ich will meinen geschriebenen C Code in C++ umwandeln, kenn aber leider niemanden der beide Sprachen gut genug beherrscht und selber habe ich mir die Sprache (noch) nicht angeeignet.<br />
Ist nen relitiv langer Codeabschnitt, aber jede Hilfe wuerde mir da wirklich weiterhelfen. Besten dank schonmal im Voraus!<br />
Hier der Codeabschnitt:</p>
<pre><code class="language-cpp">#include &quot;list.h&quot;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

plist *create_empty_list()
{
	plist *l = (plist*) calloc(1,sizeof(plist));
	l-&gt;first = NULL;
	l-&gt;last = NULL;
	return l;
}

int empty(plist *l)
{
	if (l-&gt;first == NULL)
		return 1;
	return 0;
}

void print_l(plist *l)
{
	if (empty(l)) {
		printf(&quot;empty list\n&quot;);
		return;
	}
	printf(&quot;---&gt;\n&quot;);
	pnode* current = l-&gt;first;
	while (current) {
		print_p(current-&gt;elem);
		current = current-&gt;next;
	}
	printf(&quot;&lt;---\n&quot;);

}

void add(plist *l, person *p)
{
	pnode *node = (pnode*) calloc(1, sizeof(pnode));
	node-&gt;prev = NULL;
	node-&gt;next = NULL;
	node-&gt;elem = p;
	if (empty(l)) {
		l-&gt;first = node;
		l-&gt;last = node;
	} else {
		l-&gt;last-&gt;next = node;
		node-&gt;prev = l-&gt;last;
		l-&gt;last = node;
	}

}

void swap(plist *list, pnode *n1, pnode *n2)
{
#ifdef __SWAP_CONTENT__
         /* person *tmp = n1-&gt;elem;
	 n1-&gt;elem=n2-&gt;elem;
	 n2-&gt;elem=tmp;
	 */
#else
	//nothing to do
	if (n1 == n2)
		return;
	//if n2 is the first element, we treat n2 as n1
	if (list-&gt;first == n2) {
		pnode *tmp = n1;
		n1 = n2;
		n2 = tmp;
	}
	//if n1 is the last element, we treat n1 as n2
	if (list-&gt;last == n1) {
		pnode *tmp = n1;
		n1 = n2;
		n2 = tmp;
	}
	//to assume, that n2 is not the first and n1 is not the last element

	pnode *tmp_n1_next = n1-&gt;next;
	pnode *tmp_n1_prev = n1-&gt;prev;
	pnode *tmp_n2_prev = n2-&gt;prev;
	pnode *tmp_n2_next = n2-&gt;next;

	n1-&gt;next = n2-&gt;next;
	n2-&gt;prev = n1-&gt;prev;
	if (list-&gt;first == n1) {
		list-&gt;first = n2;

		if (list-&gt;last == n2) {
			n1-&gt;prev = n2;
			n2-&gt;next = n1;
			list-&gt;last = n1;
			return;
		}
	} else {
		tmp_n1_prev-&gt;next = n2;
	}

	if (list-&gt;last == n2) {
		list-&gt;last = n1;
	} else {
		tmp_n2_next-&gt;prev = n1;
	}

	//special case
	if (tmp_n1_next == n2) {
		n1-&gt;prev = n2;
		n2-&gt;next = n1;
	} else {
		n1-&gt;prev = tmp_n2_prev;
		n2-&gt;next = tmp_n1_next;
	}
#endif
}

void bubblesort(plist *list, int(*cmp)(person*, person*))
{
	if (empty(list))
		return;
	unsigned changed = 1;
	while (changed) {
		changed = 0;
		pnode *current = list-&gt;first;
		while (current-&gt;next) {
			if (cmp(current-&gt;elem, current-&gt;next-&gt;elem) &gt; 0) {
				swap(list, current, current-&gt;next);

				changed = 1;
			}
			if (current-&gt;next)
				current = current-&gt;next;

		}

	}
	return;
}

person *search_name(plist *list, char* firstname, char*lastname)
{
	pnode *current = list-&gt;first;
	while (current) {

		if (strcmp(current-&gt;elem-&gt;firstname, firstname) == 0 &amp;&amp; strcmp(
				current-&gt;elem-&gt;lastname, lastname) == 0) {
			return current-&gt;elem;
		}
		current = current-&gt;next;
	}
	return NULL;

}

person *search_date(plist *list, mdate birthday)
{
	pnode *current = list-&gt;first;
	while (current) {

		if (datecmp(&amp;(current-&gt;elem-&gt;birthday), &amp;birthday) == 0) {
			return current-&gt;elem;
		}
		current = current-&gt;next;
	}
	return NULL;
}

pnode *search_person(plist *list, person *p)
{
	pnode *current = list-&gt;first;
	while (current) {

		if (current-&gt;elem == p) {
			return current;
		}
		current = current-&gt;next;
	}
	return NULL;
}

void remove_node(plist *list, pnode *node)
{
	if (node == NULL || list == NULL)
		return;

	if (list-&gt;first == node &amp;&amp; list-&gt;last == node) {
		//just one element
		list-&gt;first = NULL;
		list-&gt;last = NULL;
		free(node);
		return;
	}

	if (list-&gt;first == node) {
		list-&gt;first = node-&gt;next;
		list-&gt;first-&gt;prev = NULL;
		node-&gt;next = NULL;
		free(node);
		return;
	}

	if (list-&gt;last == node) {
		list-&gt;last = node-&gt;prev;
		list-&gt;last-&gt;next = NULL;
		node-&gt;prev = NULL;
		free(node);
		return;
	}
	//ordinary case
	node-&gt;prev-&gt;next = node-&gt;next;
	node-&gt;next-&gt;prev = node-&gt;prev;
	node-&gt;next = NULL;
	node-&gt;prev = NULL;

	//reallocating the memory of node, elem remains
	free(node);
}

void remove_person(plist* l, person* p)
{
	remove_node(l, search_person(l, p));
}

void clear_l(plist *list)
{
	pnode *current = list-&gt;first;
	while(current){
		pnode *tmp=current;
		current=current-&gt;next;
		//delete also the content
		delete_p(tmp-&gt;elem);
		free(tmp);
	}
	list-&gt;first=NULL;
	list-&gt;last=NULL;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/288635/umwandlung-von-c-code</link><generator>RSS for Node</generator><lastBuildDate>Wed, 19 Aug 2026 22:00:21 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/288635.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 20 Jun 2011 11:03:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Umwandlung von C Code on Mon, 20 Jun 2011 11:03:08 GMT]]></title><description><![CDATA[<p>Ich habe folgendes Problem: Ich will meinen geschriebenen C Code in C++ umwandeln, kenn aber leider niemanden der beide Sprachen gut genug beherrscht und selber habe ich mir die Sprache (noch) nicht angeeignet.<br />
Ist nen relitiv langer Codeabschnitt, aber jede Hilfe wuerde mir da wirklich weiterhelfen. Besten dank schonmal im Voraus!<br />
Hier der Codeabschnitt:</p>
<pre><code class="language-cpp">#include &quot;list.h&quot;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

plist *create_empty_list()
{
	plist *l = (plist*) calloc(1,sizeof(plist));
	l-&gt;first = NULL;
	l-&gt;last = NULL;
	return l;
}

int empty(plist *l)
{
	if (l-&gt;first == NULL)
		return 1;
	return 0;
}

void print_l(plist *l)
{
	if (empty(l)) {
		printf(&quot;empty list\n&quot;);
		return;
	}
	printf(&quot;---&gt;\n&quot;);
	pnode* current = l-&gt;first;
	while (current) {
		print_p(current-&gt;elem);
		current = current-&gt;next;
	}
	printf(&quot;&lt;---\n&quot;);

}

void add(plist *l, person *p)
{
	pnode *node = (pnode*) calloc(1, sizeof(pnode));
	node-&gt;prev = NULL;
	node-&gt;next = NULL;
	node-&gt;elem = p;
	if (empty(l)) {
		l-&gt;first = node;
		l-&gt;last = node;
	} else {
		l-&gt;last-&gt;next = node;
		node-&gt;prev = l-&gt;last;
		l-&gt;last = node;
	}

}

void swap(plist *list, pnode *n1, pnode *n2)
{
#ifdef __SWAP_CONTENT__
         /* person *tmp = n1-&gt;elem;
	 n1-&gt;elem=n2-&gt;elem;
	 n2-&gt;elem=tmp;
	 */
#else
	//nothing to do
	if (n1 == n2)
		return;
	//if n2 is the first element, we treat n2 as n1
	if (list-&gt;first == n2) {
		pnode *tmp = n1;
		n1 = n2;
		n2 = tmp;
	}
	//if n1 is the last element, we treat n1 as n2
	if (list-&gt;last == n1) {
		pnode *tmp = n1;
		n1 = n2;
		n2 = tmp;
	}
	//to assume, that n2 is not the first and n1 is not the last element

	pnode *tmp_n1_next = n1-&gt;next;
	pnode *tmp_n1_prev = n1-&gt;prev;
	pnode *tmp_n2_prev = n2-&gt;prev;
	pnode *tmp_n2_next = n2-&gt;next;

	n1-&gt;next = n2-&gt;next;
	n2-&gt;prev = n1-&gt;prev;
	if (list-&gt;first == n1) {
		list-&gt;first = n2;

		if (list-&gt;last == n2) {
			n1-&gt;prev = n2;
			n2-&gt;next = n1;
			list-&gt;last = n1;
			return;
		}
	} else {
		tmp_n1_prev-&gt;next = n2;
	}

	if (list-&gt;last == n2) {
		list-&gt;last = n1;
	} else {
		tmp_n2_next-&gt;prev = n1;
	}

	//special case
	if (tmp_n1_next == n2) {
		n1-&gt;prev = n2;
		n2-&gt;next = n1;
	} else {
		n1-&gt;prev = tmp_n2_prev;
		n2-&gt;next = tmp_n1_next;
	}
#endif
}

void bubblesort(plist *list, int(*cmp)(person*, person*))
{
	if (empty(list))
		return;
	unsigned changed = 1;
	while (changed) {
		changed = 0;
		pnode *current = list-&gt;first;
		while (current-&gt;next) {
			if (cmp(current-&gt;elem, current-&gt;next-&gt;elem) &gt; 0) {
				swap(list, current, current-&gt;next);

				changed = 1;
			}
			if (current-&gt;next)
				current = current-&gt;next;

		}

	}
	return;
}

person *search_name(plist *list, char* firstname, char*lastname)
{
	pnode *current = list-&gt;first;
	while (current) {

		if (strcmp(current-&gt;elem-&gt;firstname, firstname) == 0 &amp;&amp; strcmp(
				current-&gt;elem-&gt;lastname, lastname) == 0) {
			return current-&gt;elem;
		}
		current = current-&gt;next;
	}
	return NULL;

}

person *search_date(plist *list, mdate birthday)
{
	pnode *current = list-&gt;first;
	while (current) {

		if (datecmp(&amp;(current-&gt;elem-&gt;birthday), &amp;birthday) == 0) {
			return current-&gt;elem;
		}
		current = current-&gt;next;
	}
	return NULL;
}

pnode *search_person(plist *list, person *p)
{
	pnode *current = list-&gt;first;
	while (current) {

		if (current-&gt;elem == p) {
			return current;
		}
		current = current-&gt;next;
	}
	return NULL;
}

void remove_node(plist *list, pnode *node)
{
	if (node == NULL || list == NULL)
		return;

	if (list-&gt;first == node &amp;&amp; list-&gt;last == node) {
		//just one element
		list-&gt;first = NULL;
		list-&gt;last = NULL;
		free(node);
		return;
	}

	if (list-&gt;first == node) {
		list-&gt;first = node-&gt;next;
		list-&gt;first-&gt;prev = NULL;
		node-&gt;next = NULL;
		free(node);
		return;
	}

	if (list-&gt;last == node) {
		list-&gt;last = node-&gt;prev;
		list-&gt;last-&gt;next = NULL;
		node-&gt;prev = NULL;
		free(node);
		return;
	}
	//ordinary case
	node-&gt;prev-&gt;next = node-&gt;next;
	node-&gt;next-&gt;prev = node-&gt;prev;
	node-&gt;next = NULL;
	node-&gt;prev = NULL;

	//reallocating the memory of node, elem remains
	free(node);
}

void remove_person(plist* l, person* p)
{
	remove_node(l, search_person(l, p));
}

void clear_l(plist *list)
{
	pnode *current = list-&gt;first;
	while(current){
		pnode *tmp=current;
		current=current-&gt;next;
		//delete also the content
		delete_p(tmp-&gt;elem);
		free(tmp);
	}
	list-&gt;first=NULL;
	list-&gt;last=NULL;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2081014</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081014</guid><dc:creator><![CDATA[FabianLoe]]></dc:creator><pubDate>Mon, 20 Jun 2011 11:03:08 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von C Code on Mon, 20 Jun 2011 11:07:07 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;list&gt;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2081016</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081016</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 20 Jun 2011 11:07:07 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von C Code on Mon, 20 Jun 2011 11:07:58 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#include &lt;list&gt;
</code></pre>
</blockquote>
<p>Wollte ich auch gerade schreiben.</p>
<p>P.S. was ist denn das für eine monströse swap-Funktion? Bist du sicher, dass das nicht einfacher gegangen wäre?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081017</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081017</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Mon, 20 Jun 2011 11:07:58 GMT</pubDate></item><item><title><![CDATA[Reply to Umwandlung von C Code on Mon, 20 Jun 2011 11:17:28 GMT]]></title><description><![CDATA[<p>haha, ja danke schonmal <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="😉"
    /></p>
<p>War damals ne relativ komplexe Uni Aufgabe, dass ich so eine Tauschfunktion gebraucht hab. Zumindestens konnt/ kann ich das heut auch nicht besser. Hat so die ein oder andere Schweissperle gekostet, wie das immer so ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081024</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081024</guid><dc:creator><![CDATA[FabianLoe]]></dc:creator><pubDate>Mon, 20 Jun 2011 11:17:28 GMT</pubDate></item></channel></rss>