#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *link; struct node *insert; }JD; int main() {JD *h,*f; int j,x; h=(JD*)malloc(sizeof(JD)); /*生成表头结点*/ h->data=0; f=h; for(j=1;j<=5;j++) /*调用lbcr函数生成具有5个结点的单链表*/ {scanf("%d",&x); f=lbsc(f,x);} for(j=1;j<=5;j++) /*输出生成的单链表*/ {h=h->link;printf("%d,",h->data);} printf("\n"); } lbsc (JD *p,int x) { JD *s; s=(JD*)malloc(sizeof(JD)); s->data=x; p->link=s; return s;} typedef struct node node*insert(node * head, int x) { node * last, * current, * p; //要插入的结点 p = (node *)malloc(sizeof(node)); p->num = x; //空表插入 if(head == NULL) { head = p; p->next = NULL; return head; } //找插入位置 current = head; while(x > current->num && current->next != NULL) { last = current; current = current->next; } if(x <= current->num) { if(head == current)//在第一结点之前插入 { p->next = head; head = p; return head; } else//中间位置插入 { p->next = current; last->next = p; return head; } } } 中有[Error] expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
时间: 2024-01-24 19:03:36 浏览: 83
这段代码中存在语法错误。错误的原因是在定义结构体 `node` 时,缺少了结构体名称。修改方法如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *link;
struct Node *insert;
} JD;
JD* lbsc(JD *p, int x) {
JD *s;
s = (JD*)malloc(sizeof(JD));
s->data = x;
p->link = s;
return s;
}
int main() {
JD *h, *f;
int j, x;
h = (JD*)malloc(sizeof(JD));
h->data = 0;
f = h;
for(j = 1; j <= 5; j++) {
scanf("%d", &x);
f = lbsc(f, x);
}
for(j = 1; j <= 5; j++) {
h = h->link;
printf("%d,", h->data);
}
printf("\n");
return 0;
}
```
同时,代码中还有一个 `node* insert(node * head, int x)` 函数,该函数定义在主函数之外,需要将其放到主函数内部或者将其定义在外部并且在主函数之前声明。
阅读全文