c -链表循环



我真的很喜欢这个代码的实现,但我不希望它被硬编码,所以谁能帮我做一个循环for list = new_node(1);等等

#include <stdlib.h>
#include <stdio.h>
typedef struct node {
int number;
struct node *next;
} node;
node *new_node(int number)
{
node *n = malloc(sizeof(*n));
if (n == NULL)
{
perror("new_node");
exit(1);
}
n->number = number;
n->next = NULL;
return n;
}
int main(void) {
node *list;
list = new_node(1);
list->next = new_node(2);
list->next->next = new_node(3);
list->next->next->next = new_node(4);
for (node *tmp = list; tmp != NULL; tmp = tmp->next)
{
printf("%in", tmp->number);
}
while (list != NULL) {
node *tmp = list->next;
free(list);
list = tmp;
}
return 0;
}

当您尝试将新节点添加到列表尾部时,那么在这种情况下,定义一个双边单链表将是合理的。

例如

typedef struct node 
{
int number;
struct node *next;
} node;
typedef struct list
{
struct node *head;
struct node *tail;
} list;

在main中你可以声明一个像

这样的列表
list lst = { .head = NULL, .tail = NULL };

将节点追加到列表尾部的函数看起来像

node * new_node( int number )
{
node *n = malloc( sizeof( *n ) );
if ( n != NULL )
{
n->number = number;
n->next = NULL;
}
return n;
}
int push_back( list *lst, int number )
{
node *n = new_node( number );
int success = n != NULL;
if ( success )
{
if ( lst->tail == NULL )
{
lst->head = n;
}
else
{
lst->tail->next = n;
}
lst->tail = n;
}
return success;
} 

在main中函数的调用方式是

push_back( &lst, 1 );

if ( !push_back( &lst, 1 ) )
{
puts( "Error: there is no enough memory." );
}

这是一个示范程序。

#include <stdio.h>
#include <stdlib.h>
typedef struct node 
{
int number;
struct node *next;
} node;
typedef struct list
{
struct node *head;
struct node *tail;
} list;
node * new_node( int number )
{
node *n = malloc( sizeof( *n ) );
if ( n != NULL )
{
n->number = number;
n->next = NULL;
}
return n;
}
int push_back( list *lst, int number )
{
node *n = new_node( number );
int success = n != NULL;
if ( success )
{
if ( lst->tail == NULL )
{
lst->head = n;
}
else
{
lst->tail->next = n;
}
lst->tail = n;
}
return success;
} 
void display( const list *lst )
{
for ( const node *current = lst->head; current != NULL; current = current->next )
{
printf( "%d -> ", current->number );
}
puts( "null" );
}
int main( void )
{
list lst = { .head = NULL, .tail = NULL };
enum { N = 10 };
for ( int i = 0; i < N; i++ )
{
push_back( &lst, i );
}
display( &lst );
}

程序输出为

0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

你需要自己写一些其他的函数,比如释放为列表分配的所有内存的函数。

这应该能回答你的问题

#include <stdlib.h>
#include <stdio.h>
typedef struct node {
int number;
struct node *next;
} node;
node *new_node(node **h, int number)
{
node *n = malloc(sizeof(*n));

if (n == NULL)
return (NULL);
n->number = number;
n->next = NULL;

if (*h == NULL)
{
*h = n;
return (*h);
}

n->next = *h;
*h = n;

return (*h);

}
void print_node(node *h)
{
printf("Head => ");
while (h != NULL)
{
printf("%d => ", h->number);
h = h->next;
}

printf("Tailn");
}
int main(void) 
{
node *head = NULL;
new_node(&head, 8);
new_node(&head, 9);

print_node(head);
return 0;
}

Head => 9 => 8 => Tail

如果您有一个值序列,那么在链表中获取它们的一个好方法是在前面加上节点,同时以反向顺序迭代该序列。

所以主代码的第一部分可以变成:

node *list = NULL;
for (int i = 4; i > 0; i--) {
node *newNode = new_node(i);
newNode->next = list;
list = newNode;
}

如果列表的值不是顺序的,而是来自某个数组,那么它将像这样工作:

// For demo, we initialise the array here, but it could be initialised differently
int numbers[] = {1, 2, 3, 4};
node *list = NULL;
int size = sizeof(numbers) / sizeof(int);
for (int i = size - 1; i >= 0; i--) {
node *newNode = new_node(numbers[i]);
newNode->next = list;
list = newNode;
}

另一个例子:如果数据源是用户输入,那么可以这样做:

node *list = NULL;
printf("Enter integers for the list, and terminate with -1:n");
while (1) {
int data;
scanf(" %d", &data);
if (data == -1) break;
node *newNode = new_node(data);
newNode->next = list;
list = newNode;
}

最新更新