一,建立正序链表
`#include
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
node *head=NULL,*p,*s;
s=new node;
cin>>s->data;
head=s;
p=s;
while(p->data!=0)
{
s=new node;
cin>>s->data;
p->next=s;
p=s;
}
p->next=NULL;
p=head;
while(p->next!=NULL)
{
cout<data<<” “;
p=p->next;
}delete p;
return 0;
}`
二、建立逆序链表
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
node *head=NULL,*p;
p=new node;
cin>>p->data;
while(p->data!=0)
{
if(head==NULL)
{
p->next=NULL;
head=p;
}
else
{
p->next=head;
head=p;
}
p=new node;
cin>>p->data;
}
delete p;
p=head;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
return 0;
}