Hello I try to make one linked list to double linked list but i have some probems with this
.Can somebody help me?
This is yhe the linked list:

typedef struct ns 
{
	int data;
	struct ns *next;
} node;
node *list_add(node **p, int i) 
{
	node *n = (node *)malloc(sizeof(node));
	if (n == NULL)
		return NULL;
	n->next = *p; 
	*p = n;
	n->data = i;
	return *p;
}
void list_remove(node **p) /* remove head */
{ 
	if (*p != NULL)
	{
		node *n = *p;
		*p = (*p)->next;
		free(n);
	}
}
node **list_search(node **n, int i)
{
	while (*n != NULL) 
	{
		if ((*n)->data == i) 
			return n;
		n = &(*n)->next;
	}
	return NULL;
}

Ok the structure have to be

typedef struct ns 
{
        struct ns *next;
	int data;
	struct ns *next;
} node;

With add fuction I think something like that,but i'm not sure

node *list_add(node **p, int i) 
{
	node *n = (node *)malloc(sizeof(node));
	if (n == NULL)
		return NULL;
	n->next = *p; 
	*p = n;
        n->prev = NULL;
	n->data = i;
	return *p;
}

for the remove and search function i have no idea
Thanks in advance

Recommended Answers

All 2 Replies

Two things as starting points:

1) In your second code fragment, you have two members named next. You can't do that.

2) This is really a C program, not a C++ program, so you would be better off asking your question in the C forum.

Two things as starting points:

1) In your second code fragment, you have two members named next. You can't do that.

2) This is really a C program, not a C++ program, so you would be better off asking your question in the C forum.

Indeed. If this is supposed to be C++, then use classes with proper constructor(s), initializers, destructors, and use new/delete instead of malloc/free to allocate data. So, is this supposed to be a C program, or a C++ program?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.