C双向链表
第一步:建立抽象类型,定义接口
//实现双向链表
#ifndef _LIST_H
#define _LIST_H
#include <stdbool.h>
#define SIZE 20
//抽象一个数据类型
typedef struct _item
{
char title[20];
double value;
}Item;
//定义节点,每个结点包括指向上一个和指向下一个结点的指针
typedef struct _node
{
struct _node * last;
Item item;
struct _node * next;
}Node;
//定义双向链表类型,成员包括头指针、尾部指针
typedef struct _list
{
Node * head;
Node * tail;
}List;
//已定义一个指向链表的指针,将链表初始化
void InitializeList(List * ls);
//判断链表是否已满,(内存是否用尽)
bool ListIsFull(const List * ls);
//判断链表是否为空
bool ListIsEmpty(const List * ls);
//返回链表中项数
unsigned int CountSize(const List * ls);
//已初始化的链表中添加结点
void AddToList(List * ls, Item item);
//非空的链表中,任意位置插入一个结点
void InsertToList(List * ls, int index, Item item);
//已初始化的链表中删除一个结点
void DelInList(List * ls, Item item);
//打印展示非空链表中的数据
void ShowInList(const List * ls);
#endif
第二步:实现接口
自己的一些想法,实现或许不是最优的,还有很多需要改进的地方。以后回过头再来看看。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
static void CopyToNode(Node * p, Item item)
{
/* 以具体Item类型变化 */
p->item = item;
}
void InitializeList(Li