1。新建节点,把当前扫描的原始链表节点,插入新链表节点之前
#include <cstdlib>
#include <iostream>
using namespace std;
typedef struct node * pnode;
struct node
{
int data;
pnode next;
};
void add(pnode &head,pnode &tail)
{
int num;
cin>>num;
if(head==NULL)
{
head=new node;
head->data=num;
head->next=NULL;
tail=head;
return ;
}
pnode tmp=new node;
tmp->data=num;
tmp->next=NULL;
tail->next=tmp;
tail=tmp;
return ;
}
void travel(pnode &head)
{
pnode tmp=head;
while(tmp!=NULL)
{
cout<<tmp->data<<endl;
tmp=tmp->next;
}
}
void turntravel(pnode head)
{
pnode newlist=NULL;
pnode tmp=head;
while(tmp!=NULL)
{
pnode res=new node;
res->data=tmp->data;
res->next=newlist;
newlist=res;
tmp=tmp->next;
}
cout<<newlist<<endl;
travel(newlist);
}
int main(int argc, char *argv[])
{
pnode tail=NULL;
pnode list=NULL;
while(1)
{
int op;
cin>>op;
if(op==1)
{
add(list,tail);
}
else break;
}
travel(list);
turntravel(list);
system("PAUSE");
return 0;
}