Dieser Code soll eine einfache verkettete Liste mit Ganzzahlen von 0 bis 20 erstellen. Ich bekomme immer den Fehler: unknown type name 'node'
, wenn ich meinen Code für was jede Instanz von Knoten im Programm scheint kompilieren. Ich bin mir nicht sicher, ob ich sie definieren muss oder ein größerer Fehler im Code ist.Typedefs und Knoten
#include<stdio.h>
#include<stdlib.h>
int main() {
int i;
struct node *first=NULL;
for(i=1;i<=20;i++)
first=insertrear(first,i);
dispnodes(first);
sum(first);
return 0;
}
typedef struct node {
int data;
struct node *link;
};
node* getnode(node *temp,int i) {
temp=(node*)malloc(sizeof(node));
temp->data=i;
temp->link=NULL;
return temp;
}
node* insertrear(node *first,int a) {
node *temp=NULL,*i;
temp= getnode(temp,a);
if(first==NULL) {
first=temp;
return first;
}
for(i=first;i->link!=NULL;i=i->link);
i->link=temp;
return first;
}
void dispnodes(node *first) {
int j;
if(first==NULL) {
printf("\nlist empty");
return;
}
node *i;
for(i=first,j=0;i!=NULL;j++,i=i->link)
printf("\nNode #%d contains %d ",j,i->data);
}
void sum(node *first) {
node *i;
int total=0;
for(i=first;i!=NULL;i=i->link)
total+=i->data;
printf("\nThe sum total of all nodes in this list is %d",total);
}
Andere Dinge müssen auch vor dem Haupt deklariert werden! –