#include<stdio.h> #include<stdlib.h> typedef struct Node{ int data; struct Node* next; }Node; Node* initList(){ Node*list=(Node*)malloc(sizeof(Node)); list -> data=0; list->next=NULL; return list;} void headInsert(Node*list,int data){ Node*node=(Node*)malloc(sizeof(Node)); Node*head=list; node->data=data; node->next=list->next; list->next=node; list->data++; } void lastInsert(Node*list,int data){ Node*head=list; Node*node=(Node*)malloc(sizeof(Node)); node->data=data; node->next=NULL; list=list->next; while(list->next){ list=list->next; } list->data=node; list->data++; } void printList(Node*list){ list=list->next; while(list){ printf("%d",list->data); list=list->next; }printf("\n");} int main(){ Node*list=intiList(); headInsert(list,2); headInsert(list,3); headInsert(list,4); lastInsert(list,8); printList(list); return 0; }
时间: 2024-04-12 08:27:42 浏览: 111
I see that there is a typo in your code. In the `main()` function, you have a typo in the line `Node*list=intiList();`. It should be `Node*list=initList();` instead.
Here's the corrected version of your code:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* initList() {
Node* list = (Node*)malloc(sizeof(Node));
list->data = 0;
list->next = NULL;
return list;
}
void headInsert(Node* list, int data) {
Node* node = (Node*)malloc(sizeof(Node));
Node* head = list;
node->data = data;
node->next = list->next;
list->next = node;
list->data++;
}
void lastInsert(Node* list, int data) {
Node* head = list;
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
list = list->next;
while (list->next) {
list = list->next;
}
list->next = node;
head->data++;
}
void printList(Node* list) {
list = list->next;
while (list) {
printf("%d ", list->data);
list = list->next;
}
printf("\n");
}
int main() {
Node* list = initList();
headInsert(list, 2);
headInsert(list, 3);
headInsert(list, 4);
lastInsert(list, 8);
printList(list);
return 0;
}
```
Now you should be able to compile and run your code without any syntax errors. The corrected code initializes a linked list, inserts nodes at the head and at the end, and then prints the data in the list.
阅读全文