C语言中没有malloc函数的链接列表

时间:2013-06-04 14:45:12

标签: c data-structures linked-list malloc

如何在不使用malloc的情况下使用此程序?如果我使用temp.next=&newtemp程序工作没有错误?我必须学习如何避免malloc。

    for(int ;;){
    printf("Fusni N(numrin e qyteteve): "); scanf("%d", &N);
    if(N<13 || N>100) {printf("Futet N gabim\t( 13 < N < 100)\n\n");continue;}
M=1;
while(1){
p = q = malloc(sizeof(struct node));

    p->id_qyteti = 1;

    for (i = 2; i <= N; ++i) {
        p->pas = malloc(sizeof(struct node));
        p = p->pas;
        p->id_qyteti = i;
    }

    p->pas = q;
 printf("M=%d, Zonat ku nderpritet rryma:\n", M);

for (count = N; count > 1; --count) {
 printf("%d ",p->pas->id_qyteti);   p->pas = p->pas->pas;
if(count==2)printf("\n\n");
      for (i = 0; i < M - 1; ++i) p = p->pas;
}
if(p->id_qyteti==13)

 {
printf("13.Tirana ngelet e fundit\n");
   printf("M-ja minimale: %d\n", M);
   break;

 }
else M++;

}}
return 0;
}

1 个答案:

答案 0 :(得分:2)

您可以将它与数组一起使用,但问题是您的容量有限。 例如,你可以有一个struct yourstruct[500];和一个整数来显示下一个自由结构的位置,你可以做类似mynode->next = &yourstruct[i++]的事情,当然你必须有一个更好的控制来知道自由结构的位置是。