请看这个:
#define NULL 0
struct student
{long num;
float score;
struct student *next;
};
main()
{struct student a,b,c,*head,*p;
a.num=111;a.score=88;
b.num=222;b.score=99;
c.num=333;c.score=77;
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do{
printf"%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
这个是谭爷爷书上的原文;“本例比较简单,所有的接点都是在程序中定义,不是临时开辟的,也不能用完后释放,这种链表称为‘静态链表’”;
你们说这个话对吗?
|
"
///////////////////////////////////////////////////////
//实现由终端输入集合A,B实现集合运算(A-B)U(B-A)
//类c语言描述
///////////////////////////////////////////////////////
#define MAXSIZE 1000
typedef struct {
int data;
int cur;
}component,SLinkList[MAXSIZE];
//将一维数组space中各分量链成一个备用链表,space[0].cur为头指针,‘0’表是空指针
void InitSpace_SL(SLinkList &space) {
int t;
for(t=0;t<MAXSIZE-1;++t)
space[m].cur=t+1;
space[MAXSIZE-1].cur=0;
}
//若备用空间链表非空,返回分配的接点下标,否则返回0
int Malloc_SL(SLinkList &space){
int t;
t=space[0].cur;
if(space[0].cur)
space[0].cur=space[t].cur;
return t;
}
//将下表为k的空闲接点回收到备用链表
void Free_SL(SLinkList &space,int k) {
space[k].cur=space[0].cur;
space[0].cur=k;
}
//依次输入集合a和b的元素,在space中建立表示集合(a-b)U(b-a)的静态
//链表,s为其头指针。假设备用空间足够大,space[0].cur为其头指针。
void difference(SLinkList &space,int &S){
int m,n,f,k,t,j,b,r,p;
InitSpace_SL(space);
S=Malloc_SL(space);
r=S;
printf("Enter the number item of A and B\n");
scanf("%d %d",&m,&n);
for(j=1;j<m;++j){
m=Malloc_SL(space);
printf("Enter the item of A:\n");
scanf("%d",&space[m].data);
space[r].cur=t;
r=t;
}
space[r].cur=0;
for(j=1;j<=n;++j){
printf("Enter the item of B\n");
scanf("%d",&b);
while(k!=space[r].cur&&space[k].data!=b) {
p=k;
k=space[k].cur;
}
if(k==space[r].cur) {
t=Malloc_SL(space);
space[t].data=b;
space[t].cur=space[r].cur;
space[r].cur=t;
}
else {
space[p].cur=space[k].cur;
Free_SL(space,k);
if(r==k)
r=p;
} //else
} //for
} //difference
说明:举个例子A=(c,b,e,g,f,d),B=(a,b,n,f)
运算前:
space(0:11)
0 8
1 2
2 c 3
3 b 4
4 e 5
5 g 6
6 f 7
7 d 0
8 9
9 10
10 11
11 0
运算后:
space(0:11)
0 6
1 2
2 c 4
3 n 8
4 e 5
5 g 7
6 f 9
7 d 3
8 a 0
9 10
10 11
11 0
"
|