#include <stdio.h> #include <stdlib.h> //单向循环链表 typedef struct node { int data; struct node *next; }node; //创建头节点 node *add_head() { node *Head = (node *)malloc(sizeof(node)); if(Head == NULL) return NULL; Head->next = Head; return Head; } //尾插法 void add_node(node *Head,int data) { node *new = (node*)malloc(sizeof(node)); if(new == NULL) return; //节点成员赋值 new->data = data; new->next = NULL; //链接 node *pT = NULL; for(pT = Head;pT->next != Head;pT = pT->next); new->next = pT->next; pT->next = new; } //节点交换 void swap_node(node *Head) { node *p1 = Head->next;//第一个节点 node *p2 = Head->next->next;//第二个节点 //前两个节点交换 p1->next = p2->next; p2->next = p1; Head->next = p2; node *pT = NULL; node *pT1 = NULL; node *pT2 = NULL; for(pT = Head;pT->next->next->next != Head;pT = pT->next);//找到倒数第三个节点 for(pT1 = Head;pT1->next->next != Head;pT1 = pT1->next);//倒数第二个 for(pT2 = Head;pT2->next != Head;pT2 = pT2->next);//最后一个节点 //后两个节点交换 pT1->next = Head; pT2->next = pT1; pT->next = pT2; } //输出链表 void output(node *Head) { if(Head->next == Head) return; node *pT = Head->next; while(pT != Head) { printf("%d ",pT->data); pT = pT->next; } } int main(void) { node *Head = add_head();//头节点 int n,i,j; scanf("%d",&n); int arr[n]; //将键盘键入的数据存放到数组中 for(i = 0;i < n;i++) scanf("%d",&arr[i]); //将数据插入链表 for(j = 0;j < n;j++) add_node(Head, arr[j]); swap_node(Head); output(Head); return 0; }