标题:单链表删除第一个节点出现错误,不明白为什么。
只看楼主
chenxd
Rank: 2
来 自:北京
等 级:论坛游民
帖 子:20
专家分:24
注 册:2015-5-19
得分:0 
回复 10楼 hellovfp
我也在疑惑自己是不是压根没有创建头结点,现在明白了。非常感谢!!!确实指针这一块我现在还是感觉很模糊

不要嘲笑我家樱木!
2015-11-21 22:02
张童
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2015-11-30
得分:0 
我也想问你刚才问的,节点和指针它们的声明都是一样的,按理说它们应该类型是一样的,为什么这个是节点,那个是指针
2015-12-07 16:31
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
结点的单词是node,不是node*,前者是实体,后者是指针。在关于链表的描述中,往往把“指向结点的指针”简称“结点”,其实那是错误的简称,在自己阅读和书写的时候,必须心中有数到底说的哪个概念。

授人以渔,不授人以鱼。
2015-12-07 16:37
yy896988359
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-6-20
得分:0 
回复 楼主 chenxd
帮我看一道题可以吗大神。我第一个节点删不了,刚学有点不懂,求解决。第一个节点出现删除时,出现错误。求大神解决
题如下:
#include<stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    char name[10];
    long no;
    char tel[13];
    char Email[20];
    struct student *next;
};
struct student *head;
int n;
struct student *creat()
{
    struct student *p,*q;
    int i;
    printf("学生人数:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        p=(struct student *)malloc(LEN);
        printf("姓名:"); scanf("%s",p->name);
        printf("学号:"); scanf("%ld",&p->no);
        printf("电话:"); scanf("%s",p->tel);
        printf("邮箱:"); scanf("%s",p->Email);
        if(i==0)
            head=p;
        else
            q->next=p;
        q=p;
    }
    q->next=NULL;
    return(head);
}
void print(struct student *head)
{
    struct student *p;
    printf("\n\tname\t\tno\t\ttel\t\tEmail\n");
    p=head;
    if(p==NULL)
        printf("链表为空");
    else
    {
        while(p!=NULL)
        {
            printf("\t%s\t\t%d\t\t%s\t\t%s\n",p->name,p->no,p->tel,p->Email);
            p=p->next;
        }
    }
}
void find(struct student *head)
{
    int st;
    struct student *p;
    printf("输入学号:");
    scanf("%ld",&st);
    p=head;
    while(p!=NULL&&p->no!=st)
        p=p->next;
    if(p!=NULL)
    {
        printf("\n\tname\tno\ttel\tEmail\n");
        printf("\t%s\t%d\t%s\t%s\n",p->name,p->no,p->tel,p->Email);
    }
    else
        printf("不存在%d学号的学生\n",st);
}
int del(struct student *head,int m)
{
    struct student *p,*q;
    q=head;
    p=head->next;
    while(p!=NULL)
        if(m!=p->no)
        {
            q=q->next;
            p=p->next;
        }
        else
            break;
    if(p==NULL)
        return 0;
    q->next=p->next;
    free(p);
    return 1;
}
void insert(struct student *head,int m)
{
    struct student *p,*q,*k;
    k=(struct student *)malloc(LEN);
    k->no=m;
    q=head;
    p=head->next;
    while(p!=NULL)
        if(p->no<=m)
        {
            q=q->next;
            p=p->next;
        }
        else
            break;
        k->next=p;
        q->next=k;
        n=n+1;
        printf("请输入新学生的基本情况:\n");
        printf("姓名:"); scanf("%s",k->name);
        printf("学号:"); scanf("%ld",&k->no);
        printf("电话:"); scanf("%s",k->tel);
        printf("邮箱:"); scanf("%s",k->Email);
        
}
void main()
{
    int k,m;
    head=creat();
    printf("链表为:");
    print(head);
    printf("寻找链表的节点为:");
    find(head);
    printf("输入插入节点的学号:");
    print(head);
    scanf("%d",&m);
    insert(head,m);
    print(head);
    printf("输入删除节点的学号:");
    scanf("%d",&m);
    k=del(head,m);
    if(k==1)
    {
        printf("删除节点后的链表为:");
        print(head);
    }
    else
        printf("需要删除的节点不存在");
}
2018-06-20 17:35
yy896988359
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-6-20
得分:0 
帮我看一道题可以吗大神。我第一个节点删不了,刚学有点不懂,求解决。第一个节点出现删除时,出现错误。求大神解决
题如下:
#include<stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    char name[10];
    long no;
    char tel[13];
    char Email[20];
    struct student *next;
};
struct student *head;
int n;
struct student *creat()
{
    struct student *p,*q;
    int i;
    printf("学生人数:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        p=(struct student *)malloc(LEN);
        printf("姓名:"); scanf("%s",p->name);
        printf("学号:"); scanf("%ld",&p->no);
        printf("电话:"); scanf("%s",p->tel);
        printf("邮箱:"); scanf("%s",p->Email);
        if(i==0)
            head=p;
        else
            q->next=p;
        q=p;
    }
    q->next=NULL;
    return(head);
}
void print(struct student *head)
{
    struct student *p;
    printf("\n\tname\t\tno\t\ttel\t\tEmail\n");
    p=head;
    if(p==NULL)
        printf("链表为空");
    else
    {
        while(p!=NULL)
        {
            printf("\t%s\t\t%d\t\t%s\t\t%s\n",p->name,p->no,p->tel,p->Email);
            p=p->next;
        }
    }
}
void find(struct student *head)
{
    int st;
    struct student *p;
    printf("输入学号:");
    scanf("%ld",&st);
    p=head;
    while(p!=NULL&&p->no!=st)
        p=p->next;
    if(p!=NULL)
    {
        printf("\n\tname\tno\ttel\tEmail\n");
        printf("\t%s\t%d\t%s\t%s\n",p->name,p->no,p->tel,p->Email);
    }
    else
        printf("不存在%d学号的学生\n",st);
}
int del(struct student *head,int m)
{
    struct student *p,*q;
    q=head;
    p=head->next;
    while(p!=NULL)
        if(m!=p->no)
        {
            q=q->next;
            p=p->next;
        }
        else
            break;
    if(p==NULL)
        return 0;
    q->next=p->next;
    free(p);
    return 1;
}
void insert(struct student *head,int m)
{
    struct student *p,*q,*k;
    k=(struct student *)malloc(LEN);
    k->no=m;
    q=head;
    p=head->next;
    while(p!=NULL)
        if(p->no<=m)
        {
            q=q->next;
            p=p->next;
        }
        else
            break;
        k->next=p;
        q->next=k;
        n=n+1;
        printf("请输入新学生的基本情况:\n");
        printf("姓名:"); scanf("%s",k->name);
        printf("学号:"); scanf("%ld",&k->no);
        printf("电话:"); scanf("%s",k->tel);
        printf("邮箱:"); scanf("%s",k->Email);
        
}
void main()
{
    int k,m;
    head=creat();
    printf("链表为:");
    print(head);
    printf("寻找链表的节点为:");
    find(head);
    printf("输入插入节点的学号:");
    print(head);
    scanf("%d",&m);
    insert(head,m);
    print(head);
    printf("输入删除节点的学号:");
    scanf("%d",&m);
    k=del(head,m);
    if(k==1)
    {
        printf("删除节点后的链表为:");
        print(head);
    }
    else
        printf("需要删除的节点不存在");
}
2018-06-20 17:36
yy896988359
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-6-20
得分:0 
回复 15楼 yy896988359
求大神帮我改一下。谢谢了!
2018-06-20 17:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-459421-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.174161 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved