donjames 发表于 2021-9-13 17:21:44

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

#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;
}

844616771 发表于 2021-9-13 19:16:16

C:\Users\YJ\Desktop\QQ截图20210913191416.jpg我这有用

jhq999 发表于 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;
}

donjames 发表于 2021-9-15 20:05:00

jhq999 发表于 2021-9-13 20:15
在函数定义和调用时,有三种常见的参数传递方式:

1.传值


谢谢大佬
页: [1]
查看完整版本: 这个删除结点的函数哪里有问题,当他要删除第一个节点时就会出错