2015-09-22 3 views
-2

Я очень новичок в программировании, и я начал изучать C. Теперь я просто не могу понять, почему моя структура узла не видна моим функциям.struct uneclared (первое использование в этой функции)

Я пытаюсь получить некоторую помощь на http://www.cprogramming.com/tutorial/c/lesson7.html , но не повезло в использовании блоков кода 13.12

#include <stdio.h> 
#include <stdlib.h> 

struct node { 
int data; 
struct ptr * next; 
}; 
struct node* head; 

void Insert(int x) 
{ 
struct node *temp; 
temp = (node*)malloc(sizeof(struct node)); 
if(head == NULL) 
head = temp; 
temp->data = x; 
temp->data = x; 
temp->next = NULL; 
struct node* temp1 = head; 
while(temp1-> != NULL;) { 
temp1 = temp1->next; 
} 
temp1->next = temp; 
} 

void print() { 
struct node* temp = head; 
while(temp != NULL) { 
printf("the data is %d", temp->data); 
temp = temp->next; 
} 
} 


int main() 
{ 
head = NULL; 
int a,c; 
printf("How many numbers ? : \n"); 
scanf("%d",&a); 
for(i = 0; i<a; i++); { 
printf("Enter a number:\n"); 
scanf("%d",&c); 
Insert(c); 
print(); 
} 

} 
+0

что 'структура PTR * следующий;'? –

+0

строка, начинающаяся с 'temp =' неверна: она пропускает ');' –

+0

Существует много ошибок, но не «struct undeclared (first use in this function)» была для C99. – MikeCAT

ответ

1

Есть довольно много отпускает один на один

числа 1

struct node { 
    int data; 
    struct node *next; // chagnge ptr -> node 
}; 

номер 2

struct node *temp; 
temp = malloc(sizeof(struct node)); // struct node casting 

номер 3

while(temp1->next != NULL) { // remove semi colum and put a next 
    temp1 = temp1->next; 
} 

номер 4

int i; // for while loop 
for(i = 0; i<a; i++) { 

Теперь, надеюсь, он компилирует хорошо, проверить ошибки во время выполнения (если таковые имеются)

и да

return 0; // just before main 
+1

Может быть, вы должны удалить последнюю точку с запятой в номере 4 'for (i = 0; i MikeCAT

+0

well spotted done –

+1

И просто удалите бросок из 'malloc()' полностью :) – psmears

0

вы строите бесконечный цикл с вашим первым элементом ЛОР:

temp = (struct node*)malloc(sizeof(struct node)); 
if(head == NULL) 
    head = temp; 

temp->data = x; 
temp->next = NULL; 
struct node* temp1 = head; 
while(temp1->next != NULL) { // nothing to do 
    temp1 = temp1->next; 
} 
temp1->next = temp; //temp is head and temp1 is head, so head->next points to head 

вы должны сделать что-то вроде

if (head == NULL) { 
    //fill head and leave 
} else { 
    //traverse to the last element and concatenate the new element 
} 
Смежные вопросы