免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2170 | 回复: 10
打印 上一主题 下一主题

求助:链表问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-06 15:13 |只看该作者 |倒序浏览
初学链表,想把一个小文件的内容读出,放进链表,水平有限,无法编译通过,请各位大侠帮忙指正。


#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define DIR "./b.txt"

typedef struct list{
        char str[32];
        struct list *next;
}CT;

int creat_list(CT **head,char *buf)
{
        CT *temp = *head;

        if(temp == NULL)
        {
                temp = (CT*)malloc(sizeof(CT));
                *head = temp;
                memcpy( (temp->str),buf,sizeof(buf) );
                temp->next = NULL;
                return 0;
        }

        else
        {
                CT *temp2 = temp;
                temp2 = (CT*)malloc(sizeof(CT));
                temp2->next = *head;
                *head = temp2;
                memcpy( (temp->str),buf,sizeof(buf) );
        }

        return 0;
}

int main(int argc,char **argv)
{
        FILE *fp;
        char str2[32];
        CT *pHead;

        if((fp = fopen(DIR,"r")) == NULL)
        {
                printf("fopen error!\n");
                return 0;
        }

        while(fgets(str2,sizeof(str2),fp) != NULL)
        {
                creat(&pHead,str2);
        }

        CT *pTemp = pHead;
        while(pTemp != NULL)
        {
                printf("%s\n",pTemp->str);
                pTemp = pTemp->next;
        }
        return 0;
}

b.txt文件内容如下:

djfldjkf
fjsdljfld
fjlsdkjfl
234
f34

论坛徽章:
0
2 [报告]
发表于 2010-01-06 15:41 |只看该作者
你这个链表没实用性~给你个我自己写的~

论坛徽章:
0
3 [报告]
发表于 2010-01-06 15:42 |只看该作者
#ifndef _NB_LIST_H
#define _NB_LIST_H

#define offsetof(TYPE,MEMBER) (int)(((char *)&(((TYPE*)0)->MEMBER)) -((char *)((TYPE *)0)))
#define containof(TYPE,MEMBER,PTR) ({\
        const typeof(((TYPE *)0)->MEMBER) *_mPTR = (PTR); \
        (TYPE *)( (char *)_mPTR -offsetof(TYPE,MEMBER) ); \
        })

typedef struct _node {
        struct _node *next;
        struct _node *prev;
}node_t;

typedef struct _list {
        node_t list;
         int ListNum;
}list_t;


int init_list(list_t *head);
int push_node(list_t *head,node_t *node);
node_t * pop_node(list_t *head);
int add_node_head(list_t *head,node_t *node);
int del_node_head(list_t *head);
int add_node_tail(list_t *head,node_t *node);
int del_node_tail(list_t *head);

#endif /*end of list.h*/

#include "list.h"

int init_list(list_t *head)
{
        if (NULL == head)
                return -1;
        head->list.next =&head->list;
        head->list.prev= &head->list;
        head->ListNum =0;
        return 0;
}
//www.cnxhacker.com
int push_node(list_t *head,node_t *node)
{
        if (NULL == head || NULL == node)
                return  -1;
        head->list.prev->next= node;
        node->prev = head->list.prev;
        node->next = &head->list;
        head->list.prev = node;
        head->ListNum++;
        return 0;
}

node_t *  pop_node(list_t *head)
{
        node_t * RetNode=NULL;
        if (NULL == head)
                return NULL;
        RetNode = head->list.next;

        RetNode->next->prev = &head->list;
        head->list.next = RetNode->next;
        head->ListNum--;
        return RetNode;
}

int add_node_head(list_t *head,node_t *node)
{
        if (NULL == head || NULL == node)
                        return        -1;
         head->list.next->prev = node;
        node->next = head->list.next;
        node->prev= &head->list;
        head->list.next = node;
        head->ListNum++;
        return 0;
}

int del_node_head(list_t *head)
{
        node_t * TempNode=NULL;
        if (NULL == head)
                return -1;
        TempNode = head->list.next;

        TempNode->next->prev = &head->list;
        head->list.next = TempNode->next;
        head->ListNum--;
        return 0;
}

int add_node_tail(list_t *head,node_t *node)
{
        if (NULL == head || NULL == node)
                        return -1;
        node->prev = head->list.prev;
        node->next = &head->list;
        head->list.prev->next = node;
        head->list.prev = node;
        head->ListNum++;
        return 0;
}

int del_node_tail(list_t *head)
{
        node_t *TempNode=NULL;
        if (NULL == head)
                return -1;
        TempNode = head->list.prev;
        TempNode->next->prev= &head->list;
        head->list.next= TempNode->next;
        head->ListNum--;
        return 0;
}

