反转链接列表 [英] reverse a link list
问题描述
弗兰斯,
可以U PLZ。建议我如何反转一个链接列表(没有递归)???
这里是我的代码(不完整):
#include< ; stdio.h>
#include< stdlib.h>
#define MAX_NODES 8
typedef struct node {
int data;
struct node * next;
} NODE;
void reverse_list(NODE ** head)
{
/ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /这里。 * /
/ * ___________________________ * /
返回;
}
int main()
{
NODE * head,* tmp;
int i;
head = malloc(sizeof(NODE));
如果(!head)返回1;
head-> data = 1;
head-> next = NULL;
tmp = head;
for(i = 0; i< MAX_NODES; i ++)
{
tmp-> next = malloc(sizeof(NODE));
if(!tmp-> next)break;
tmp-> next-> data = i + 2;
tmp = tmp-> next;
}
reverse_list(& head);
tmp = head;
而(tmp)
{
printf("%d \ n",tmp-> data);
>
tmp = tmp-> next;
}
返回0;
}
-Neo
Hi Frns,
Could U plz. suggest me how can i reverse a link list (without recursion)???
here is my code (incomplete):
#include<stdio.h>
#include<stdlib.h>
#define MAX_NODES 8
typedef struct node {
int data;
struct node *next;
} NODE;
void reverse_list(NODE **head)
{
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* TODO: Reverse the List here. */
/* ___________________________ */
return;
}
int main()
{
NODE *head, *tmp;
int i;
head = malloc(sizeof(NODE));
if(!head) return 1;
head->data = 1;
head->next = NULL;
tmp = head;
for(i=0; i < MAX_NODES; i++)
{
tmp->next = malloc(sizeof(NODE));
if(!tmp->next) break;
tmp->next->data = i + 2;
tmp = tmp->next;
}
reverse_list(&head);
tmp = head;
while(tmp)
{
printf("%d\n", tmp->data);
tmp = tmp->next;
}
return 0;
}
-Neo
推荐答案
Neo写道:
Neo wrote:
可以U PLZ。建议我如何反转一个链接列表(没有
Could U plz. suggest me how can i reverse a link list (without
递归)???
假设原始列表在old_list中并且反转列表将是
在new_list中创建
WHILE old_list不为空
从old_list的头部获取项目
将项目添加到new_list的尾部
-
Nick Keighley
recursion)???
assuming the original list is in old_list and the reversed list will be
created in new_list
WHILE old_list not empty
get item from head of old_list
add item to tail of new_list
--
Nick Keighley
" Nick Keighley" < NI ****************** @ hotmail.com>在消息中写道
news:11 ********************** @ z14g2000cwz.googlegr oups.com ...
"Nick Keighley" <ni******************@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Neo写道:
Neo wrote:
可以U PLZ。建议我如何反转一个链接列表(没有
Could U plz. suggest me how can i reverse a link list (without
递归)???
假设原始列表在old_list中并且反向列表将被创建
在new_list
WHILE old_list不为空
从old_list的头部获取项目
将项目添加到new_list的尾部
recursion)???
assuming the original list is in old_list and the reversed list will be
created in new_list
WHILE old_list not empty
get item from head of old_list
add item to tail of new_list
嗯......
Woundn不会给你完全相同的清单吗?
我建议:
WHILE old_list不空
从old_list的头部获取项目
将项目添加到new_list的头部
所以old_list的最后一项是new_list的负责人。
Ummmm...
Woundn''t that give you exactly the same list?
I''d suggest:
WHILE old_list not empty
get item from head of old_list
add item to head of new_list
So the last item of old_list is the head of new_list.
2005年1月4日04:05:17 -0800,Nick Keighley
< ni ****************** @ hotmail.com>写道:
On 4 Jan 2005 04:05:17 -0800, Nick Keighley
<ni******************@hotmail.com> wrote:
Neo写道:
Neo wrote:
可以U PLZ。建议我如何反转一个链接列表(没有
Could U plz. suggest me how can i reverse a link list (without
递归)???
假设原始列表在old_list中并且反向列表将被创建
在new_list
WHILE old_list不为空
从old_list的头部获取项目
将项目添加到new_list的尾部
recursion)???
assuming the original list is in old_list and the reversed list will be
created in new_list
WHILE old_list not empty
get item from head of old_list
add item to tail of new_list
我认为你的意思是将项目添加到new_list的头部:
旧列表项目新列表
删除
12345 1 1
2345 2 21
345 3 321
45 4 4321
5 5 54321
(假设列表的''head''在左边。)
Chris C
I think you mean "add item to head of new_list":
old list item new list
removed
12345 1 1
2345 2 21
345 3 321
45 4 4321
5 5 54321
(Assuming that the ''head'' of a list is on the left.)
Chris C
>
这篇关于反转链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!