#if 1
#include <stdio.h>
#include <stdlib.h>
struct Student {
int num;
float score;
struct Student* next;
};
void output(struct Student* head);
int main() {
struct Student* head = NULL,*prep = NULL;
struct Student* node,*tail = NULL;
struct Student* p;
enum direction { left=-1,right=1}dire;//1 代表从正方向,也就是尾巴开始插入
int i,j,length;
double a;
printf("please input the direction that you want to insert!\n");
scanf_s("%d", &dire);
scanf_s("%lf", &a);
//node = (struct Student*)malloc(sizeof(struct Student));
//node->num = 001;
//node->score = 89.5 ;
//node->next = NULL;//动态创建一个节点,并且赋值,指向空
//head = node;//节点给头
//tail = node;
length = 0;
j = 0;
do {
node = (struct Student*)malloc(sizeof(struct Student));
node->score = a;
node->next = NULL;//动态创建一个节点,并且赋值,指向空
length++;
if (head==NULL)
{
head = node;
tail = node;
prep = node;
}
else
{
p = head;
if (node->score > tail->score)//比最后一个节点的值还大的话,就直接插入到尾部,????(是为什么这个判断很重要,但是下面那个判断不重要)
{
tail->next = node;//先移动指向
tail = node;
}
else if(node->score < head->score)
{
node->next = head;//当前的头成为新插入的节点的next
head = node;
}
else
{
//printf("tail-1 = %lf,tail = %lf\n", p->score, tail->score);
//(tail - 1)->next = node;//看来结构体指针不能使用加减号,得单独定义一个前指针和后指针
//node->next = tail;
while (node->score > p->score)
{
prep = p;
p = p->next;
}
prep->next = node;
node->next = p;
}
//if (dire==right)
//{
// tail->next = node;//先移动指向
// tail = node;
//}
//else
//{
// node->next = head;//当前的头成为新插入的节点的next
// head = node;//新插入的节点成为头结点
//}
}
scanf_s("%lf", &a);
} while (a != 0);
//node = (struct Student*)malloc(sizeof(struct Student));
//node->num = 002;
//node->score = 90;
//node->next = NULL;//同上一个节点的创建方式,重新创了一个节点
//head->next = node;//节点给头的next
//node = (struct Student*)malloc(sizeof(struct Student));
//node->num = 003;
//node->score = 85;
//node->next = NULL;
//head->next->next = node;
output(head);//传入头指针,进行链表打印
return 0;
}
void output(struct Student* head)
{
struct Student* p;
p = head;//
do {//使用函数,参数是链表头
printf("%5.1f\n", p->score);
p = p->next;
} while (p != NULL);
}
#endif