链表逆置和函数调用的问题,initlist函数可以运行,但inverselist函数没有输出值,...
# include <stdio.h># include <stdlib.h>
typedef struct LNODElist
{
int data;
struct LNODElist *next;
}st;
void inverselist(st **);
void initlist(st **);
int main(void) //主函数
{
st *l;
initlist(&l);
inverselist(&l);
printf("%d", l ->next ->data);
return 0;
}
void initlist(st **b) //初始化链表
{
int i,j;
st *m,*a,*k;
k = (st*)malloc(sizeof(st*));
*b = &k;
k ->next = NULL;
a = k;
for(i = 0, j = 1; i < 10; i++,j++)
{
m = (st*)malloc(sizeof(st*));
m -> data =j;
printf("%d\n", m ->data);
a -> next = m;
a = a -> next;
}
a = NULL;
}
void inverselist(st **b) //逆置链表并输出
{
st *p, *succ,*k;
succ = NULL;
k = &*b;
p = k ->next;
k -> next = NULL;
while(p != NULL);
{
succ = p ->next;
p ->next = k ->next;
k ->next = p;
p = succ;
}
} 求大神帮忙看看!!!感谢!!! 浅笑灬丨 发表于 2018-9-8 14:18
求大神帮忙看看!!!感谢!!!
[新手教程] 发提问帖前请先读我 >> https://fishc.com.cn/thread-61051-1-1.html
你的问题是? claws0n 发表于 2018-9-8 14:26
[新手教程] 发提问帖前请先读我 >> https://fishc.com.cn/thread-61051-1-1.html
你的问题是?
调用函数inverselist函数使链表l逆置,使主函数输出链表l头结点->next ->data的值,谢谢!!1 浅笑灬丨 发表于 2018-9-8 14:31
调用函数inverselist函数使链表l逆置,使主函数输出链表l头结点->next ->data的值,谢谢!!1
void inverselist(st **b) //逆置链表并输出
{
st *p, *succ,*k;
succ = NULL;
k = &*b;
p = k ->next;
k -> next = succ; //k -> next = NULL;//也可以啦
while(p != NULL);
{
succ = p ->next;
p ->next = k;
k = p;
p = succ;
}
//没有返回?
*b = k;
}
没上机,自己调一下吧~ 还是不行,大佬你看看我主函数调用initlist函数后是不是没有保存数据啊,对函数调用这块还不是很懂 浅笑灬丨 发表于 2018-9-8 16:22
还是不行,大佬你看看我主函数调用initlist函数后是不是没有保存数据啊,对函数调用这块还不是很懂
改了一些变量,比较好理解,大体架构没有变#include <stdio.h>
#include <stdlib.h>
typedef struct LNODElist
{
int data;
struct LNODElist *next;
}St;
void reverslist(St **list);
void initlist(St **list);
void print(St *list);
int main(void)
{
St *l = NULL;
initlist(&l);
print(l);
reverslist(&l);
print(l);
return 0;
}
void initlist(St **list)
{
int i;
St *pNew;
for(i = 10; i > 0; i--)
{
pNew = (St *)malloc(sizeof(St));
pNew->data =i;
printf("%d ", pNew->data);
pNew->next = *list;
*list = pNew;
}
printf("\n");
}
void reverslist(St **list)
{
St *prev = NULL, *curr = *list, *succ;
while( curr )
{
succ = curr->next;
curr->next = prev;
prev = curr;
curr = succ;
}
*list = prev;
}
void print(St *list)
{
St *temp = list;
while( temp )
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
页:
[1]