| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 998 人关注过本帖, 1 人收藏
标题:双向环链,实在找不到错误了。。。
只看楼主 加入收藏
shanhuaiyu
Rank: 1
等 级:新手上路
帖 子:6
专家分:8
注 册:2015-10-12
结帖率:50%
收藏(1)
 问题点数:0 回复次数:1 
双向环链,实在找不到错误了。。。

#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
搜索更多相关主题的帖子: include create 
2015-10-31 01:03
shanhuaiyu
Rank: 1
等 级:新手上路
帖 子:6
专家分:8
注 册:2015-10-12
收藏
得分:0 
找到了~~~~~
2015-10-31 03:47
快速回复:双向环链,实在找不到错误了。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017172 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved