#include<iostream>
using namespace std;
struct node{
int d;
struct node *next;
};//定义结点
node *build1()//头插法构造单链表
{
node *p;//指向新建结点
node *head;//头指针
head=NULL;
p=head;
int x;
cin>>x;
while(x!=-1)
{
p=new node;
p->d=x;
p->next=head;
head=p;
cin>>x;
}
return head;
}
node *build2()//尾插法构造单链表
{
node *head;//头指针
node *p,*s;//p指向当前结点,s指向尾结点
head=NULL;
p=head;
s=head;
int x;
cin>>x;
while(x!=-1)
{
p=new node;
p->d=x;
if(!head)head=p;
else
s->next=p;
s=p;
cin>>x;
}
if(s)s->next=NULL;
return head;
}
int main()
{
node *p;
p=build2();
while(p!=NULL)
{
cout<<(p->d);
p=p->next;
}
return 0;
}本文出自 “7883538” 博客,请务必保留此出处http://7893538.blog.51cto.com/7883538/1435486
原文地址:http://7893538.blog.51cto.com/7883538/1435486