双向环链,实在找不到错误了。。。
#ifndef LISH_H__
#define LISH_H__ //声明部分 .h
#define FIRST 1
#define SECOND 2
typedef void list_op(const void *);
struct node
{
void *data;
struct node *prev;
struct node *next;
};
typedef struct
{
int size;
struct node head;
}list;
list *create(int initsize);
int insirt(list *,void *data,int mode);
void show(list *,list_op *);
void shifang(list *);
#endif
#include <stdio.h> //.c实现部分
#include <stdlib.h>
#include <string.h>
#include "list.h"
list *create(int initsize)
{
list *node;
node = malloc(sizeof(*node));
if(node == NULL)
return NULL;
node -> size = initsize;
node -> head.data == NULL;
node -> head.prev == &node -> head;
node -> head.next == &node -> head;
return node;
}
int insirt(list *ptr,void *data,int mode)
{
struct node *newnode;
newnode = malloc(sizeof(*newnode));
if(newnode == NULL)
return -1;
newnode -> data = malloc(sizeof(&ptr -> head));
if(newnode -> data ==NULL)
return -2;
memcpy(newnode -> data,data,ptr -> size);
if(mode == FIRST)
{
newnode -> prev = &ptr -> head;
newnode -> next = ptr -> head.next;
}else if(mode == SECOND)
{
newnode -> prev = ptr -> head.prev;
newnode -> next = &ptr -> head;
}
else
{
return -3;
}
newnode -> prev -> next = newnode;
newnode -> next -> prev = newnode;
return 0;
}
void show(list *ptr,list_op *op)
{
struct node *cur;
for(cur = ptr -> head.next;cur != &ptr -> head;cur = cur -> next)
{
op(cur -> data);
}
}
void shifang(list *ptr)
{
struct node *cur;
for(cur = ptr -> head.next;cur != &ptr -> head;cur = cur -> next)
{
free(cur -> data);
free(cur);
}
free(ptr);
}
#include <stdio.h> //main.c部分
#include <stdlib.h>
#include "list.h"
#define NAMESIZE 32
struct student
{
int id;
char name[NAMESIZE];
int math;
int chinese;
int english;
};
static void print_s(const void *record)
{
const struct node *c = record;
printf("%d %s %d %d %d\n",c -> id,c -> name,c -> math,c -> chinese,c -> english);
}
int main()
{
list *stu;
int i,ret;
struct student tmp;
stu = list *create(sizeof(struct student));
if(stu == NULL)
exit(1);
for(i = 1;i < 10; i++)
{
tmp.id = i;
snprintf(tmp.name,NAMESIZE,"stu%d",i);
tmp.math = rand() % 100;
tmp.chinese = rand() % 100;
tmp.english = rand() % 100;
ret = insirt(stu,&tmp,FIRST);
if(ret == 0)
exit(1);
}
show(stu,print_s);
printf("\n");
shifang(stu);
exit(0);
}
all:list //Makefile
list:list.o main.o
$(CC) $^ -o $@
clean:
rm list *.o -rf
cc -c -o main.o main.c //报错部分
main.c: In function ‘print_s’:
main.c:19:30: error: ‘const struct node’ has no member named ‘id’
printf("%d %s %d %d %d\n",c -> id,c -> name,c -> math,c -> chinese,c -> englis
^
main.c:19:38: error: ‘const struct node’ has no member named ‘name’
printf("%d %s %d %d %d\n",c -> id,c -> name,c -> math,c -> chinese,c -> englis
^
main.c:19:48: error: ‘const struct node’ has no member named ‘math’
printf("%d %s %d %d %d\n",c -> id,c -> name,c -> math,c -> chinese,c -> englis
^
main.c:19:58: error: ‘const struct node’ has no member named ‘chinese’
printf("%d %s %d %d %d\n",c -> id,c -> name,c -> math,c -> chinese,c -> englis
^
main.c:19:71: error: ‘const struct node’ has no member named ‘english’
rintf("%d %s %d %d %d\n",c -> id,c -> name,c -> math,c -> chinese,c -> english)
^
main.c: In function ‘main’:
main.c:29:8: error: expected expression before ‘list’
stu = list *create(sizeof(struct student));
^
<builtin>: recipe for target 'main.o' failed
make: *** [main.o] Error 1