程序无法完成删除操作,我返回的return head我感觉不是首地址
#include<stdio.h>#include<stdlib.h>
#pragma warning(disable:4996)
#include<string.h>
typedef char DataType;
typedef struct ListNode
{
DataType name;
int grade;
struct ListNode* next;
}ListNode;
struct ListNode* creat()
{
ListNode* p1, * p2, * head;
int n;
p1 = p2 = (ListNode*)malloc(sizeof(ListNode));
printf("please input name:");
scanf("%s", p1->name);
printf("grade : ");
scanf("%d", &p1->grade);
head = NULL;
n = 0;
while (p1->grade)
{
n++;
if (1 == n)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct ListNode*)malloc(sizeof(ListNode));
printf("please input name:");
scanf("%s", p1->name);
printf("grade : ");
scanf("%d", &p1->grade);
}
p2->next = NULL;
return head;
}
ListNode* printNode(ListNode* head1)
{
ListNode* head;
head = head1;
printf("开始打印:\n");
do
{
printf("%s\n", head->name);
printf("%d\n", head->grade);
head = head->next;
} while (head);
return head1;
}
struct ListNode* DelNode(struct ListNode* head, DataType x)
{
ListNode* p1, * p2;
if (head == NULL)
{
printf("the list is null\n");
exit(-1);
}
p1 = p2 = head;
while (strcmp(p1->name, x) != 0 && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (strcmp(p1->name, x) == 0)
{
if (p1 = head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
}
return head;
}
int main()
{
DataType delname;
// char delname;
ListNode* head;
head = creat();
printf("请输入要删除的元素姓名 : \n");
scanf("%s", delname);
head = DelNode(head, delname);
printf("开始打印:\n");
head = printNode(head);
return 0;
}
我在printf是head指针也会改变位置,那它不就返回的不是头指针了吗 sunjiam 发表于 2022-3-13 16:30
我在printf是head指针也会改变位置,那它不就返回的不是头指针了吗
只要不返回不就完事?基本上你的函数 printNode() 用来打印单链表,只需传入头指针便可,无需返回啊 傻眼貓咪 发表于 2022-3-13 16:48
只要不返回不就完事?基本上你的函数 printNode() 用来打印单链表,只需传入头指针便可,无需返回啊
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#pragma warning(disable:4703)
#include<string.h>
typedef char DataType;
typedef struct ListNode
{
DataType name;
int grade;
struct ListNode* next;
}ListNode;
struct ListNode* creat()
{
ListNode* p1, * p2, * head;
int n;
p1 = p2 = (ListNode*)malloc(sizeof(ListNode));
printf("please input name:");
scanf("%s", p1->name);
printf("grade : ");
scanf("%d", &p1->grade);
head = NULL;
n = 0;
while (p1->grade)
{
n++;
if (1 == n)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct ListNode*)malloc(sizeof(ListNode));
printf("please input name:");
scanf("%s", p1->name);
printf("grade : ");
scanf("%d", &p1->grade);
}
p2->next = NULL;
return head;
}
void printNode(ListNode* head)
{
printf("开始打印:\n");
do
{
printf("%s\n", head->name);
printf("%d\n", head->grade);
head = head->next;
} while (head);
}
void DelNode(struct ListNode* head, int grade)
{
ListNode* p1, * p2;
if (head == NULL)
{
printf("the list is null\n");
exit(-1);
}
p1 = head;
while (p1->grade != grade && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p1->grade == grade)
{
if (p1 = head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
}
}
int main()
{
int grade;
// char delname;
ListNode* head;
head = creat();
printf("请输入要删除的元素姓名 : \n");
scanf("%d", &grade);
DelNode(head, grade);
printf("开始打印:\n");
printNode(head);
return 0;
}
傻眼貓咪 发表于 2022-3-13 16:48
只要不返回不就完事?基本上你的函数 printNode() 用来打印单链表,只需传入头指针便可,无需返回啊
我删除不了啊,我看小甲鱼将的时候就返回return head;
我有点懵 please input name:小狗
grade : 99
please input name:小猫
grade : 98
please input name:小姐
grade : 65
please input name:小弟
grade : 888
please input name:小节
grade : 666
please input name:AD
grade : 0
请输入要删除的元素成绩 :
888
开始打印:
开始打印:
小狗
99
小猫
98
小姐
65
小弟
888
小节
666
C:\Users\lenovo\OneDrive\桌面\ly\链表1\x64\Debug\链表1.exe (进程 8344)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . . 抱歉兄弟,我不擅长修改其他人的代码(可能太长),所以我重新写了,你试试:#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define T char
// 单链表结构体
typedef struct Node {
T name;
int grade;
struct Node* next;
}ListNode;
// 输入
ListNode* insert(ListNode* head) {
ListNode* tail = head;
head = (ListNode*)malloc(sizeof(ListNode));
if (head) {
printf("please input a name: ");
if(scanf("%s", head->name))
;
if(getchar())
;
printf("please input a grade: ");
if(scanf("%d", &head->grade))
;
if (getchar())
;
head->next = tail;
}
else printf("head is NULL\n");
return head;
}
// 删除
ListNode* delete(ListNode* head, T name) {
ListNode* prev, * now;
prev = now = head;
if (head) {
while (head) {
if (!(strcmp(now->name, name))) break;
prev = now;
now = now->next;
}
prev->next = now->next;
free(now);
return head;
}
else printf("head is NULL\n");
return NULL;
}
// 打印
void print(ListNode* head) {
if (head) {
while (head) {
printf("name: %s, grade: %d\n", head->name, head->grade);
head = head->next;
}
}
else printf("head is NULL\n");
}
ListNode* FREE_ALL(ListNode* head) {
if (head) {
while (head) {
ListNode* p = head;
head = head->next;
free(p);
}
return head;
}
else printf("head is NULL\n");
return NULL;
}
int main() {
ListNode* head = NULL;
int N = 3; // 假设想输入 3 个人
T name;
for (int n = 0; n < N; n++) {
head = insert(head);
}
print(head); // 打印单链表
printf("Please enter the name you want to delete: ");
if (scanf("%s", name))
;
head = delete(head, name); // 删除个资
print(head);// 打印单链表
head = FREE_ALL(head); // 释放内存
free(head); // 释放内存
return 0;
} 傻眼貓咪 发表于 2022-3-13 18:28
抱歉兄弟,我不擅长修改其他人的代码(可能太长),所以我重新写了,你试试:
我找见自己的错误了,也谢谢你提供的代码{:5_108:}
页:
[1]