Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203864
  • 博文数量: 36
  • 博客积分: 2501
  • 博客等级: 少校
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-18 23:27
个人简介

时间就是一切。

文章分类

全部博文(36)

文章存档

2023年(1)

2017年(2)

2016年(6)

2014年(1)

2009年(1)

2008年(15)

2007年(10)

我的朋友

分类: C/C++

2016-06-09 20:02:04

#ifndef _LISTTEST_H_
#define _LISTTEST_H_


typedef struct Student
{
char name[16];
int src;
struct Student *parent;
struct Student *next;
}Student,*p_Student;


typedef struct DataNode
{
struct DataNode *pNext;
void *data;
}DataNode,*p_DataNode;


typedef struct ListHead
{
int size;
p_DataNode headNode;
}ListHead, *p_ListHead;

#endif



#include "stdio.h"
#include "ListTest.h"
#include "malloc.h"
#include "string.h"


p_ListHead studentList=NULL;


void add_node(p_Student *node)
{
p_DataNode *tp_node=NULL, *tq_node=NULL;
p_DataNode datanNode=NULL, *tmp=NULL;


if(NULL==studentList||NULL==node)
{
return;
}
studentList->size=studentList->size+1;
tp_node=&studentList->headNode;
datanNode = ( p_DataNode )malloc( sizeof( ListHead ));
memset( datanNode, 0x0, sizeof( ListHead ));
if(NULL==(*tp_node))
{
//添加链表头
tp_node=&datanNode;
(*tp_node)->data=(*node);
studentList->headNode=*tp_node;
return;
}
while(tp_node)
{
if(NULL==(*tp_node)->pNext)
{
(*tp_node)->pNext=datanNode;
(*tp_node)->pNext->data=(*node);
break;
}
tp_node=&(*tp_node)->pNext;
}
}


void rm_node(p_Student *node)
{
p_DataNode *tp_node=NULL, *tq_node=NULL, tmp=NULL;
Student sinfo={0};
int flag=0;


if(NULL==studentList||NULL==node)
{
return;
}
tp_node=&studentList->headNode;
while(tp_node)
{
if(NULL!=(*tp_node)->data)
{
memcpy(&sinfo,(*tp_node)->data,sizeof(Student));
if(0==strcmp(sinfo.name, (*node)->name))
{
flag=1;
break;
}
}
tq_node=&(*tp_node);
tp_node=&(*tp_node)->pNext;
}
if(0!=flag && NULL==tq_node)
{
//释放链表头
tq_node=&(*tp_node);
tmp=((*tp_node))->pNext;
free((*tp_node)->data);
(*tp_node)->data=NULL;
free((*tp_node));
(*tp_node)=NULL;
(*tq_node)=tmp;
}
else if(1==flag && NULL!=tq_node)
{
//释放链表节点
tmp=((*tp_node))->pNext;
free((*tp_node)->data);
(*tp_node)->data=NULL;
free((*tp_node));
(*tp_node)=NULL;
(*tq_node)->pNext=tmp;
}
return;
}




int main( void )
{
p_Student nodeInfo = NULL,nodeInfo_1 = NULL,nodeInfo_2 = NULL;


studentList = ( p_ListHead )malloc( sizeof( ListHead ));
memset( studentList, 0x0, sizeof( ListHead ));
studentList->size=0;
studentList->headNode=NULL;


nodeInfo=(p_Student)malloc(sizeof(Student));
memset(nodeInfo, 0x0, sizeof( Student ));
strcpy(nodeInfo->name,"nodeInfo");


nodeInfo_1=(p_Student)malloc(sizeof(Student));
memset(nodeInfo_1, 0x0, sizeof( Student ));
strcpy(nodeInfo_1->name,"nodeInfo_1");


nodeInfo_2=(p_Student)malloc(sizeof(Student));
memset(nodeInfo_2, 0x0, sizeof( Student ));
strcpy(nodeInfo_2->name,"nodeInfo_2");


add_node(&nodeInfo);
add_node(&nodeInfo_1);
add_node(&nodeInfo_2);




rm_node(&nodeInfo_2);
rm_node(&nodeInfo_1);
rm_node(&nodeInfo);


return 0;
}
ListTest.rar
阅读(1209) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~