//链表的建立和输出问题示范:
#include
typedef struct LNode//类型定义
{
int data;
struct LNode *next;
}LNode,*LinkList;
void createList1(LinkList &L,int n)//逆位序创建
{
L=(LinkList)malloc(sizeof(LNode));//为头结点申请空间
L->next=NULL;//请看插入操作的第一条语句,这个空指针最后到了链表的末尾
LinkList p;
for(int i=0;i
p=(LinkList)malloc(sizeof(LNode));//为新结点申请空间并输入数据以初始化
printf("请输入第%d个数据:\n",(i+1));
scanf("%d",&p->data);
p->next=L->next;//插入操作
L->next=p;
}
}
void createList2(LinkList &L,int n)//顺位序创建
{
L=(LinkList)malloc(sizeof(LNode));//为头结点申请空间
LinkList tail;//头结点要保存起来,使用tail替代;
tail=(LinkList)malloc(sizeof(LNode));//为tail申请空间;
tail=L;//开始L即为tail;
LinkList p;
for(int i=0;i
p=(LinkList)malloc(sizeof(LNode));//为新结点申请空间并输入数据以初始化
printf("请输入第%d个数据:\n",(i+1));
scanf("%d",&p->data);
tail->next=p;//插入操作
tail=tail->next;
}
tail->next=NULL;//最后tail->next指向NULL;
}
void inputList(LinkList L)//链表遍历
{
while(L->next)
{
LinkList p;
p=(LinkList)malloc(sizeof(LNode));
p=L->next;
printf("%d\t",p->data);
L=L->next;
}
}
void main()
{
LinkList L;
createList2(L,10); //这里使用顺位序创建
inputList(L);
}
#include "stdio.h"
#include "stdlib.h"
int main()
{
struct fac
{
int a;
struct fac *next;
}*head,*tail,*p;
int value,i=0,n,j;
FILE *fp=fopen("d;\\ctest\\stu15","ab+");
head=tail=NULL;
printf("Input the number:");
scanf("%d",&value);
while(value!=0)
{
p=(struct fac*)malloc(sizeof(struct fac));
p->a=value;
p->next=NULL;
if(head==NULL)
{
head=tail=p;
}
else
{
tail->next=p;
p=tail;
}
fwrite(p,sizeof(struct fac),1,fp);
i++;
printf("Input the number:");
scanf("%d",&value);
}
FILE *fpn=fopen("d:\\ctest\\stu15_n.txt","w");
fprintf(fpn,"%d",i);
fclose(fp);
fclose(fpn);
fp=fopen("d;\\ctest\\stu15","rb");
fpn=fopen("d:\\ctest\\stu15_n.txt","r");
fscanf(fpn,"%d",&n);
for(j=0;j
fread(p,sizeof(struct fac),1,fp);
printf("%d\n",p->a);
}
return 0;
}
两个问题:
1、头文件错了,应该用stdio.h,用不到stdafx.h。而且,一般用<>括起函数库来就可以了。""也可以。但是二者是有区别的。
2、int main类型的函数,最后掉了一个返回值。或者你直接写void mian(当然c++里面要求最好用int main和return 0)。