Вообщем такие пироги пацаны.
Лаба звучит так :
Дан указатель на список. Определите количество элементов в списке.
Мы с корешем вот написали, но я не уверен правильно ли все это и выполнена ли задача. Проверьте плз и посмотрите есть ли косяки.
Код:
#include "stdio.h"
#include "stdlib.h"
struct List {
int number;
struct List *next;
};
struct List *first, *cure;
void insel(struct List *cure, int number);
int count(struct List *cure);
void main()
{
int n = 0, x = 0, k = 0;
printf("\nVvedite 4islo elementov\n");
if (scanf("%d",&n) == 0)
{
printf("Epic fail!");
return;
}
first = (struct List*)malloc(sizeof(struct List));
printf("Enter the first element\n");
scanf("%d",&first->number);
printf("\nFirst element = %d\n",first->number);
first->next = NULL;
cure = first;
for(int i = 0; i < n-1; i++) // потому что первый уже вводили
{
printf("Enter the %d element\n",i+2); // первый уже вводили и нумерация от 0
scanf("%d",&x);
insel(cure,x);
cure = cure->next;
}
k = count(first); // какой указатель передадим, с такого и будет считать
printf("\n ListLen = %d \n",k);
getchar();
free(first);
}
void insel(struct List *cure, int x)
{
struct List *p = NULL; // в р вписывыем то куда указывала вначале
p = cure->next; // сохраняем указатель на следующий
cure->next = (struct List*)malloc(sizeof(struct List));
cure->next->number = x;
cure->next->next = p;
}
int count(struct List *cure)
{
int k = 0;
while(cure)
{
k++;
cure = cure->next;
}
return k;
}