普通单向链表的翻转

来源:互联网 发布:gta4优化怎么样 编辑:程序博客网 时间:2024/05/16 05:49

代码如下:

// ReverseLink3.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;typedef struct LinkNode {int data;struct LinkNode* next;}Node;Node* creatate_Link(int num){Node* head,*p,*q;p=new Node();p->data=1;head=p;for(int i=2;i<=num;i++){q=new Node();q->data=i;p->next=q;p=q;}p->next=NULL;return head;}void printf_Link(Node* head){while (head!=NULL){cout<<head->data;head=head->next;}}Node* ReverseLink(Node* head){Node* cur=head;Node* newNode=NULL;while (cur!=NULL){head=cur->next;cur->next=newNode;newNode=cur;cur=head;}return newNode;}int _tmain(int argc, _TCHAR* argv[]){Node* head=creatate_Link(8);printf_Link(head);cout<<"链表翻转后"<<endl;Node* head1=ReverseLink(head);printf_Link(head1);cout<<endl;system("pause");return 0;}


结果见图: