Dodanie listy do listy, lista list

0

Witam.
Mam do napisania program w c, ktory tworzy dwie listy (wezłów i grafów), a nastepnie dodaje nowy wezeł do grafu. Moim zadaniem jest napisanie funkcji "
node *add_node (graph *g, size_t id) " która dodaje nowy węzeł do grafu g o podanym id i aktualizuje jego rozmiar. Należy sprawdzić czy w grafie nie znajduje się już węzeł o takim id. Nie rozumiem, jak mam "połączyć" te dwie listy, to znaczy jak dopisac jedną do drugiej i zaktualizowac jej rozmiar
Mój kod:

#include <stdlib.h>
struct node
{
	size_t id;
	size_t degree;
	struct node *next_node;
};
typedef struct node node;
struct graph
{
	size_t n_nodes;
	size_t n_links;
	node *next_node;
};
typedef struct graph graph;
node *new_node (size_t id)
{
	node *nowy_wezel=malloc(sizeof(node));
	nowy_wezel->id=id;
	nowy_wezel->degree=0;
	nowy_wezel->next_node=NULL;
	return nowy_wezel;
}
graph *new_graph(size_t n_nodes)
{
	graph *nowy_graf=malloc(sizeof(graph));
	nowy_graf->n_nodes=n_nodes;
	nowy_graf->next_node=NULL;
	node *wezly=malloc(sizeof(n_nodes)); 
	return nowy_graf;
}```
0
#include <stdio.h>
#include <stdlib.h>

typedef struct link link;
typedef struct node node;
typedef struct graph graph;

struct link
{
	double weight;
    node *to;
    link *next;
};

typedef struct node
{
    size_t id,degree;
    node *next;
    link *first;
};

struct graph
{
    size_t node_count,link_count;
    node *first;
};

link *make_link(node *to,double weight,link *next)
{
	link *ret=(link*)malloc(sizeof(link));
	ret->weight=weight;
	ret->to=to;
	ret->next=next;
	return ret;
}

node *make_node(size_t id,node *next)
{
	node *ret=(node*)malloc(sizeof(node));
	ret->id=id;
	ret->degree=0;
	ret->next=next;
	ret->first=NULL;
	return ret;
}

node *find_or_make_node(graph *g,size_t id)
{
	node **n=&(g->first);
	while((*n)&&((*n)->id<id)) n=&((*n)->next);
	if(((*n)==NULL)||((*n)->id>id))
	{
		(*n)=make_node(id,*n);
		++g->node_count;
	}
	return (*n);
}

void connect_nodes(graph *g,size_t id_from,size_t id_to,double weight,int bidirectional)
{
	node *from=find_or_make_node(g,id_from), *to=find_or_make_node(g,id_to);
	++g->link_count;
	from->first=make_link(to,weight,from->first);
	++from->degree;
	if(!bidirectional) return;
	to->first=make_link(from,weight,to->first);
	++to->degree;
}

void show_graph(graph *g)
{
	for(node *n=g->first;n;n=n->next)
	{
		printf("%d =>",n->id);
		for(link *k=n->first;k;k=k->next)
		{
			printf(" %d(%.1f)",k->to->id,k->weight);
		}
		printf("\n");
	}
}

void clear_graph(graph *g)
{
	for(node *nx=NULL,*n=g->first;n;n=nx)
	{
		nx=n->next;
		for(link *kx=NULL,*k=n->first;k;k=kx)
		{
			kx=k->next;
			free(k);
		}
		free(n);
	}
	g->node_count=g->link_count=0;
	g->first=0;
}

int main()
{
	graph g={0};
	connect_nodes(&g,1,3,9,1);
	connect_nodes(&g,3,5,9,1);
	connect_nodes(&g,5,2,9,1);
	connect_nodes(&g,2,4,9,1);
	connect_nodes(&g,4,1,9,1);
	show_graph(&g);
	clear_graph(&g);
	return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1