//定义结构体
typedef struct data {
int data;
struct data *pro; //前驱
struct data *next; //后继
}DATA;
//创建双向循环链表
DATA *creat () {
DATA *head, *node1, *node2;
char choice;
//先创建一个空的双向循环链表
head = node1 = malloc(N);
head->pro = head;
printf ("\n PLease input the data: \n");
do {
node2 = malloc(N);
scanf ("%d“,node -> data);
node2->next = head;
head->pro = node2;
node2->pro = node1;
node1->next = node2;
node1 = node2;
printf ("\nDo you want to continue?(y/n) \n");
getchar();
scanf ("%c", &choice);
}while (choice == 'y' || choice == 'Y');
return head;
}
//插入
int insert (DATA *head) {
char choice;
DATA *node, *node_insert;
node = head->next;
printf ("\n insert: \n");
do {
node_insert = malloc(N);
printf ("\nPlease input the data you want to insert:\n");
scanf ("%d", &node_insert -> data);
printf ("%d", node_insert -> data);
while (node != head && node_insert -> data < node -> data) {
node = node->next;
}
node_insert->pro = node->pro;
node->pro->next = node_insert;
node_insert->next = node;
node->pro = node_insert;
printf ("\nDo you want to continue?(y/n)");
getchar();
scanf ("%c", &choice);
}while (choice == 'y' || choice == 'Y');
return 0;
}
//删除 类比插入自行推理~
小编提示:不要忘记在创建在开始创建一个空的双向循环链表,小编之前就忘记了,结果出现大大的bug,后来,请教了小编的老师,才知道的。之前在网上搜索的感觉好像看不懂,而且还有bug,给老师看得时候,老师说“你连单向的都不清楚,先用单向吧”,所以,网上的程序,你们懂得,小编就不多说了,但是,小编的一定时=是正确的哦。还有,就是在交换的时候,注意指针的指向,最好画画图,搞清楚,不要绕晕啦,之前小编就晕呢。