#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define LEN sizeof(struct student)
struct student *create();//创建链表
struct student *del(struct student *head, int num);//删除链表中数字 (链表头指针,要删除的学号)
struct student *insert(struct student *head, struct student *head_2);//网链表中加元素(链表头指针,要加的那个的)
void print(struct student *head);//输出链表
struct student *clear(struct student *head);//清空链表
void change(struct student *head, int num, float score);//更改链表数据
struct student
{
long int num;
float score;
struct student *next;
};
int n;
char ch;
int main()
{
struct student *head, head_2;
int num, i;
float score;
printf("请先建立一个链表!(要结束请在number处输入0)\n");
head = create();
s: fflush(stdin);
printf("---------------------------------------------------------\n");
printf("请选择要的操作:\n");
printf("1.打印记录 ");
printf("2.插入记录 ");
printf("3.删除记录 \n");
printf("4.更改记录 ");
printf("5.清除记录 ");
printf("6.退出程序 \n");
printf("---------------------------------------------------------\n");
//scanf("%d", &i);
scanf_s("%d", &i);
switch (i)//根据用户输入的不同数字来执行不同的代码
{
case 1:
print(head);
goto s;
break;
case 2:
s4 : fflush(stdin);
printf("what number are you want to insert:");
scanf_s("%d", &head_2.num);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s4;
}
}
s5: fflush(stdin);
printf("what score are you want to insert:");
scanf_s("%f", &head_2.score);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s5;
}
}
head = insert(head, &head_2);
goto s;
break;
case 3:
s6 : fflush(stdin);
printf("please putinto the number you want to delete:");
scanf_s("%ld", &num);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s6;
}
}
head = del(head, num);
goto s;
break;
case 4:
s7 : fflush(stdin);
printf("Please putinto the number you want to change:");
scanf_s("%d", &num);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s7;
}
}
s8: fflush(stdin);
printf("Please putinto the score you want to save:");
scanf_s("%f", &score);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s8;
}
}
change(head, num, score);
break;
case 5:
head = clear(head);
break;
case 6:
return 0;
break;
default:
printf("对不起,你的输入有误,请重新输入!");
goto s;
break;
}
goto s;
}
struct student *create()
{
struct student *head;
struct student *p1, *p2;
s: fflush(stdin);
head = NULL;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please purinto student's munber:");
scanf_s("%ld", &p1->num);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s;
}
}
s1: printf("Please putinto student's score:");
fflush(stdin);
scanf_s("%f", &p1->score);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s1;
}
}
n = 0;
while (p1->num)
{
n++;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
s2: fflush(stdin);
printf("Please purinto student's munber:");
scanf_s("%ld", &p1->num);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s2;
}
}
if (p1->num == 0)
{
goto e;
}
s3: fflush(stdin);
printf("Please putinto student's score:");
scanf_s("%f", &p1->score);
while ((ch = getchar()) != '\n')
{
if ((ch >= 48 && ch <= 57) || ch == 46)
{
;
}
else
{
printf("你的输入有误!\n");
goto s3;
}
}
}
e: p2->next = NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p = head;
printf("共有%d条记录:\n", n);
if (head)
{
do
{
printf("%ld号学生的成绩是:%f\n", p->num, p->score);
p = p->next;
} while (p);
}
}
struct student *del(struct student *head, int num)
{
struct student *p1, *p2;
p2 = head;
if (NULL == head)
{
printf("The List is NULL!\n");
goto END;
}
p1 = head;
while (p1->num != num&&p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (num == p1->num)
{
if (p1 == head)
{
head = p1->next;
printf("Delete NO.%d succeed!\n", num);
n--;
}
else
{
p2->next = p1->next;
printf("Delete NO.%d succeed!\n", num);
n--;
}
}
else
{
printf("NO.%d not been found!\n", num);
}
END:
return head;
}
struct student *insert(struct student *head, struct student *head_2)
{
struct student *p0, *p1, *p2;
p0 = head_2;
p1 = head;
p2 = head;
if (p1 == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while (p0->num >p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num)
{
if (p1 == head)
{
head = p0;
}
else
{
p2->next = p0;
}
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
n++;
e: return head;
}
struct student *clear(struct student *head)
{
head = NULL;
n = 0;
printf("以清除全部记录!\n");
return head;
}
void change(struct student *head, int num, float score)
{
struct student *p1, *p2;
if (head == NULL)
{
printf("This is a NULL!\n");
goto end_2;
}
p1 = head;
while (p1->num != num&&p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p1->num == num)
{
p1->score = score;
}
else
{
printf("NO.%d not been found!", num);
}
end_2:
printf("");
}
|