无头单链表逆序(法二)
2013-02-27 09:16
197 查看
#include "stdio.h" #include "stdlib.h" #include "time.h" typedef struct node{ int a; struct node* next; }LNODE; LNODE* LinklistCreate(void); void LinklistVisit(LNODE *head); LNODE* LinklistConvert(LNODE *); int main(void) { LNODE *head = NULL; head = LinklistCreate(); if(head == NULL) return -1; printf("before convert:\n"); LinklistVisit(head); head = LinklistConvert(head); printf("after convert:\n"); LinklistVisit(head); return 0; } LNODE* LinklistCreate(void) { int num = 0,i = 0; LNODE *head = NULL,*pf = NULL,*pb = NULL; printf("How many nodes do you want?\n"); scanf("%d",&num); if(num < 1) return NULL; for(i = 0;i < num;i++) { pf = (LNODE*)malloc(sizeof(LNODE)); if(pf == NULL) { if(i == 0) return NULL; else { for(;i > 0;i--) { pf = head->next; free(head); head = pf; } return NULL; } } if(i == 0) { head = pf; pb = pf; } else { pb->next = pf; pb = pf; } } pf->next = NULL; srand(time(NULL)); pf = head; do{ pf->a = rand()%100; }while(pf = pf->next); return head; } void LinklistVisit(LNODE *head) { if(head == NULL) return ; do{ printf("%d\n",head->a); }while(head = head->next); } LNODE* LinklistConvert(LNODE *head) { LNODE *pf = NULL,*tail = NULL; LNODE *newhead = NULL; if(head == NULL) return NULL; while(tail != head) { pf = head; while(pf->next != tail) pf = pf->next; if(tail == NULL) newhead = pf; else tail->next = pf; tail = pf; } tail->next = NULL; return newhead; }
相关文章推荐
- 链表面试题逆序打印,删除无头非尾节点,非头位置插入,约瑟夫环,查找中间和倒数第k个节点
- 链表面试题(一):逆序打印链表、无头链表删除插入节点、约瑟夫环、逆置单链表
- 无头单链表逆序(法一)
- 链表逆序的原理及实例
- 链表逆序输出
- 将一个链表逆序
- 【面试题】剑指offer05--逆序打印链表
- 第八十九题(链表逆序,atoi函数,快速排序)
- 逆序链表查询
- 数据结构实验之链表二:逆序建立链表
- 【单链表】逆序
- 单链表逆序的递归与非递归算法
- 无头单链表的非尾节点删除
- 2. Add Two Numbers 给定的两个链表是逆序排列的,相加后放在一个新的链表里边
- 单链表应用——逆序输出单链表中的元素
- 2117数据结构实验之链表二:逆序建立链表
- 单链表逆序2
- (单链表)单链表的整体逆序和局部逆序
- 单链表中的冒泡排序(无头节点)
- 逆序建链表