记录一段有关结构的东西,备忘。
typedef double price; //可以把已存在的数据类型换个名字。
C语言允许对相同结构类型的变量进行直接赋值。stu1 = stu2;
结构指针比如 STU *stu1, 和 int *p 一样都是占四个字节。
结构体指针访问变量的两种方式(*stu1).ID 或者 stu1->ID;
再记录一个容易忘记的东西
typedef struct node LNode, *LinkList;
LNode s1 ⇒ struct node
LinkList s2 ==> struct node * ⇒ LNode *
//相比较而言,感觉头插比尾插更简单!
//头插法
void CreateList(LinkList head)
{
LinkList p;
int num;
while(scanf("%d", &num), num!=-1)
{
p = (LinkList)malloc(sizeof(LNode));
p->data = num;
p->next = head->next;
head->next = p;
}
}
//尾插法
void CreateList(LinkList head)
{
LinkList p, rear = head;
int; num;
while(scanf("%d", &num), num!=-1)
{
p = (LinkList)malloc(sizeof(LNode));
p->data = num
rear->next = p;
rear = p;
}
rear->next = NULL;
}
课设代码
带头结点判空表的条件 H->next == NULL
不带头结点判空表的条件 H == NULL;此时H是头指针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct stud
{
char id[13];
char name[21];
int score1, score2, score3;
int sum;
struct stud *next;
};
typedef struct stud LNode, *LinkList;
//LNode a等价于struct stud a
//LinkList p 等价于 struct stud *p
void Display();
void CreateList(LinkList head);
void PrintList(LinkList head);
void ListDelete(LinkList head);
void ListInsert(LinkList head);
void ListStaticScore(LinkList head);
int main()
{
LinkList head;
int select;
head = (LinkList) malloc (sizeof(LNode));
head -> next = NULL;
Display();
while(1)
{
scanf("%d", &select);
switch(select)
{
case 1: CreateList(head);
break;
case 2: ListInsert(head);
break;
case 3: ListDelete(head);
break;
case 4: PrintList(head);
printf("\n");
break;
case 5: ListStaticScore(head);
break;
case 0:
exit(0);
}
}
return 0;
}
void Display()
{
printf("\n======================================\n\n");
printf("*欢迎进入学生管理系统 V3.0!*\n");
printf("1: 创建列表\n");
printf("2: 插入元素\n");
printf("3: 删除元素\n");
printf("4: 显示元素\n");
printf("5: 统计\n");
printf("0: 退出系统\n");
printf("\nCopyright @ 2017 STU by CLAY. All rights reserved.");
printf("\n======================================\n");
printf("请选择菜单项: \n");
}
void CreateList(LinkList head)//尾插法
{
printf("输入若干整数,输入 # 表示结束 \n");
LinkList p, rear = head;
while(1)
{
p = (LinkList)malloc(sizeof(LNode));
scanf("%s", p->id);
if(strcmp(p->id, "#") != 0)
//if(p->id[0] != '#')
{
scanf("%s%d%d%d", p->name, &p->score1, &p->score2, &p->score3);
p->sum = p->score1 + p->score2 + p->score3;
}
else
break;
rear ->next = p;
rear = p;
}
rear ->next = NULL;
printf("\n新创建的学生信息为:\n");
PrintList(head);
}
void PrintList(LinkList head)
{
LinkList p = head ->next;
while(p)
{
printf("%s\t%s\t%d\t%d\t%d\t%d\n", p ->id, p->name, p->score1, p->score2, p->score3, p->sum);
p = p ->next;
}
}
void ListInsert(LinkList head)//按照ID从小到大排序
{
printf("当前链表中学生信息为:\n");
PrintList(head);
LinkList p, pre = head->next, rec;//rec存的是待插入节点之前的节点,按id顺序插
int flag = 0;
p = (LinkList)malloc(sizeof(LNode));
printf("\n请输入待添加学生的信息: \n");
scanf("%s%s%d%d%d", p->id, p->name, &p->score1, &p->score2, &p->score3);
p->sum = p->score1+p->score2+p->score3;
while(pre)
{
if( strcmp(pre->id, p->id) == 0)
{
flag = 1;
break;
}
if( strcmp(pre->id, p->id) > 0 )
{
flag = 0;
break;
}
rec = pre;
pre = pre->next;
}
if(flag == 1)
{
printf("学号已有,无法添加\n\n");
}
else
{
if( !pre)
{
rec->next = p;
p->next = NULL;
}
else
{
p->next = rec->next;
rec->next = p;
}
}
printf("\n链表修改后的学生信息为:\n");
PrintList(head);
}
void ListDelete(LinkList head)
{
printf("当前链表中学生信息为:\n");
PrintList(head);
LinkList p, pre=head->next, rec;
int flag = 0;
p = (LinkList)malloc(sizeof(LNode));
printf("\n请输入待删除学生的学号信息:\n");
scanf("%s", p->id);
while(pre)
{
if( strcmp(p->id, pre->id) == 0)
{
printf("待删除学生的信息为:\n");
printf("%s\t%s\t%d\t%d\t%d\t%d\n", pre->id, pre->name, pre->score1, pre->score2, pre->score3, pre->sum);
flag = 1;
break;
}
rec = pre;
pre = pre->next;
}
if(flag == 0)
printf("查无此号,无法删除!\n");
else//考虑三种情况在首元节点,中间,末尾的空节点
{
if( pre == head->next)
{
head->next = pre->next;
free(pre);
}
else if( pre->next != NULL)
{
rec->next = pre->next;
free(pre);
}
else
rec->next = NULL;
}
printf("\n链表修改后的学生信息为:\n");
PrintList(head);
}
void ListStaticScore(LinkList head)
{
printf("当前链表中学生信息为:\n");
PrintList(head);
printf("有不及格学生信息统计为:\n");
LinkList pre = head->next;
while(pre)
{
if( (pre->score1 < 60) || (pre->score2 < 60) || (pre->score3 < 60) )
{
printf("%s\t%s\t%d\t%d\t%d\t%d\n", pre->id, pre->name, pre->score1, pre->score2, pre->score3, pre->sum);
}
pre = pre->next;
}
}