8 12 56 4 6 55 15 33 62
12 56 4 6 55 15 33 62
写的时候,定义一个结构体变量就要为它申请空间,不然程序会运行时出问题 !
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <algorithm>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n;
int i, j;
struct node *head;
head=(struct node*)malloc(sizeof(struct node)); // 重点
head->next=NULL;
struct node *tail, *p;
tail=head;
cin>>n;
for(i=0; i<n; i++)
{
p=(struct node *)malloc(sizeof(struct node)); // 重点
scanf("%d", &p->data );
p->next=NULL;
tail->next=p;
tail=p;
}
for(j=0; j<n; j++)
{
if(j==0)
cout<<head->next->data;
else
cout<<" "<<head->next->data;
head=head->next;
}
cout<<endl;
return 0;
}
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <algorithm>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n;
int i, j;
struct node *head;
head= new struct node; // 用C++的new 申请空间也行!
head->next=NULL;
struct node *tail, *p;
tail=head;
cin>>n;
for(i=0; i<n; i++)
{
p=new struct node;
scanf("%d", &p->data );
p->next=NULL;
tail->next=p;
tail=p;
}
for(j=0; j<n; j++)
{
if(j==0)
cout<<head->next->data;
else
cout<<" "<<head->next->data;
head=head->next;
}
cout<<endl;
return 0;
}
原文:http://www.cnblogs.com/yspworld/p/4089866.html