赞
踩
链表均带有头结点
#include <bits/stdc++.h> using namespace std; typedef struct LNode{ int data; struct LNode *next; }LNode,*LinkList; void initlist(LinkList &L) { L=(LNode*)malloc(sizeof(LNode)); L->next=NULL; LNode *s,*p; p=L; int x; scanf("%d",&x); while(x!=-1){ s=(LNode*)malloc(sizeof(LNode)); s->data=x; s->next=NULL; p->next=s; p=s; scanf("%d",&x); } } void printlist(LinkList &L) { LNode *cur; if(L==NULL||L->next==NULL) return; cur=L->next; while(cur!=NULL){ cout<<cur->data<<" "; cur=cur->next; } cout<<endl; } void reverse(LinkList &L) { if(L==NULL||L->next==NULL) return; LNode *p1,*p2,*p3; p1=L->next; p2=p1->next; while(p2!=NULL){ p3=p2->next; p2->next=p1; p1=p2; p2=p3; } L->next->next=NULL; L->next=p1; } int main() { LinkList L; initlist(L); printlist(L); reverse(L); printlist(L); return 0; }
#include <bits/stdc++.h> using namespace std; typedef struct LNode{ int data; struct LNode *next; }LNode,*LinkList; void initlist(LinkList &L) { L=(LNode*)malloc(sizeof(LNode)); L->next=NULL; LNode *s,*p; p=L; int x; scanf("%d",&x); while(x!=-1){ s=(LNode*)malloc(sizeof(LNode)); s->data=x; s->next=NULL; p->next=s; p=s; scanf("%d",&x); } } void printlist(LinkList &L) { LNode *cur; if(L==NULL||L->next==NULL) return; cur=L->next; while(cur!=NULL){ cout<<cur->data<<" "; cur=cur->next; } cout<<endl; } void reverse(LinkList &L) { if(L==NULL||L->next==NULL) return; LNode *p,*q; p=L->next; q=p->next; while(q!=NULL) { p->next=q->next; q->next=L->next; L->next=q; q=p->next; } } int main() { LinkList L; initlist(L); printlist(L); reverse(L); printlist(L); return 0; }
#include <bits/stdc++.h> using namespace std; typedef struct LNode{ int data; struct LNode *next; }LNode,*LinkList; void initlist(LinkList &L) { L=(LNode*)malloc(sizeof(LNode)); L->next=NULL; LNode *s,*p; p=L; int x; scanf("%d",&x); while(x!=-1){ s=(LNode*)malloc(sizeof(LNode)); s->data=x; s->next=NULL; p->next=s; p=s; scanf("%d",&x); } } void printlist(LinkList &L) { LNode *cur; if(L==NULL||L->next==NULL) return; cur=L->next; while(cur!=NULL){ cout<<cur->data<<" "; cur=cur->next; } cout<<endl; } LinkList reverse(LinkList &head) { if(head==NULL||head->next==NULL) return head; LinkList new_head=reverse(head->next); head->next->next=head; head->next=NULL; return new_head; } int main() { LinkList L; initlist(L); printlist(L); L->next=reverse(L->next); printlist(L); return 0; }
#include <bits/stdc++.h> using namespace std; typedef struct LNode{ int data; struct LNode *next; }LNode,*LinkList; void initlist(LinkList &L) { L=(LNode*)malloc(sizeof(LNode)); L->next=NULL; LNode *s,*p; p=L; int x; scanf("%d",&x); while(x!=-1){ s=(LNode*)malloc(sizeof(LNode)); s->data=x; s->next=NULL; p->next=s; p=s; scanf("%d",&x); } } void printlist(LinkList &L) { LNode *cur; if(L==NULL||L->next==NULL) return; cur=L->next; while(cur!=NULL){ cout<<cur->data<<" "; cur=cur->next; } cout<<endl; } LinkList reverse(LinkList &L) { if(L==NULL||L->next==NULL) return NULL; LinkList new_L; new_L=(LinkList)malloc(sizeof(LNode)); new_L->next=NULL; int data; LNode *p,*s; p=L->next; while(p!=NULL) { data=p->data; s=(LNode*)malloc(sizeof(LNode)); s->data=data; s->next=new_L->next; new_L->next=s; p=p->next; } return new_L; } int main() { LinkList L; initlist(L); printlist(L); LinkList new_L=reverse(L); printlist(new_L); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。