C -- 双向链表

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值