好贷网好贷款

编程实现单链表的逆置

发布时间:2016-12-4 10:04:41 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"编程实现单链表的逆置",主要涉及到编程实现单链表的逆置方面的内容,对于编程实现单链表的逆置感兴趣的同学可以参考一下。

#include <tchar.h> #include <iostream> using namespace std; typedef struct student{ int data; struct student * next; } node; node* create() { node *head, *p, *s; int x, cycle = 1; head = (node *)malloc(sizeof(node)); p = head; while(cycle) { cout << "please input the data: "; cin >> x; cout << endl; if( x != 0) { s = (node *)malloc(sizeof(node)); s->data = x; p->next = s; p = s; } else { cycle = 0; } } p->next = NULL; head = head->next; return head; } int length(node *head) { int n = 0; node *p; p = head; while (p != NULL) { p = p->next; n++; } return n; } void print(node *head) { int n; node *p; p = head; n = length(head); cout << "There is " << n << " data in list\n" << endl; while(p != NULL) { cout << p->data << " -> "; p = p->next; } cout << endl; } node *reverse(node *head) { node *p1, *p2, *p3; if(NULL == head && NULL == head->next) return head; p1 = head; p2 = p1->next; while(p2) { p3 = p2->next; p2->next = p1; p1 = p2; p2 = p3; } head->next = NULL; head = p1; return head; } int _tmain(int argc, _TCHAR * argv[]) { node *head; head = create(); print(head); head = reverse(head); print(head); return 0; }

上一篇:C# 发送邮件(内容中可以插入图片,邮件可以添加附件,163邮箱、QQ邮箱等都可以使用)
下一篇:Unity3D自带寻路学习笔记

相关文章

相关评论