鱼C论坛

 找回密码
 立即注册
查看: 1156|回复: 3

[已解决]这个删除结点的函数哪里有问题,当他要删除第一个节点时就会出错

[复制链接]
发表于 2021-9-13 17:21:44 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
typedef struct Node* node;
struct Node {
    int data;
    node next;
};
node CreateList()
{
    int num;
    node head=NULL;
    node tail,p;//p指向新申请的节点,tail指向尾巴
    scanf("%d",&num);
    if(num==-1)
        return head;
    else
    {
        p=(node)malloc(sizeof(struct Node));
        head = p;
    }
    while(num!=-1)
    {
        tail=p;
        tail->data=num;//读入数据
        p = (node)malloc(sizeof(struct Node));
        tail->next = p;
        scanf("%d",&num);
    }
    tail->next=NULL;//尾巴指向NULL标志结束
    return head;
}
void PrintList(node head)
{
    node p;
    for(p=head;p!=NULL;p=p->next)
        printf("\t%d",p->data);
}
void DeleteData(node head,int m)//删除指定结点
{
    if(head)//链表非空
    {
        node p=head,temp;
        while(p->data!=m&&p!=NULL)//找到待删除的结点的位置
        {
            temp=p;//temp永远指向p的前一个位置
            p=p->next;
        }
        if(p==head)//如果删除的是头节点
        {
            head=p->next;
            free(p);
        }
       else if(p->data==m)
        {
           temp->next=p->next;
           free(p);
        }
       else
        {
           printf("没找到,草\n");
        }
    }
    else//链表为空
    {
        printf("链表为空\n");
    }
}
int main()
{
    int num;
    int n;
    node head=NULL;
    head = CreateList();
    PrintList(head);
    scanf("%d",&n);
    DeleteData(head,n);
    PrintList(head);
    return 0;
}
最佳答案
2021-9-13 20:15:30
本帖最后由 jhq999 于 2021-9-13 20:33 编辑

在函数定义和调用时,有三种常见的参数传递方式:

1.传值

2.传指针

3.传引用
#include "stdafx.h"
#include<stdlib.h>
typedef struct Node* node;
struct Node {
    int data;
    node next;
};
node CreateList()
{
    int num;
    node head=NULL;
    node tail=NULL,p=NULL;//p指向新申请的节点,tail指向尾巴
    scanf("%d",&num);
    if(num==-1)
        return head;
    else
    {
        p=(node)malloc(sizeof(struct Node));
        head = p;
    }
    while(num!=-1)
    {
        tail=p;
        tail->data=num;//读入数据
        p = (node)malloc(sizeof(struct Node));
        tail->next = p;
        scanf("%d",&num);
    }
    tail->next=NULL;//尾巴指向NULL标志结束
    return head;
}
void PrintList(node head)
{
    node p;
    for(p=head;p!=NULL;p=p->next)
        printf("\t%d",p->data);
}
void DeleteData(node *phead,int m)//删除指定结点             你传的是指针的值,你得传指针的地址,否则出函数后head不会变成next还是原来的,或者用引用void DeleteData(node &head,int m)
{
        node head=*phead;//////////////////////////////
    if(head)//链表非空
    {
        node p=head,temp=NULL;
        while(p->data!=m&&p!=NULL)//找到待删除的结点的位置
        {
            temp=p;//temp永远指向p的前一个位置
            p=p->next;
        }
        if(p==head)//如果删除的是头节点
        {
            head=p->next;
            free(p);
        }
       else if(p->data==m)
        {
           temp->next=p->next;
           free(p);
        }
       else
        {
           printf("没找到,草\n");
        }
    }
    else//链表为空
    {
        printf("链表为空\n");
    }
    *phead=head;///////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
int main()
{
    int num;
    int n;
    node head=NULL;
    head = CreateList();
    PrintList(head);
    scanf("%d",&n);
    DeleteData(&head,n);
    PrintList(head);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-13 19:16:16 | 显示全部楼层
C:\Users\YJ\Desktop\QQ截图20210913191416.jpg我这有用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-13 20:15:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-9-13 20:33 编辑

在函数定义和调用时,有三种常见的参数传递方式:

1.传值

2.传指针

3.传引用
#include "stdafx.h"
#include<stdlib.h>
typedef struct Node* node;
struct Node {
    int data;
    node next;
};
node CreateList()
{
    int num;
    node head=NULL;
    node tail=NULL,p=NULL;//p指向新申请的节点,tail指向尾巴
    scanf("%d",&num);
    if(num==-1)
        return head;
    else
    {
        p=(node)malloc(sizeof(struct Node));
        head = p;
    }
    while(num!=-1)
    {
        tail=p;
        tail->data=num;//读入数据
        p = (node)malloc(sizeof(struct Node));
        tail->next = p;
        scanf("%d",&num);
    }
    tail->next=NULL;//尾巴指向NULL标志结束
    return head;
}
void PrintList(node head)
{
    node p;
    for(p=head;p!=NULL;p=p->next)
        printf("\t%d",p->data);
}
void DeleteData(node *phead,int m)//删除指定结点             你传的是指针的值,你得传指针的地址,否则出函数后head不会变成next还是原来的,或者用引用void DeleteData(node &head,int m)
{
        node head=*phead;//////////////////////////////
    if(head)//链表非空
    {
        node p=head,temp=NULL;
        while(p->data!=m&&p!=NULL)//找到待删除的结点的位置
        {
            temp=p;//temp永远指向p的前一个位置
            p=p->next;
        }
        if(p==head)//如果删除的是头节点
        {
            head=p->next;
            free(p);
        }
       else if(p->data==m)
        {
           temp->next=p->next;
           free(p);
        }
       else
        {
           printf("没找到,草\n");
        }
    }
    else//链表为空
    {
        printf("链表为空\n");
    }
    *phead=head;///////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
int main()
{
    int num;
    int n;
    node head=NULL;
    head = CreateList();
    PrintList(head);
    scanf("%d",&n);
    DeleteData(&head,n);
    PrintList(head);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-15 20:05:00 | 显示全部楼层
jhq999 发表于 2021-9-13 20:15
在函数定义和调用时,有三种常见的参数传递方式:

1.传值

谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-9-22 07:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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