鱼C论坛

 找回密码
 立即注册
查看: 3645|回复: 6

链表代码的错误,不知在那里,帮忙看看

[复制链接]
发表于 2011-8-6 11:19:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这段代码我运行了一下,创建链表和第一次打印链表没有问题,但是运行到  删除链表  就弹出了对话框提示  程序终止了,帮忙看看。谢谢
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN struct student
struct student{

int num;
float score;
struct student *next;

};
int n = 0;
struct student *p1,*p2,*head;
void main()
{


void print(struct student *);
void dele(struct student *);
p1= (struct student *)malloc(sizeof(LEN));

head = p1;
printf("请输入学生的数据:\n");
scanf("%d",&p1->num);
printf("score\n");
scanf("%f",&p1->score);
p1->next = NULL;
while (p1->num)
{
n++;
p2 = p1;
p1 = (struct student *)malloc(sizeof(LEN));
p2->next = p1;

printf("请输入学生的数据:\n");
scanf("%d",&p1->num);
printf("score\n");
scanf("%f",&p1->score);

}
p2->next = NULL;
print(head);
dele(head);
//print(head);



}
void print(struct student *head)
{
int i ;
for (i = 1; i<=n;i++)
{
printf("num: %d \t",head->num);
printf("score:%f \t",head->score);
head = head->next;
if (i%2==0)
printf("\n"); 

}

}
void dele(struct student *p)
{
int find,boo=0,i;
printf("please inpput the num\n");
scanf("%d",&find);
p1=p; 

for (i=1;i<=n;i++)
{
if(strcmp(find,p1->num)==0 && i==1)
{
head=p1->next;
p1->next=NULL;
boo=1;
}
if(strcmp(find,p1->num)==0 && i>1)
{
p2->next=p1->next;
p1->next=NULL;
boo=1;
}
else
{

p2=p1;
p1=p1->next;
}


}
if(boo==0)
{
printf("sorry,there is no data of your num\n");
}


}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-6 11:21:50 | 显示全部楼层
补充一下    去掉  45行前面的   //      在看代码  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-6 11:30:21 | 显示全部楼层
没学C  帮你顶下算了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-6 14:28:21 | 显示全部楼层
谢谢 啊
,快来帮帮我吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-6 14:45:17 | 显示全部楼层
我把你那个代码前边稍微改了一下!你后边那个scanf也有错误!你自己调试吧!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct students{
    int     num;
    float     score;
    struct     students *next;
} st;
int     n = 0;
st         *p1,*p2,*head;

int main(void)
{
    void print(st *);
    void dele(st *);
    
    p1= (st *)malloc(sizeof(st));

    head = p1;
    printf("请输入学生的数据:\n");
    scanf("%d",&p1->num);
    printf("score\n");
    scanf("%f",&p1->score);    /* <<<<<<<<<<---------------*/
    /*p1->next = NULL;
    
    while (p1->num)
    {
        n++;
        p2 = p1;
        p1 = (st *)malloc(sizeof(st));
        p2->next = p1;

        printf("请输入学生的数据:\n");
        scanf("%d",&p1->num);
        printf("score\n");
        scanf("%f",&p1->score);

    }
    p2->next = NULL;
    print(head);
    dele(head);*/
    return 0;
}

/*
void print(st *head)
{
    int i ;
    for (i = 1; i<=n;i++) {
        
        printf("num: %d \t",head->num);
        printf("score:%f \t",head->score);
        head = head->next;
        if (i%2==0)
            printf("\n"); 
    }

}

void dele(st *p)
{
    int find,boo=0,i;
    printf("please inpput the num\n");
    scanf("%d",&find);
    p1=p; 

    for (i=1;i<=n;i++) {
    
        if(strcmp(find,p1->num)==0 && i==1) {
        
            head=p1->next;
            p1->next=NULL;
            boo=1;
        }
        if(strcmp(find,p1->num)==0 && i>1) {
        
            p2->next=p1->next;
            p1->next=NULL;
            boo=1;
        }
        else {

            p2=p1;
            p1=p1->next;
        }
    }
    if(boo==0) 
        printf("sorry,there is no data of your num\n");
}
*/


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-6 14:52:46 | 显示全部楼层
在啰唆一下!既然是给别人看的代码,有注释还是不较好!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-6 15:05:15 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-8 15:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表