| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1856 人关注过本帖
标题:链表添加错误~~~
取消只看楼主 加入收藏
a15022606145
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2015-7-11
收藏
 问题点数:0 回复次数:2 
链表添加错误~~~
图片附件: 游客没有浏览图片的权限,请 登录注册


程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Datastore.h"

int main()
{
    Datastore * store = ds_create();
    student obj;
    obj.id = 1;
    strcpy(obj.name,"zhangsan");
        ds_add(store,&obj);
    obj.id = 2;
    strcpy(obj.name,"zhangsan");
        ds_add(store,&obj);
    obj.id = 3;
    strcpy(obj.name,"zhangsan");
    ds_add(store,&obj);

    student * z = ds_find(store,3);
    printf("\n\n%d\t\t%s\n",z->id,z->name);


    show_printf(store);

    ds_destroy(store);

    return 0;
}



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include"Datastore.h"

//*****************创建于销毁**********************/
 Datastore * ds_create()

 {
    Datastore * store = (Datastore*)malloc(sizeof(Datastore *));
    store->head.next = NULL;
    return store;

 }


 void ds_destroy(Datastore* store)

 {
     student * p =store->head.next;
     while(p)
     {
         student * next=p->next;
         free(p);
         p=next;
     }

 }

 //************添加****************************//
void ds_add(Datastore* store,const student * data)
{
    student * copy =(student *)malloc(sizeof(student));
    *copy = *data;
    student * cur = store->head.next;
    student * pre = & store->head;
    while(cur)
    {
        if(copy->id < data->id)
            break;
        pre=cur;
        cur=cur->next;
    }
    copy->next = pre->next;
    pre->next=copy;

}
/*******************查找*********************/
student *ds_find(Datastore* store,int id)
{
    student * p = store->head.next;
    while(p)
    {
        if(p->id == id)
            return p;
        p=p->next;
    }
    
}
/******************删除************************/
void ds_remove(Datastore* store,int id)
{
    student * p = store->head.next;
    while(p)
    {
        if(p->id == id)
        {
            student * z = p;
            p=p->next;
            free(z);
        }
        p=p->next;
    }
}
/******************打印************************/
void show_printf(Datastore* store)
{
    student * p = store->head.next;
    while(p)
    {
        printf("ID = %d\t\t name = %s\n",p->id,p->name);
        p = p->next;
    
    }

}

//******************student 结构体****************//
struct student
{
    int id;
    char name[32];
    student *next;
};

struct Datastore
{
    student head ;
};

//*****************创建于销毁**********************/
Datastore * ds_create();

 void ds_destroy(Datastore* store);

 //************添加****************************//
void ds_add(Datastore* store,const student * data);
/*******************查找*********************/
student *ds_find(Datastore* store,int id);

/******************删除************************/
void ds_remove(Datastore* store,int id);

/******************打印************************/
void show_printf(Datastore* store);



如图所示, why? and
搜索更多相关主题的帖子: student obj head next void 
2018-09-08 12:31
a15022606145
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2015-7-11
收藏
得分:0 
回复 2楼 幻紫灵心
??? 哪里空?? 我小白 看不懂欸
2018-09-08 13:38
a15022606145
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2015-7-11
收藏
得分:0 
回复 4楼 吹水佬
thank you  原来我申请的是一个指针 大小就是四  我现在指向结构体 好使了, 不过为什么用malloc啊  我感觉new和delete更好用呢?
2018-09-08 15:31
快速回复:链表添加错误~~~
数据加载中...
 
   



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

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