C语言:链表的逆序,两种思考方法,第一种用rev_1()实现,第二种用rev_2( )实现.
来源:互联网 发布:hadoop搭建大数据平台 编辑:程序博客网 时间:2024/05/29 19:26
#include <stdio.h>#include <stdlib.h>struct s{int n;struct s *next;};int i;struct s *creat(int m){struct s *head, *p, *p1;head = NULL; for(i=0; i<m; i++){p = (struct s*)malloc(sizeof(struct s));printf("Num %d: ", i+1);scanf("%d", &p->n);if(i==0)head = p;elsep1->next = p;p1 = p;}p1->next =NULL;return head;}struct s *rev_1(struct s *head)//第一种逆序方法{struct s *t, *p;p = head;head = NULL;if(p == NULL || p->next==NULL){return p;}while(p){t = p;p = p->next;t->next = head;head = t;}return head;}struct s *rev_2(struct s *head)//第二种逆序方法{struct s *t, *p, *q;p = head;q = head->next;t = NULL;if(head==NULL || head->next==NULL){return head;}while(q){t = q->next;q->next = p;p = q;q = t;}head->next = NULL;return p;}void output(struct s *head){int i=0;while(head){printf("Num %d: %d \n",i+1, head->n);head = head->next;i++;}printf("\n");}void main(){struct s *head;int m;printf(" \nPlease input the list nodes number: ");scanf("%d", &m);head = creat(m);printf(" \nOutput the list\n");output(head);printf(" \nOutput the list of reverse\n");head = rev_1(head);output(head);printf(" \noutput the list of reverse\n");head = rev_2(head);output(head);}
0 0
- C语言:链表的逆序,两种思考方法,第一种用rev_1()实现,第二种用rev_2( )实现.
- C语言:链表的循环,两种思考方法,第一种用select_1()实现,第二种用select_2()实现.
- 用不同的方法实现字符串的逆序输出(C语言实现)
- C语言 实现两种排序方法
- c语言strstr实现的两种方法
- C语言实现对半法查找的两种方法
- 实现数组逆序排列的两种方法
- 用C语言实现两变量内容交换的N种方法
- 两种方法实现单向链表的创建、遍历、删除、查找、逆序输出(循环法和递归法)
- 进制转换的一种思路两种实现方法
- 用c语言实现单链表的逆序输出
- 文件输入输出的两种方法(常用第一种)
- 一种用C语言实现的打印程序源码的方法
- C语言中的两种hash表的实现
- c语言:两种方法实现给定一个大写字母,用小写字母输出
- C语言两种方法实现字符串反转
- C语言两种方法实现进程间 socket 通信
- c语言:两种方法实现字符串拷贝strcpy
- C++ 用libcurl库进行http 网络通讯编程
- Android应用版本更新
- 【XCode中变更 resource文件后,需要重新编译才能正确识别更新】问题的解决方案
- 七年之痒
- POJ 1990 MooFest 树状数组
- C语言:链表的逆序,两种思考方法,第一种用rev_1()实现,第二种用rev_2( )实现.
- [Unity 3D] Quaternion.LookRotation
- 链队的创建、入队、出队
- 请问java.util.ConcurrentModificationException怎么解决啊
- c++静态库与动态库的区别
- Cocos2D-x权威指南: CCNode类方法:
- 使用httpclient无需证书调用https的示例(java调用https)
- dll 导出函数名的那些事
- NS3之first.cc注解