博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构线性表---双链表
阅读量:3576 次
发布时间:2019-05-20

本文共 2949 字,大约阅读时间需要 9 分钟。

AfxStd.h
#ifndef AfxStd#define AfxStd#include
#include
#include
#include
#endif // !USUSL_H
DList.h
#ifndef DLIST_H#define DLIST_H#include "AfxStd.h"typedef int ElemType;typedef struct DLNode {	DLNode *pre;	ElemType data;	DLNode *next;}DLNode, *DList;struct DUList {	DList head;	int cursize;};DLNode *BuyNode(DLNode *ptag = NULL, DLNode *ntag = NULL);/*避免编译上的二义性,在声明时给默认值*/void FreeNode(DList p);void InitList(DUList &L);void DestoryList(DUList &L);int GetLength(DUList &L);bool DlistEmpty(DUList &L);bool Insert(DUList &L, int pos, ElemType e);bool push_back(DUList &L, ElemType e);bool push_front(DUList &L, ElemType e);bool InsertElem(DUList &L, DLNode *ptr, ElemType e);bool DelectElem(DUList &L, DLNode *ptr);bool pop_back(DUList &L);bool pop_front(DUList &L); bool Declect(DUList &L, int pos);#endif // !1
DList.cpp
#include "Dlist.h"/*购买节点*/DLNode *BuyNode(DLNode *ptag, DLNode *ntag){	DList s = (DList)malloc(sizeof(DLNode));	if (s == NULL)	{		exit(1);	}	memset(s, 0, sizeof(DLNode));	s->pre = ptag == NULL ? s : ptag;	s->next = ntag == NULL ? s : ntag;	return s;}/*释放节点*/void FreeNode(DList p){	free(p);}/*初始化节点*/void InitList(DUList &L){	L.cursize = 0;	L.head = BuyNode();	//L.head->pre = L.head;	//L.head->next = L.head;}/*摧毁链表*/void DestoryList(DUList &L){	L.cursize = 0;	free(L.head);	L.head = NULL;}/*按位置增加一个节点*/bool Insert(DUList &L, int pos, ElemType e){	if (pos<1 || pos>L.cursize + 1)	{		printf("插入位置有误!");		return false;	}	DLNode *p = L.head;	while (1 != pos--)                     /*先参与运算再自减*/	{		p = p->next;	}	InsertElem(L, p->next, e);	/*	DLNode *newNode = BuyNode(p, p->next);	newNode->data = e;	L.cursize++;	*/	return true;}/*在某节点之前加入节点*/bool InsertElem(DUList &L, DLNode *ptr, ElemType e){	if (ptr == NULL)	{		return false;	}	/*	DLNode *newNode = BuyNode(ptr->pre, ptr);	newNode->data = e;	ptr->pre->next = newNode;	ptr->next = newNode;	L.cursize++;	*/	ptr->pre = BuyNode(ptr->pre, ptr);	ptr = ptr->pre;	ptr->pre->next = ptr;	ptr->data = e;	L.cursize++;	return true;}/*头插*/bool push_front(DUList &L, ElemType e){	InsertElem(L, L.head->next, e);	//Insert(L, L.cursize + 1, e);	return true;}/*尾插*/bool push_back(DUList &L, ElemType e){	InsertElem(L, L.head, e);	//Insert(L,1,e);	return true;}/*删除某节点*/bool DelectElem(DUList &L, DLNode *ptr){	if (ptr == NULL)	{		return false;	}	ptr->pre->next = ptr->next;	ptr->next->pre = ptr->pre;	free(ptr);	L.cursize--;	return true;}/*尾删*/bool pop_back(DUList &L){	DelectElem(L, L.head->pre);	return true;}/*头删*/bool pop_front(DUList &L){	DelectElem(L, L.head->next);	return true;}/*按位置删除节点*/bool Declect(DUList &L, int pos){	if (pos<1 || pos>L.cursize)	{		printf("删除位置有误!");		return false;	}	DLNode *p = L.head;	while (1 != pos--)                     /*先参与运算再自减*/	{		p = p->next;	}	DelectElem(L, p->next);	return true;}/*判空*/bool DlistEmpty(DUList &L){	return L.cursize == 0;}/*链表的长度*/int GetLength(DUList &L){	return L.cursize;}

转载地址:http://znxgj.baihongyu.com/

你可能感兴趣的文章
什么情况下会发生栈内存溢出。
查看>>
何为去中心化
查看>>
缓存一致性:写策略
查看>>
Cache一致性:MESI
查看>>
缓存一致性:写未命中
查看>>
为什么用中间位作为组索引
查看>>
缓存:局部性
查看>>
mysql原理:b+树索引
查看>>
mysql原理:最左原则
查看>>
mysql原理:join标到底是什么,为什么有军规不建议超过三个
查看>>
redis缓存穿透
查看>>
redis缓存雪崩
查看>>
mysql的事务隔离
查看>>
mvc架构
查看>>
ElasticSearch(0) ES的认识
查看>>
JPA入门
查看>>
JPA关系
查看>>
4.spring注解和生命周期相关的(了解)
查看>>
3.spring 的纯注解配置
查看>>
4.Spring 整合 Junit
查看>>