链表添加错误~~~
程序代码:#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









