|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#include<string.h>
typedef char DataType;
typedef struct ListNode
{
DataType name[20];
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[20])
{
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[20];
// char delname[20];
ListNode* head;
head = creat();
printf("请输入要删除的元素姓名 : \n");
scanf("%s", delname);
head = DelNode(head, delname);
printf("开始打印: \n");
head = printNode(head);
return 0;
}
抱歉兄弟,我不擅长修改其他人的代码(可能太长),所以我重新写了,你试试: - #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- #define T char
- // 单链表结构体
- typedef struct Node {
- T name[20];
- 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[20]) {
- 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[20];
- 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;
- }
复制代码
|
|