最近时间比较宽裕,所以就封装了一个双向链表,其中包含了栈,和队列的机制,对链表的删除,查找,添加操作都以函数定位,和数字序号定位两种定位方式实现,所有的函数都经过我的测试(估计还会用不妥的地方),分享给大家,以后用到链式存储的数据结构时,拿来就用就行啦,下面是数据类型,函数声明,定义的代码,(测试函数不再提供):
函数头文件dlist.h
#ifndef _LIST_H_
#define _LIST_H_
/*定义bool型变量*/
typedef enum{false,true} bool;
/*定义结点成员*/
typedef struct data{
int age;
char name[10];
}Item;
/*定义构造双向链表的结点*/
typedef struct node{
Item item;
struct node * pre;
struct node * next;
}Node;
/*定义指向结点的指针*/
typedef Node * pNode;
/*定义头结点,记录链表头结点,尾结点的地址,及链表结点数*/
typedef struct head{
int nodenum;
Node * head;
Node * trail;
}Head;
/*
功能:初始化头结点
参数:指向头结点的指针
返回值类型:void
*/
void InitlizeList(Head * phead);
/*
功能:返回链表的成员数量
参数:指向头结点的指针
返回值类型:int
*/
int ListCount(const Head * phead);
/*
功能:判断链表是否为空
参数:指向头结点的指针
返回值类型:bool
*/
bool IsEmpty(const Head * phead);
/*
功能:将新结点追加到最后
参数:指向头结点指针,Item
返回值:bool
*/
bool PushStack(Head * phead,Item item);
/*
功能:出栈
参数:指向头结点的指针
返回值:出栈的元素
*/
bool PopStack(Head * phead,Item * pitem);
/*
功能:入队列
参数:指向头结点的指针,指向数据的指针
返回值:bool
*/
bool EnQueue(Head * phead,Item item);
/*
功能:出队列
参数:指向头结点的指针,指向数据的指针
返回值:bool
*/
bool OutQueue(Head * phead,Item * pitem);
/*
功能:结构体赋值
参数:指向结点的指针,Item
返回值:void
*/
void CopyToNode(Node * pnode,Item item);
/*
功能:将出栈或出队列的数据赋值给指定的item
参数:要出栈或出队列的指针
返回值:void
*/
void CopyToItem(Node * pnode,Item * pitem);
/*
功能:删除一个结点
参数:指向头结点的指针,指向该结点的指针
返回值:bool
*/
bool DelTheNode(Head * phead,Node * pnode);
/*
功能:遍历链表
参数:指向头结点的指针,判断是否符合条件的函数指针
返回值:指向符合条件的结点的指针
*/
Node * Search(Head * phead,bool (*)(Item item));
/*
功能:找到第n个元素的头结点
参数:指向头结点的指针,n
返回值:指向该结点的指针
*/
Node * Find(Head * phead,int n);
/*
功能:删除符合条件的结点
参数:指向头结点的指针,判断是否符合条件的函数指针
返回值:bool
*/
bool DelOne(Head * phead,bool (*)(Item item));
/*
功能:删除指定位置的结点
参数:指向头结点的指针
返回值:bool
*/
bool DelAppointedOne(Head * phead,int n);
/*
功能:添加一个item到链表
参数:指向头结点的指针,item,n
返回值:bool
*/
bool InsertOne(Head * phead,Item item,int n);
/*
功能:添加一个结点到指定位置
参数:指向该位置前一个结点的指针,新成员Item
返回值:bool
*/
bool InsertTheOne(Head * phead,Node * pnode,Item item);
/*
功能:制造一个新节点
参数:Item
返回值:指向新节点的指针
*/
Node * MakeNewNode(Item item);
/*
功能:遍历整个链表,对每个成员进行处理
参数:指向头结点的指针,用户自定义的函数指针
返回值:void
*/
void Traverse(Head * phead,void (*)(Item item));
/*
功能:释放所有结点
参数:指向头结点的指针
返回值:void
*/
void DelAll(Head * phead);
/*
功能:销毁链表
参数:指向头结点的指针
返回值:void
*/
void DestroyList(Head * phead);
#endif
下面是函数实现部分dlist.c
#include <stdio.h>
#include <stdlib.h>
#include "dlist.h"
void InitlizeList(Head * phead)
{
phead ->nodenum = 0;
phead ->head = NULL;
phead ->trail = NULL;
}
int ListCount(const Head * phead)
{
return phead ->nodenum;
}
bool IsEmpty(const Head * phead)
{
if(phead ->nodenum == 0)
return true;
return false;
}
bool PushStack(Head * phead,Item item)
{
Node * pnew;
pnew = MakeNewNode(item);
if(pnew == NULL)
return false;
if(phead ->nodenum == 0)
{
phead ->head = pnew;
phead ->trail = pnew;
phead ->nodenum++;
return true;
}
phead ->trail ->next = pnew;
pnew ->pre = phead ->trail;
phead ->trail = pnew;
phead ->nodenum++;
return true;
}
bool PopStack(Head * phead,Item * pitem)
{
if(phead ->nodenum == 0)
return false;
CopyToItem(phead ->trail,pitem);
phead ->trail = phead ->trail ->pre;
if(phead ->trail == NULL)
{
free(phead ->head);
phead ->head = NULL;
}
else
{
free(phead ->trail ->next);
phead ->trail ->next = NULL;
}
phead ->nodenum--;
return true;
}
bool EnQueue(Head * phead,Item item)
{
return PushStack(phead,item);
}
bool OutQueue(Head * phead,Item * pitem)
{
Node * temp = phead ->head;
if(phead ->nodenum == 0)
return false;
CopyToItem(phead ->head,pitem);
if(phead ->nodenum == 1)
{
phead ->head = NULL;
phead ->trail = NULL;
}
else
{
phead ->head = phead ->head ->next;
phead ->head ->pre = NULL;
}
free(temp);
phead ->nodenum--;
return true;
}
void CopyToNode(Node * pnode,Item item)
{
pnode ->item = item;
}
void CopyToItem(Node * pnode,Item * pitem)
{
*pitem = pnode ->item;
}
bool DelTheNode(Head * phead,Node * pnode)
{
Item item;
// phead ->nodenum--;
if(pnode ->next == NULL)
{
PopStack(phead,&item);
return true;
}
if(pnode ->pre == NULL)
{
OutQueue(phead,&item);
return true;
}
pnode ->pre ->next = pnode ->next;
pnode ->next ->pre = pnode ->pre;
phead ->nodenum--;
free(pnode);
return true;
}
Node * Search(Head * phead,bool (* pfun)(Item item))
{
Node * pt;
pt = phead ->head;
while(pt)
{
if(pfun(pt ->item))
return pt;
pt = pt ->next;
}
return NULL;
}
Node * Find(Head * phead,int n)
{
Node * ptemp;
Node * pt = phead ->head;
int i = 1;
if(n == 1)
return pt;
while(pt && (i != n))
{
pt = pt ->next;
i++;
}
return pt;
}
bool DelOne(Head * phead,bool(* pfun)(Item item))
{
Node * pt;
pt = Search(phead,pfun);
if(pt)
return DelTheNode(phead,pt);
}
bool DelAppointedOne(Head * phead,int n)
{
Node * pt;
if(phead ->nodenum < n || n <= 0)
{
printf("Sorry!The one is not exist!");
return false;
}
pt = Find(phead,n);
return DelTheNode(phead,pt);
}
bool InsertOne(Head * phead,Item item,int n)
{
Node * pt;
Node * pnode;
int i = 1;
if(n <= 0 || n > phead ->nodenum + 1)
{
printf("The serial number is too large or too small,we can't insert to this place.");
return false;
}
pt = MakeNewNode(item);
pnode = phead ->head;
if(pnode == NULL && n == 1)
{
phead ->head = pt;
phead ->trail = pt;
phead ->nodenum++;
return true;
}
else if(n == 1)
{
pt ->next= pnode;
pnode ->pre = pt;
phead ->head = pt;
phead ->nodenum++;
return true;
}
else
{
while(pnode && i != n)
{
pnode = pnode ->next;
i++;
}
if(pnode == NULL && i != n)
return false;
else if(pnode == NULL && i == n)
{
phead ->trail ->next = pt;
pt ->pre = phead ->trail;
phead ->trail = pt;
phead ->nodenum++;
return true;
}
else
{
pnode ->pre ->next = pt;
pt ->pre = pnode ->pre;
pt ->next = pnode;
pnode ->pre = pt;
phead ->nodenum++;
return true;
}
}
}
/*
bool InsertTheOne(Head * phead,Node * pnode,Item item)
{
Node * ptemp;
ptemp = MakeNewNode(item);
if(pnode == NULL)
{
phead ->head = ptemp;
phead ->trail = ptemp;
}
else if(pnode ->next == NULL)
{
phead ->trail ->next = ptemp;
phead ->trail = ptemp;
}
else if(pnode ->pre == NULL)
{
}
}
*/
Node * MakeNewNode(Item item)
{
Node * pt;
pt = (Node *)malloc(sizeof(Node));
CopyToNode(pt,item);
pt ->pre = NULL;
pt ->next = NULL;
return pt;
}
void Traverse(Head * phead,void (* pfun)(Item item))
{
Node * pt;
pt = phead ->head;
while(pt)
{
pfun(pt ->item);
pt = pt ->next;
}
}
void DelAll(Head * phead)
{
Node * pt,* ptemp;
pt = phead ->head;
while(pt)
{
ptemp = pt ->next;
free(pt);
pt = ptemp;
phead ->nodenum--;
}
}
void DestroyList(Head * phead)
{
DelAll(phead);
free(phead);
}