c程序用数组实现顺序表的建立,并计算单链表的长度,急啊 在先等

如题所述

第1个回答  2011-12-08
#include<stdio.h>
#include<stdlib.h>
struct cell
{
int n; //数据项
struct cell *next;
} *top=NULL;
typedef struct cell link;
link *creat(int n) //入队算法建立链表
{
link *p,*p0;
top=(link*)malloc(sizeof(link));
printf("输入数据 ");
scanf("%d",&top->n);
top->next=NULL;
p0=top;
while(--n)
{
p=(link*)malloc(sizeof(link));
printf("输入数据 ");
scanf("%d",&p->n);
p0->next=p;
p->next=NULL;
p0=p;
}
return top;
}
void printl(link *head) //输出链表
{
int count=0;
link *p0,*p;
p=head;
while(p!=NULL)
{
p0=p;
printf("%d\n",p->n);
count++; //链表长度
p=p->next;
}
printf("链表长度=%d\n",count);
}

void main()
{
int n;
printf("链表长度=");
scanf("%d",&n);
top=creat(n);
printl(top);
}
第2个回答  2011-12-08
到底是顺序表还是链表啊本回答被提问者采纳
相似回答