有毒!!!新人求助
//=============================下面是跟着老甲鱼小师讲课打的代码,可以编译运行,可是结果不正确============================================================#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Member
{
int member;
struct Member *next;
};
void addMember(struct Member **head,int ch)
{
struct Member *shang;
struct Member *xia;
struct Member *new;
shang = *head;
xia = NULL;
new = (struct Member*)malloc(sizeof(struct Member));
if(new == NULL)
{
printf("内存分配失败了!\n");
}
while (shang != NULL&& shang->member < ch)
{
xia = shang;
shang = shang->next;
}
new->member = ch;
new->next = shang;
if (xia == NULL)
{
*head = new;
}
else
{
xia->next = new;
new->member = ch;
}
}
void printMember(struct Member *head)
{
struct Member *member;
member = head;
while (member != NULL)
{
printf("%d ",member->member);
member = member->next;
}
putchar('\n');
}
int main(int argc, char *argv[])
{
struct Member *head;
int ch;
while (1)
{
printf("请输入一个整数(输入-1表示结束输入):");
scanf("%d",&ch);
if(ch == -1)
{
break;
}
addMember(&head,ch);
printMember(head);
}
return 0;
}
// ============================然后我把各个函数的位置换一下,28个错就离谱!!!=======================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void addMember(struct Member **head,int ch);
void printMember(struct Member *head);
int main(int argc, char *argv[])
{
struct Member
{
int member;
struct Member *next;
};
struct Member *head;
int ch;
while (1)
{
printf("请输入一个整数(输入-1表示结束输入):");
scanf("%d",&ch);
if(ch == -1)
{
break;
}
addMember(&head,ch);
printMember(head);
}
return 0;
}
void addMember(struct Member **head,int ch)
{
struct Member *shang;
struct Member *xia;
struct Member *new;
shang = *head;
xia = NULL;
new = (struct Member*)malloc(sizeof(struct Member));
if(new == NULL)
{
printf("内存分配失败了!\n");
}
while (shang != NULL&& shang->member < ch)
{
xia = shang;
shang = shang->next;
}
new->member = ch;
new->next = shang;
if (xia == NULL)
{
*head = new;
}
else
{
xia->next = new;
new->member = ch;
}
}
void printMember(struct Member *head)
{
struct Member *member;
member = head;
while (member != NULL)
{
printf("%d ",member->member);
member = member->next;
}
putchar('\n');
}
file:///C:/Users/Admin/Pictures/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202021-02-17%20114817.png//==================================================求各位大神帮看看,改改错========================================================
本帖最后由 巴巴鲁 于 2021-2-17 13:13 编辑
定义的指针在使用前一定要赋值
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Member
{
int member;
struct Member *next;
};
void addMember(struct Member **head,int ch)
{
struct Member *shang;
struct Member *xia;
struct Member *new;
shang = *head;
xia = NULL;
new = (struct Member*)malloc(sizeof(struct Member));
if(new == NULL)
{
printf("内存分配失败了!\n");
}
while (shang != NULL&& shang->member < ch)
{
xia = shang;
shang = shang->next;
}
new->member = ch;
new->next = shang;
if (xia == NULL)
{
*head = new;
}
else
{
xia->next = new;
new->member = ch;
}
}
void printMember(struct Member *head)
{
struct Member *member;
member = head;
while (member != NULL)
{
printf("%d ",member->member);
member = member->next;
}
putchar('\n');
}
int main(int argc, char *argv[])
{
struct Member *head = NULL; // 这里要给头指针赋值为空的 ,不赋值不就是野指针了吗?造成系统崩溃
int ch;
while (1)
{
printf("请输入一个整数(输入-1表示结束输入):");
scanf("%d",&ch);
if(ch == -1)
{
break;
}
addMember(&head,ch);
printMember(head);
}
return 0;
}
页:
[1]