int add_node_ByIndex(list_t *head,node_t *node,unsigned int Index)
{
        node_t *InsertNode=NULL;
        if (NULL == head || NULL == node)
                return -1;
        if (Index >= head->ListNum)
                return add_node_tail(head,node);

        InsertNode = head->list.next;
        while(Index-1) {
                InsertNode = InsertNode->next;
                Index--;
        }
        node->next= InsertNode->next;
        node->prev = InsertNode;
        InsertNode->next->prev = node;
        InsertNode->next = node;
        head->ListNum++;
        return 0;
}

node_t * Search_node_ByIndex(list_t *head,unsigned int Index)
{
        node_t * TempNode=NULL;
        if (NULL == head)
                return NULL;
        if (Index >= head->ListNum)
                return head->list.prev;

        TempNode= head->list.next;
        while(Index) {
                TempNode= TempNode->next;
                Index--;
        }
        return TempNode;
}
int del_node_ByIndex(list_t *head,unsigned int Index)
{
        node_t * TempNode=NULL;
        if (NULL == head)
                return -1;
        if (Index < 0 || Index >head->ListNum)
                return -1;
        TempNode= head->list.next;
        while(Index-1) {
                TempNode= TempNode->next;
                Index--;
        }
        TempNode->prev->next = TempNode->next;
        TempNode->next->prev = TempNode->prev;
        head->ListNum--;
        return 0;
}

这个链表用起来很爽~参照linux内核中的链表

论坛徽章:
1
双子座
日期:2014-08-29 17:15:03
4 [报告]
发表于 2010-01-06 15:49 |只看该作者
std::vector<std::string> _buf;

论坛徽章:
0
5 [报告]
发表于 2010-01-06 15:50 |只看该作者
谢谢了,我只是练习练习,能不能帮我修改一下刚才的呢?

论坛徽章:
0
6 [报告]
发表于 2010-01-06 16:01 |只看该作者
memcpy( (temp->str),buf,sizeof(buf) );
sizeof(buf) 等于4的~

论坛徽章:
0
7 [报告]
发表于 2010-01-06 16:13 |只看该作者

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>

  4. #define DIR "./b.txt"

  5. typedef struct list{
  6.         char str[32];
  7.         struct list *next;
  8. }CT;

  9. int creat_list(CT **head,char *buf)
  10. {
  11.    CT *temp = *head;

  12.         if(temp == NULL)
  13.         {
  14.            temp = (CT*)malloc(sizeof(CT));
  15.            *head = temp;
  16.             strcpy( temp->str,buf);
  17.             temp->next = NULL;
  18.             return 0;
  19.         }
  20.         else
  21.         {
  22.             CT* temp2 = (CT*)malloc(sizeof(CT));
  23.             temp2->next = *head;
  24.             *head = temp2;
  25.              strcpy( temp2->str,buf);
  26.         }

  27.         return 0;
  28. }

  29. int main(int argc,char **argv)
  30. {
  31.         FILE *fp;
  32.         char str2[32];
  33.         CT *pHead = NULL;

  34.         if((fp = fopen(DIR,"r")) == NULL)
  35.         {
  36.                 printf("fopen error!\n");
  37.                 return 0;
  38.         }

  39.         while(fgets(str2,sizeof(str2),fp) != NULL)
  40.         {
  41.           printf("read:%s", str2);
  42.           creat_list(&pHead,str2);
  43.         }

  44.         CT *pTemp = pHead;
  45.         while(pTemp != NULL)
  46.         {
  47.           printf("list:%s",pTemp->str);
  48.           pTemp = pTemp->next;
  49.         }
  50.      fclose(fp);
  51.         return 0;
  52. }


复制代码

论坛徽章:
0
8 [报告]
发表于 2010-01-06 16:17 |只看该作者
strcpy( temp->str,buf);
楼上的,用strncpy吧~

论坛徽章:
0
9 [报告]
发表于 2010-01-06 16:22 |只看该作者
在说说问题吧
1. 你编译的时候gcc -Wall   最好加个-Wall   新手无罪 大家都是新手过来的... 但是编译错误你应该学会自己解决
2. 弄清楚memcpy   strcpy的区别     strlen  sizeof的区别(这两个你不清楚的话自己先打印出来看看) 字符串结尾'\0'你也要注意 strlen
      memcpy( temp2->str,buf , strlen(buf)+1);  这里strlen(buf)+1你要明白
3. 具体的指针赋值的时候你最好在纸上画画^_^
4. 时刻的加debug信息...

还有几个不重要的..
3. 有开就有关 文件只开不关:wink: :wink:
4. 分配内存判断是否失败
5.内存有分配就要释放  不过你这里程序over了不重要了..不过好的代码风格是慢慢养成的

呵呵  废话一大堆  我帮你稍微改了下 测试通过了...

论坛徽章:
0
10 [报告]
发表于 2010-01-06 16:24 |只看该作者
最NB的链表是内核的list.h   用户程序如何使用list.h 做下广告呵呵  我blog里有   也分析了内核的list.h的实现  
内核的代码是写的你不得不服
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP