C语言链表节点删除问题

Zhngbn斌 2018-01-06 04:04:32
题目:试编写一个程序完成:建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。
问题(错误):在struct student *cancel()的定义中出现了10几个错误。
1.struct student *head,*p1,*p2;这里missing ';' before 'type'
2.head=p2=p1=p; ,有'head' : undeclared identifier
3.'p1' : undeclared identifier
left of '->ages' must point to struct/union
left of '->next' must point to struct/union
'p2' : undeclared identifier
left of '->next' must point to struct/union
left of '->next' must point to struct/union
left of '->next' must point to struct/union
left of '->next' must point to struct/union
希望大家能帮忙解决,已经想了好久了,都找不到问题所在,希望大家能帮我改改代码,指出问题所在。感激不尽
源代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
char sex;
int ages;
struct student *next;
};
int n;

struct student *creat() //不需要传入,只需返回一个head地址
{
struct student *head,*p1,*p2;
n=0;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
scanf("%d%s%c%d",&p1->num,p1->name,&p1->sex,*&p1->ages);
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d%s%c%d",&p1->num,p1->name,&p1->sex,&p1->ages);
}
p2->next=NULL;
return(head);
}

struct student *cancel(struct stduent *p)
{
int getage;
scanf("%d",&getage);
struct student *head,*p1,*p2;
head=p2=p1=p; //把p的地址传给前者

while(p1!=0) //p1是p2前面的表头
{
if(p1->ages==getage)
{
if(head==p1)
head=p1->next;
else
p2->next=p1->next;
p1=p1->next;
}
else
{
p2=p1;
p1=p1->next;
}
}
return head;
}

void print(struct student *p)
{
while(p1!=0)
{
printf("%-5d %-8s %c %2d\n",p->num,p->name,p->sex,p->ages);
p1=p1->next;
}
}

void main()
{
struct student *head; //返回的head地址传给head指针变量
struct stduent *head1;
head=creat();
head1=cancel(head);
print(head1);
}
...全文
946 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yx777777 2018-01-09
  • 打赏
  • 举报
回复
老哥这个代码里面都是些低级错误呀,student写成了stduent,p写成了p1
赵4老师 2018-01-08
  • 打赏
  • 举报
回复
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
自信男孩 2018-01-08
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define LEN sizeof(struct student)

struct student
{
    int num;
    char name[20];
    char sex;
    int ages;
    struct student *next;
};

int n;

struct student *creat()
{
    struct student *head,*p1,*p2;
    head = NULL;
    p1 = p2 = (struct student *)malloc(LEN);
    scanf("%d%s %c%d", &p1->num,p1->name,&p1->sex,&p1->ages);
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head = p1;
        else
            p2->next=p1;
        p2 = p1;
        p1 = (struct student *)malloc(LEN);
        scanf("%d%s %c%d", &p1->num,p1->name,&p1->sex,&p1->ages);
    }
    free(p1);    /* free not used node */
    p2->next = NULL;

    return(head);
}

struct student *cancel(struct student *phead)
{
    int getage;
    struct student *prev,*pcur;

    scanf("%d", &getage);

    if (phead->ages == getage) {
        prev = phead;
        phead = phead->next;
        free(prev);
        return phead;
    }

    prev = phead;
    pcur = phead->next;
    while (pcur) {
        if (pcur->ages == getage) {
            prev->next = pcur->next;
            free(pcur);
            return phead;
        }
        prev = pcur;
        pcur = pcur->next;
    }

    return phead;

    /*

    while(p1!=0)  //p1是p2前面的表头
    {
        if(p1->ages==getage)
        {
            if(head==p1)
                head=p1->next;
            else
                p2->next=p1->next;
            p1=p1->next;
        }
        else
        {
            p2=p1;
            p1=p1->next;
        }
    }
    */
}

void print(struct student *phead)
{
    struct student *p = phead;
    while(p)
    {
        printf("%-5d %-8s %c %2d\n",p->num,p->name,p->sex,p->ages);
        p = p->next;
    }
}

//void main()
int main()
{
    struct student *head;
    head = creat();
    print(head);
    head = cancel(head);
    print(head);
}
参考一下吧
Zhngbn斌 2018-01-06
  • 打赏
  • 举报
回复
初始化为NULL没用呐
qq_21767819 2018-01-06
  • 打赏
  • 举报
回复
一个习惯吧,试试给你定义的指针初始化为NULL试试

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