struct unclared(首次使用此函数)

时间:2015-09-22 10:01:05

标签: c

我对编程非常陌生,我开始学习C.现在我无法理解为什么我的节点结构对我的函数不可见。

我尝试在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();
}

}

1 个答案:

答案 0 :(得分: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
}