马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
struct dy_lklist{
int num;
struct dy_lklist *next;
};
typedef struct dy_lklist NODE;
NODE *creat_linklist();
void print(NODE *head);
NODE *delete(NODE *head);
NODE *insert(NODE *head, NODE *stu);
void free_linklist(NODE *head);
void main(){
NODE *plist, stu, stu2;
plist = creat_linklist();
print(plist);
plist = delete(plist);
print(plist);
plist = insert(plist, &stu);
print(plist);
plist = insert(plist, &stu2);
print(plist);
printf("test2");
free_linklist(plist);
print(plist);
printf("test3");
}
NODE *creat_linklist(){
NODE *p1, *p2, *head = NULL;
int n = 0;
p1 = (NODE *)malloc(sizeof(NODE));
if(p1 == NULL){
printf("no enough memory!");
}
p2 = p1;
printf("please input the number of student : ");
scanf("%d", &(p1->num));
while(p1->num != 0){
n += 1;
if (n == 1){
head = p1;
}
else{
p2->next = p1;
}
p2 = p1;
p1 = (NODE *)malloc(sizeof(NODE));
if(p1 == NULL){
printf("no enough memory!");
}
printf("please input the number of student : ");
scanf("%d", &(p1->num));
}
p2->next = NULL;
return head;
}
void print(NODE *head){
NODE *p;
p = head;
if(p == NULL){
printf("the linklist is null!");
}
if (NULL != head){
do{
printf("%d\n", p->num);
p = p->next;
} while (p != NULL);
}
}
NODE *delete(NODE *head){
int num;
NODE *p, *p1;
p1 = p = head;
printf("please input a number of student which you want to delete: ");
scanf("%d", &num);
if (head != NULL){
do{
if(p->num == num){
break;
}
p1 = p;
p = p->next;
}while(p != NULL);
}
else{
printf("the linklist is empty!\n");
goto END;
}
if (p == NULL){
printf("you want to delete the number is not exist!\n");
goto END;
}
else if (p == head){
head = p->next;
}
else{
p1->next = p->next;
}
free(p);
END:
return head;
}
NODE *insert(NODE *head, NODE *stu){
NODE *p0, *p1, *p2;
int num;
printf("please input the number which you want to insert in the linklist : ");
scanf("%d", &num);
p0 = stu;
p0->num = num;
p1 = p2 = head;
if (head == NULL){
head = p0;
p0->next = NULL;
goto END;
}
while(p1->next != NULL && p2->num < p0->num){
p1 = p2;
p2 = p2->next;
}
if (p1->next == NULL){
p0->next = NULL;
p1->next = p0;
}
else if (p1 == head && p2 == head){
p0->next = head;
head = p0;
}
else{
p1->next = p0;
p0->next = p2;
}
END:
return head;
}
#if(0)
void free_linklist(NODE *head){
NODE *p1, *p2;
p1 = head;
while(p1->next != NULL){
p2 = p1;
p1 = p1->next;
free(p2);
}
free(p1);
printf("test1");
}
#endif
#if(1)
void free_linklist(NODE *head){
NODE *p, *q;
p = head;
while(p->next != NULL){
q = p->next;
p->next = q->next;
free(q);
}
free(head);
}
#endif
程序的前面部分可以正常运行,但是在free_linklist(NODE *head)链表销毁函数这里卡住了
貌似是free函的原因,无法释放链表。
在vc++运行到这里的时候会出现一个界面Debug Assertrion Falied!
下面是出现的状况图片
求大佬解决,困惑好几天了
第二个
void free_linklist(NODE *head){
NODE *p,*q;
p = head;
while(p->next != NULL){
q =p;
p=p->next;
free(q);
}
free(p);
}
|