交换单链表中相邻的两个点(人搜)
2015-01-20 23:19
155 查看
/************************************************************* * file:reverse_twins_node_of_list.c * brief:交换单链表中相邻的两个点 * yejing@2015.1.20 1.0 creat *************************************************************/ #include <stdio.h> #include <stdlib.h> typedef struct _node_t{ struct _node_t *next; int value; }node_t; node_t* creat_list(int n){ if(!n) return NULL; int i = 1; node_t* phead = (node_t*)malloc(sizeof(node_t)); phead->next = NULL; phead->value = 1; if(!phead) return NULL; node_t *p, *q; p = q = phead; for(i = 1; i < n; ++i){ q = (node_t*)malloc(sizeof(node_t)); p->next = q; p = q; p->value = i + 1; } return phead; } node_t* reverse_twins_node_of_list(node_t* phead){ if(!phead) return NULL; if(!phead->next) return phead; int i = 2; node_t* n = phead; phead = phead->next->next; while(i){ node_t* m = n; n = n->next; m->next = phead; phead = m; --i; } phead->next->next = reverse_twins_node_of_list(phead->next->next); return phead; } void show_list(node_t* phead, int before_after){ if(!phead) return; node_t* tmp = phead; if(before_after) printf("before transfer "); else printf("after transfer "); printf("list is:"); while(tmp){ printf(" %d", tmp->value); tmp = tmp->next; } printf("\n"); return; } int main(int argc, char* argv[]){ int count; printf("input list node count:\n"); scanf("%d", &count); getchar(); node_t* phead = creat_list(count); show_list(phead, 1); phead = reverse_twins_node_of_list(phead); show_list(phead, 0); return 1; }执行结果:
相关文章推荐
- 单链表相邻两个节点之间交换顺序
- 只调整指针来交换两个相邻的元素【双链表】
- Swap Nodes in Pairs(交换链表相邻的两个结点)
- 交换单链表中相邻的两个元素 Swap Nodes in Pairs
- 给定一个链表,交换每两个相邻节点并返回其头部。
- java 交换链表中所有相邻结点的顺序
- 建立单链表并交换表中任意两个元素
- 交换链表中的相邻节点
- [leetcode] 24 Swap Nodes in Pairs(交换链表相邻结点)
- leetcode交换相邻的两个节点的值
- 反转链表中相邻两个元素
- 单链表交换相邻元素
- 交换链表中两个节点
- 7-6 冒泡法排序(20 分) 将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们。通过一遍扫描,则最后一个元素必定是最大的元素
- LINTCODE——交换链表当中两个节点
- p62 练习3.3 通过只调整指针(而不是数据)来交换两个相邻的元素,使用:
- 练习 3.3 通过只调整指针(不是数据)来交换两个相邻的元素
- 【LeetCode91-100】编码种数,逆转单链表,IP地址转化,中序遍历二叉树,生成二叉搜索树,计算二叉树个数,交叉string【hard】,判断二叉搜索树是否合法,恢复二叉树(有两个元素被交换)
- leetcode24,交换链表相邻的节点
- 24. Swap Nodes in Pairs(每两个相邻节点进行一次交换)