研究了三晚的结构体链表的创建、插入、删除、输出、释放。期间想要写出结构体链表排序的功能。。。然而我手中三本C语言的书都没有交排序。。。于是自己想尝试下。。。
走上了不归路啊啊啊。。。考纲都不考我研究这个干啥!!!
弄了两晚上的排序,自己写了两种思路,网上找了一种思路,还是没有搞定。。吐了吐了。。
还写了另一种错误的删除方案。。。妈哒。。。
待日后有时间再来解决,倘若有大佬看到能指出我这个沙雕小白的错误那就很是开心了!
附上成功的创建、插入、删除、输出、释放的代码
源代码:
#pragma warning(disable:4996) #include#include #include #define LEN sizeof(struct STU) struct STU { char num[7]; float score; struct STU* next; }; struct STU* Create_link(int size); struct STU* Sort_link(struct STU* head); void Print_link(struct STU* head); void Add_link(struct STU** head, struct STU stu); void Del_link(struct STU** head, char* no); void Destroy_link(struct STU** head); void main() { int n; struct STU* head = NULL, stu;// *no=NULL; char no[7]; printf("本次输入信息的学生个数:n"); scanf("%d", &n); head = Create_link(n); //head = Sort_link(head); printf("n信息输入结果如下:n"); Print_link(head); #if(1) printf("n输入需要插入的学生信息:n"); scanf("%s %f", stu.num, &stu.score); Add_link(&head, stu); printf(" 信息插入后的结果:n"); Print_link(head); #endif #if(1) printf("n输入要删除信息的学号:n"); scanf("%s",no); Del_link(&head, no); printf("n信息删除后的结果:n"); Print_link(head); #endif Destroy_link(&head); } struct STU* Create_link(int size) { struct STU* head=NULL, * p1, * p2=NULL; int i = 0; while (i < size) { p1 = (struct STU*)malloc(LEN); p1->next = NULL; printf("请输入第%d个学生的学号和成绩:n", i + 1); scanf("%s %f", p1->num,&p1->score); if (head== NULL) { head = p1; p2 = p1; } else { p2->next = p1; p2 = p1; } i++; } return (head); } void Print_link(struct STU* head) { struct STU* p; p = head; while(p != NULL) { printf("%s %.1fn", p->num, p->score); p = p->next; } } void Add_link(struct STU** head, struct STU stu) { struct STU* p, * p1, * p2; p = (struct STU*)malloc(LEN); strcpy(p->num, stu.num); p->score = stu.score; p->next= NULL; p1 = p2 = *head; while (p1 != NULL && strcmp(p->num, p1->num) > 0) { p2 = p1; p1 = p1->next; } if (p1 == NULL)//说明已经移动到链表末尾 { p2->next = p; p->next = NULL;//链表在末尾加入 } else if (strcmp(p->num, p1->num) < 0)//添加的链表学号若比p1小 { if (*head == p1)//链表在表首加入 { *head = p; p->next = p1;//或(*head)->next = p1; } else { p2->next = p; p->next = p1;//链表在中间加入 } } } #if(1) void Del_link(struct STU** head, char* no) { struct STU * p, * p1; int n; char nostr[7]; p1 = p = *head; while (strcmp((p->num), no) != 0) { p1 = p; p = p->next;//为了找到目标学号 } if (p == NULL) { printf("该链表没有指定的删除信息,是否重新输入?n"); scanf("%d", &n); if (n == 1) { printf("请重新输入学号:"); scanf("%s", nostr); strcpy(*no, nostr); p1 = p = *head; } } if (strcmp((p->num), no) == 0) { if (p == *head) { p1 = p->next; *head = p1; free(p); } else if (p!= NULL) { p1->next = p->next; free(p); } } } #endif void Destroy_link(struct STU** head) { struct STU* p; while (*head != NULL) { p = *head; *head = (*head)->next; free(p); } *head = NULL; }
排序的错误代码:
//排序代码,第一种方案,失败,主要因为char型num无法直接比大小 void Sort_link(struct STU * head, int size)
#if(0)
void Sort_link(struct STU* head,int n)
{
struct STU* temp=NULL, *temp2=NULL,* p1, *p2;
int i, j;
if (n > 1)
{
p1 = head;
p2 = p1->next;
if (n < 3)
{
if (p1->num > p2->num)
{
head = p2;
p2->next = p1;
p1->next = NULL;
}
}
if (n >= 3)
{
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (p1->num > p2->num)
{
if (head == p1)
{
head = p2;
temp = p2;
p1->next = p2->next;
p2->next = p1;
p2 = p1->next;
}
else if (p2->next == NULL)
{
temp->next = p2;
p2->next = p1;
p1->next = NULL;
p1 = head;
p2 = p1->next;
temp = NULL;
}
else
{
temp->next = p2;
p1->next = p2->next;
p2->next = p1;
temp = p2;
p2 = p1->next;
}
}
if (p1->num <= p2->num)
{
if (head == p1)
{
temp = p2->next;
p1 = p2;
p2 = temp;
temp = head;
}
else if (p2->next == NULL)
{
temp = NULL;
p1 = head;
p2 = p1->next;
}
else
{
temp2 = temp;
temp = p2->next;
temp2 = p1;
p1 = p2;
p2 = temp;
temp = temp2;
temp2 = NULL;
}
}
}
}
}
}
}
#endif
//排序代码,第二种方案,失败。。。。。。。。void Sort_link(struct STU** head);
#if(0)
void Sort_link(struct STU** head, int n)
{
struct STU* temp = NULL, * p1, * p2;
if (n > 1)
{
p1 = *head;
p2 = p1->next;
if (n < 3)//只有两行数据的时候交换
{
while (strcmp(p1->num , p2->num) > 0)
{
*head = p2;
p2->next = p1;
p1->next = NULL;
}
}
if (n >= 3)
{
while (strcmp(p1->num, p2->num) > 0)
{
if ((*head) == p1)//在链首交换
{
*head = p2;
temp = p2->next;
p2->next = p1;
p1->next = temp;
temp = p2;
p2 = p1->next;
}
else if (p2->next == NULL)//在链尾交换
{
temp->next = p2;
p2->next = p1;
p1->next = NULL;
}
else//在链中交换
{
temp->next = p2;
temp = p2->next;
p1->next = temp;
p2->next = p1;
temp = p2;
p2 = p1->next;
}
}
}
}
return;
}
#endif
//排序代码,第三种方案,失败。。。。。。。。struct STU* Sort_link(struct STU* head);
#if(0)
struct STU* Sort_link(struct STU* head)
{
struct STU* p, * q, * tail,*flag;
char temp_num=NULL;
int temp_score;
if (head == NULL)
{
return (head);
}
else
{
tail = NULL;
flag = NULL;
p = q = NULL;
while (flag != head)
{
tail = flag;
flag = head;
p = q = head;
while(p->next!=tail)
{
q = p->next;
if (strcmp(p->num, q->num) > 0)
{
strcpy(temp_num, p->num);
strcpy( p->num,q->num);
strcpy(q->num, temp_num);
temp_score = p->score;
p->score = q->score;
q->score = temp_score;
flag = p->next;
}
p = p->next;
}
}
return (head);
}
}
#endif
删除功能的错误代码:
#if(0)
void Del_link(struct STU** head, char* no)
{
struct STU* p1, * p = *head;
if (strcmp((*head)->num, no) == 0)
{
*head = (*head)->next;
free(p);
}
else
{
p1 = *head;
while (p != NULL && strcmp((p->num), no) != 0)
{
p1 = p;
p = p->next;
}
if (p == NULL)
{
printf("该链表无指定删除的学号");
}
if (p != NULL && strcmp((p->num), no) == 0)
{
p1->next = p->next;
free(p);
}
}
}
#endif



