C 链表的反转(数据结构与算法)
2013-07-06 13:20
190 查看
建立一个链表,然后将其中的元素进行反转。
#include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *next; }; void display_link(struct Node *head) { struct Node *p; for(p = head; p != NULL; p = p->next) { printf("%4d",p->data); } printf("\n"); } struct Node * new_link() { struct Node *head = NULL, *p1 = NULL ,*p2 = NULL; int data; int i = 1; printf("enter the data:\n"); p1 = (struct Node * )malloc(sizeof(struct Node)); while(1) { scanf("%d",&p1->data); if(head == NULL) { head = p1; } p2 = p1; if(i == 10) { break; } p1 = (struct Node*)malloc(sizeof(struct Node)); p2->next = p1; i++; } p2->next = NULL; return head; } struct Node *transvert_link(struct Node *head) { struct Node *p,*p2,*p3; p = head; p2 = head->next; p3 = head->next->next; while(p3 != NULL) { p2->next = p; p = p2; p2 = p3; p3 = p3->next; } p2->next = p; head->next = NULL; return p2; } int main(void) { struct Node *head; head = new_link(); printf("\n the data before transvert is:\n"); display_link(head); head = transvert_link(head); printf("\n the data after transvert is:\n"); display_link(head); return 0; }
相关文章推荐
- 数据结构与算法(c++)——反转链表
- 数据结构与算法之反转链表
- 数据结构与算法(2)---Java语言实现:线性表的单链表定义:方法补充,实现单链表反转,去重
- 数据结构与算法学习笔记(二)反转链表
- 数据结构与算法笔记(三)反转部分链表
- C++数据结构与算法之反转链表的方法详解
- 剑指offer——面试题16:反转链表
- Java单双链表的创建、反转与递归反转
- 线性复杂度反转单向链表
- 【链表】将一个链表反转
- 单链表中结点删除、排序与反转
- 反转链表
- 利用Collections类操作链表(排序,反转等)
- 考研数据结构与算法(3)----有头节点和尾节点的链表
- 输入一个链表,反转链表后,输出链表的所有元素。
- 单向链表的反转最简单方法
- 单链表反转
- 【WG面试算法】链表反转
- [LeetCode-92] Reverse Linked List II(反转指定区间链表)
- 剑指offer-面试题16-反转链表