链表逆转
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct LNode {
int data;
struct LNode *next;
}LNode,*LinkList;
void Init(LinkList &L){
L = (LinkList)malloc(sizeof(int));
L->next = NULL;
}
void Insert(LinkList &L){
LinkList temp = L;
int n,k=0;
cout<<"输入添加节点个数\n";
cin>>n;
while(k<n){
cout<<"插入数据\n";
int e=0;
cin>>e;
LinkList s = new LNode;
s->data = e;
s->next = NULL;
temp->next = s;
temp = s;
k++;
}
}
void print(LinkList p){
while(p->next !=NULL){
p = p->next;
printf("%d",p->data);
}
}
void reverse(LinkList &L){
LinkList New,old,temp;
New = L->next;
old = L->next->next;
temp = L->next->next->next;
while(temp!=NULL){
old->next = New;
New = old;
old = temp;
temp = temp->next;
}
old->next = New;
L->next->next = NULL;
L->next = old;
}
int main(){
LinkList L;
Init(L);
Insert(L);
print(L);
reverse(L);
cout<<"\n";
print(L);
return 0;
}