链表翻转

来源:互联网 发布:斑马网络计划破解软件 编辑:程序博客网 时间:2024/06/05 06:51
#include<iostream>using namespace std;typedef struct head{int a;struct head *next;}NLode;void intial(NLode **head){*head=(NLode *)malloc(sizeof(NLode));(*head)->next=NULL;}void insert(NLode *head,int a){NLode *p,*q;p=head;q=(NLode*)malloc (sizeof(NLode));while(p->next!=NULL){p=p->next;}q->a=a;q->next=p->next;p->next=q;}NLode *  reserve(NLode  *head){NLode *p,*q,*r;p=head;q=head->next;p->next=NULL;while(q!=NULL){r=q;q=q->next;r->next=p;p=r;}return r;}void printSL(NLode *head){NLode *p;p=head->next;while(p!=NULL){cout<<p->a<<"   ";p=p->next;}}void printSLR(NLode *head){while(head!=NULL&&head->next!=NULL){cout<<head->a;head=head->next;}}void main(){NLode *head1,*head2,*head3,*head4; intial(&head1);int a,b,num=0,i;cin>>a;for(i=0;i<a;i++){cin>>num;insert(head1,num);} cout<<"A:"<<endl;printSL(head1);cout<<endl;cout<<"B:"<<endl;head4=reserve(head1);printSLR(head4);system("pause");}

0 0
原创粉丝点击