Umwandlung von C Code



  • 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.
    Ist nen relitiv langer Codeabschnitt, aber jede Hilfe wuerde mir da wirklich weiterhelfen. Besten dank schonmal im Voraus!
    Hier der Codeabschnitt:

    #include "list.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    plist *create_empty_list()
    {
    	plist *l = (plist*) calloc(1,sizeof(plist));
    	l->first = NULL;
    	l->last = NULL;
    	return l;
    }
    
    int empty(plist *l)
    {
    	if (l->first == NULL)
    		return 1;
    	return 0;
    }
    
    void print_l(plist *l)
    {
    	if (empty(l)) {
    		printf("empty list\n");
    		return;
    	}
    	printf("--->\n");
    	pnode* current = l->first;
    	while (current) {
    		print_p(current->elem);
    		current = current->next;
    	}
    	printf("<---\n");
    
    }
    
    void add(plist *l, person *p)
    {
    	pnode *node = (pnode*) calloc(1, sizeof(pnode));
    	node->prev = NULL;
    	node->next = NULL;
    	node->elem = p;
    	if (empty(l)) {
    		l->first = node;
    		l->last = node;
    	} else {
    		l->last->next = node;
    		node->prev = l->last;
    		l->last = node;
    	}
    
    }
    
    void swap(plist *list, pnode *n1, pnode *n2)
    {
    #ifdef __SWAP_CONTENT__
             /* person *tmp = n1->elem;
    	 n1->elem=n2->elem;
    	 n2->elem=tmp;
    	 */
    #else
    	//nothing to do
    	if (n1 == n2)
    		return;
    	//if n2 is the first element, we treat n2 as n1
    	if (list->first == n2) {
    		pnode *tmp = n1;
    		n1 = n2;
    		n2 = tmp;
    	}
    	//if n1 is the last element, we treat n1 as n2
    	if (list->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->next;
    	pnode *tmp_n1_prev = n1->prev;
    	pnode *tmp_n2_prev = n2->prev;
    	pnode *tmp_n2_next = n2->next;
    
    	n1->next = n2->next;
    	n2->prev = n1->prev;
    	if (list->first == n1) {
    		list->first = n2;
    
    		if (list->last == n2) {
    			n1->prev = n2;
    			n2->next = n1;
    			list->last = n1;
    			return;
    		}
    	} else {
    		tmp_n1_prev->next = n2;
    	}
    
    	if (list->last == n2) {
    		list->last = n1;
    	} else {
    		tmp_n2_next->prev = n1;
    	}
    
    	//special case
    	if (tmp_n1_next == n2) {
    		n1->prev = n2;
    		n2->next = n1;
    	} else {
    		n1->prev = tmp_n2_prev;
    		n2->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->first;
    		while (current->next) {
    			if (cmp(current->elem, current->next->elem) > 0) {
    				swap(list, current, current->next);
    
    				changed = 1;
    			}
    			if (current->next)
    				current = current->next;
    
    		}
    
    	}
    	return;
    }
    
    person *search_name(plist *list, char* firstname, char*lastname)
    {
    	pnode *current = list->first;
    	while (current) {
    
    		if (strcmp(current->elem->firstname, firstname) == 0 && strcmp(
    				current->elem->lastname, lastname) == 0) {
    			return current->elem;
    		}
    		current = current->next;
    	}
    	return NULL;
    
    }
    
    person *search_date(plist *list, mdate birthday)
    {
    	pnode *current = list->first;
    	while (current) {
    
    		if (datecmp(&(current->elem->birthday), &birthday) == 0) {
    			return current->elem;
    		}
    		current = current->next;
    	}
    	return NULL;
    }
    
    pnode *search_person(plist *list, person *p)
    {
    	pnode *current = list->first;
    	while (current) {
    
    		if (current->elem == p) {
    			return current;
    		}
    		current = current->next;
    	}
    	return NULL;
    }
    
    void remove_node(plist *list, pnode *node)
    {
    	if (node == NULL || list == NULL)
    		return;
    
    	if (list->first == node && list->last == node) {
    		//just one element
    		list->first = NULL;
    		list->last = NULL;
    		free(node);
    		return;
    	}
    
    	if (list->first == node) {
    		list->first = node->next;
    		list->first->prev = NULL;
    		node->next = NULL;
    		free(node);
    		return;
    	}
    
    	if (list->last == node) {
    		list->last = node->prev;
    		list->last->next = NULL;
    		node->prev = NULL;
    		free(node);
    		return;
    	}
    	//ordinary case
    	node->prev->next = node->next;
    	node->next->prev = node->prev;
    	node->next = NULL;
    	node->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->first;
    	while(current){
    		pnode *tmp=current;
    		current=current->next;
    		//delete also the content
    		delete_p(tmp->elem);
    		free(tmp);
    	}
    	list->first=NULL;
    	list->last=NULL;
    }
    


  • #include <list>
    


  • 314159265358979 schrieb:

    #include <list>
    

    Wollte ich auch gerade schreiben.

    P.S. was ist denn das für eine monströse swap-Funktion? Bist du sicher, dass das nicht einfacher gegangen wäre?



  • haha, ja danke schonmal 😉

    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.


Anmelden zum Antworten