|
发表于 2018-4-14 22:30:19
|
显示全部楼层
本楼为最佳答案
^_^
今天先就写到这里,我明天有事,要早点睡觉
Swap函数已经调试到完美,希望没有什么问题了,^_^
就剩下 void SortList(struct Postgraduate *head) 这个函数了
你先试着写一写,看看能不能写出来,如果明天有时间,我再来写,不过真的建议你先写一写
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Postgraduate // 研究生
{
int num; // 学号
char name[10]; // 姓名
char sex1[4]; // 性别
int score;
struct Postgraduate *prior;
struct Postgraduate *next;
} Node;
struct Postgraduate *CreateListDebug(struct Postgraduate arr[], int count)
{
// 头结点不存储数据
struct Postgraduate *head = malloc(sizeof(Node));;
head->num = 0;
head->name[0] = '\0';
head->sex1[0] = '\0';
head->score = 0;
head->prior = NULL;
head->next = NULL;
Node *p = head;
for(int i = 0; i < count; ++i)
{
Node *n = malloc(sizeof(Node));
n->num = arr[i].num;
strcpy(n->name, arr[i].name);
strcpy(n->sex1, arr[i].sex1);
n->score = arr[i].score;
n->prior = NULL;
n->next = NULL;
p->next = n;
n->prior = p;
p = p->next;
}
return head;
}
void Swap(Node *a, Node *b)
{
// a和b紧挨着的时候需要另一种方法
if((a->next == b) || (b->next == a))
{
// a在b的前面和b在a的前面不能使用同一种方法
if(b->next == a) // 让a始终在b的前面
{
struct Postgraduate *temp = a;
a = b;
b = temp;
}
struct Postgraduate *a_prior = a->prior;
struct Postgraduate *b_next = b->next;
if(b_next == NULL) // b是尾结点
{
a_prior->next = b;
b->prior = a_prior;
a->next = b_next;
a->prior = b;
b->next = a;
}
else
{
a_prior->next = b;
b->prior = a_prior;
a->next = b_next;
a->prior = b;
b->next = a;
b_next->prior = a;
}
}
else
{
struct Postgraduate *a_prior = a->prior;
struct Postgraduate *a_next = a->next;
struct Postgraduate *b_prior = b->prior;
struct Postgraduate *b_next = b->next;
if((a_next == NULL) || (b_next == NULL)) // 这两个结点中有一个是尾结点
{
if(a_next == NULL) // 让b成为尾结点
{
struct Postgraduate *temp = a;
a = b;
b = temp;
// update
a_prior = a->prior;
a_next = a->next;
b_prior = b->prior;
b_next = b->next;
}
a_prior->next = b;
b->prior = a_prior;
a->next = b_next;
a->prior = b_prior;
a_next->prior = b;
b_prior->next = a;
b->next = a_next;
}
else
{
a_prior->next = b;
b->prior = a_prior;
a->next = b_next;
a->prior = b_prior;
a_next->prior = b;
b_prior->next = a;
b->next = a_next;
b_next->prior = a;
}
}
}
void SortList(struct Postgraduate *head)
{
}
int main(void)
{
struct Postgraduate arr[] =
{
{0, "小红", "女", 100},
{1, "小明", "男", 10},
{2, "小玲", "女", 50},
{3, "小丽", "女", 40},
{4, "小林", "男", 90},
{5, "小张", "男", 5}
};
int arr_count = sizeof(arr) / sizeof(arr[0]);
struct Postgraduate *head = CreateListDebug(arr, arr_count);
Swap(head->next, head->next->next->next);
Swap(head->next->next->next, head->next);
Swap(head->next, head->next->next);
Swap(head->next->next, head->next);
Swap(head->next, head->next->next->next->next->next->next);
Swap(head->next->next->next->next->next->next, head->next);
Swap(head->next->next->next->next->next, head->next->next->next->next->next->next);
Swap(head->next->next->next->next->next->next, head->next->next->next->next->next);
return 0;
}
下面是前一个版本,未完成的版本,正因为有下面这个版本,才会有双向链表的这个版本,如果有兴趣,你可以参考#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Postgraduate // 研究生
{
int num; // 学号
char name[10]; // 姓名
char sex1[4]; // 性别
int score;
struct Postgraduate *next;
} Node;
void GetString(char *buff, int max_count)
{
fgets(buff, max_count, stdin);
int len = strlen(buff);
buff[len - 1] = '\0';
}
struct Postgraduate *CreateListDebug(struct Postgraduate arr[], int count)
{
struct Postgraduate *head = NULL;
Node *p;
for(int i = 0; i < count; ++i)
{
Node *n = malloc(sizeof(Node));
n->num = arr[i].num;
strcpy(n->name, arr[i].name);
strcpy(n->sex1, arr[i].sex1);
n->score = arr[i].score;
n->next = NULL;
if(head == NULL)
{
head = n;
p = head;
continue;
}
p->next = n;
p = p->next;
}
return head;
}
struct Postgraduate *CreateList(void)
{
struct Postgraduate *head = NULL;
Node *p;
int count = 5;
do
{
Node *n = malloc(sizeof(Node));
printf("学号:");
scanf("%d", &n->num);
getchar();
printf("姓名:");
GetString(n->name, 10);
printf("性别:");
GetString(n->sex1, 4);
printf("成绩:");
scanf("%d", &n->score);
getchar();
n->next = NULL;
if(head == NULL)
{
head = n;
p = head;
continue;
}
p->next = n;
p = p->next;
}
while(--count);
return head;
}
void Swap(Node *a, Node *b)
{
struct Postgraduate *temp_a;
struct Postgraduate *temp_b;
temp_a = a->next;
a->next = b->next;
temp_b = b->next->next;
b->next->next = temp_a;
b->next->next->next = temp_b;
}
void SortList(struct Postgraduate *head)
{
}
int main(void)
{
struct Postgraduate arr[] =
{
{0, "小红", "女", 100},
{1, "小明", "男", 10},
{2, "小玲", "女", 50},
{3, "小丽", "女", 40},
{4, "小林", "男", 90},
{5, "小张", "男", 5}
};
int arr_count = sizeof(arr) / sizeof(arr[0]);
//struct Postgraduate *head = CreateList();
struct Postgraduate *head = CreateListDebug(arr, arr_count); // 为debug方便,使用数组录入数据,每次都要手动输入太要命了,^_^
//Swap(head, head->next);
//Swap(head->next, head->next->next);
Swap(head, head->next->next);
return 0;
}
|
|