#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct Node{
int data;
struct Node *next;
}Node,*LinkList;
bool InitList(LinkList &L){
L=(Node*)malloc(sizeof(Node));
if(L==NULL) return false;
L->next=NULL;
printf("InitList Successful!\n");
return true;
}
bool ListInsert(LinkList &L,int e){
Node *s,*p=L;
s=(Node*)malloc(sizeof(Node));
if(s==NULL) return false;
while(p->next!=NULL){
p=p->next;
}
s->data=e;
p->next=s;
s->next=NULL;
return true;
}
bool PrintList(LinkList L)
{
Node *p=L->next;
if(p==NULL) return false;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}return true;}
void ListReverse(LinkList &L){
Node *r,*p=L->next;
L->next=NULL;
while(p!=NULL){
r=p->next;
p->next=L->next;
L->next=p;
p=r;
}
}
int main() {
LinkList L;
InitList(L);
int a[10]={1,2,3,4,5,6,7,8,9,10},i=0;
while(i<(sizeof(a)/4))
ListInsert(L,a[i++]);
PrintList(L);
ListReverse(L);
printf("\n");
PrintList(L);
return 0;
}
|