/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
class node{
public:
int data;
node *link;
};
void reverse_list(node *s);
void print_list(node *s);
int main()
{
node *t=new node();
t->link=NULL;
t->data=1;
for(int i=2;i<4;i++)
{
node *y=new node();
y->data=i;
y->link=t;
t=y;
}
print_list(t);
printf("Reverse the list t:\n");
reverse_list(t);
print_list(t);
return 0;
}
void reverse_list(node *s)
{
node *x=new node();
node *p=new node();
node *q=new node();
p=x=s; //p, x都初始化為list頭s
q=NULL; //q初始化為list的bottom=Null
while(p->link!=NULL)
{
x->link=q; //x指回前一個node q
q=p; //q變成p
p=p->link; //p再往後搜索一格
x=p; //x也是p
}
s=x;
}
void print_list(node *s)
{
while(s->link!=NULL)
{
printf("address=%p ",s);
printf("data=%d ",s->data);
printf("nextpoint=%p \n",s->link);
s=s->link;
}
printf("address=%p ",s);
printf("data=%d ",s->data);
printf("nextpoint=%p \n",s->link);
}