带头结点的双向链表的基本操作

DList.h
#pragma once
#include<malloc.h>
#include<assert.h>
//带头结点的双向链表
typedef int DateType;

typedef struct DListNode
{
	struct DListNode* pNext;
	struct DListNode* pPre;
	DateType data;
}DLNode, *PDNode;
//DLNode:双向链表结点的类型
//PDNode:操作该结点的指针

void DListInit(PDNode* pHead);//初始化要改变其外部的指针,也就是头结点,所以要传二级指针
void DListPushBack(PDNode pHead, DateType data);//尾插
void DListPopBack(PDNode pHead);//尾删
void DListPushFront(PDNode pHead, DateType data);//头插
void DListPopFront(PDNode pHead);//头删
void DListInsert(PDNode pos, DateType data);//任意位置插入
void DListErase(PDNode pos);//任意位置删除
int DListEmpty(PDNode pHead);//判断链表是否是空链表
int DListSize(PDNode pHead);//求链表有效结点个数
void DListClear(PDNode pHead);//只清空有效的结点,不删除头结点
void DListDestory(PDNode* pHead);//销毁所有结点,包括头结点
PDNode BuyDListNode(DateType data);//构建结点
DList.c
#include "DList.h"

void DListInit(PDNode * pHead)
{
	assert(pHead);
	*pHead = BuyDListNode(0);//创建一个头结点
}

void DListPushBack(PDNode pHead, DateType data)
{
	PDNode pCur = pHead;
	PDNode pNewNode = NULL;
	assert(pHead);
	while (pCur->pNext)//找当前链表最后一个结点
	{
		pCur = pCur->pNext;
	}
	pNewNode = BuyDListNode(data);
	pCur->pNext = pNewNode;
	pNewNode->pPre = pCur;
}

void DListPopBack(PDNode pHead)
{
	PDNode pTailNode = pHead;
	assert(pHead);
	while (pTailNode->pNext)//找当前链表最后一个结点
	{
		pTailNode = pTailNode->pNext;
	}
	if (pTailNode != pHead)//链表有结点
	{
		pTailNode->pPre->pNext = NULL;
		free(pTailNode);
	}
}

void DListPushFront(PDNode pHead, DateType data)
{
	PDNode pNewNode = NULL;
	assert(pHead);
	pNewNode = BuyDListNode(data);
	pNewNode->pNext = pHead->pNext;
	pHead->pNext = pNewNode;
	pNewNode->pPre = pHead;
	if (pNewNode->pNext != NULL)//头插时链表中已经有结点
	{
		pNewNode->pNext->pPre = pNewNode;
	}
}

void DListPopFront(PDNode pHead)
{
	PDNode pDelNode = NULL;
	assert(pHead);
	pDelNode = pHead->pNext;
	if (pDelNode == NULL)//空链表
	{
		return;
	}
	pHead->pNext = pDelNode->pNext;
	if (pDelNode->pNext)//待删结点后还有结点
	{
		pDelNode->pNext->pPre = pHead;
	}
	free(pDelNode);
}

void DListInsert(PDNode pos, DateType data)
{
	PDNode pNewNode = NULL;
	if (pos == NULL || pos->pPre == NULL)
	{
		return;
	}
	pNewNode = BuyDListNode(data);
	pNewNode->pNext = pos;
	pNewNode->pPre = pos->pPre;
	pos->pPre = pNewNode;
	pNewNode->pPre->pNext = pNewNode;
}

void DListErase(PDNode pos)
{
	if (pos == NULL || pos->pPre == NULL)//空位置与头结点位置无法删除
	{
		return;
	}
	pos->pPre->pNext = pos->pNext;
	if (pos->pNext)//pos不是最后一个结点
	{
		pos->pNext->pPre = pos->pPre;
	}
}

int DListEmpty(PDNode pHead)
{
	assert(pHead);
	return NULL == pHead->pNext;
}

int DListSize(PDNode pHead)
{
	PDNode pCur = NULL;
	int count = 0;
	assert(pHead);
	pCur = pHead->pNext;
	while (pCur)
	{
		count++;
		pCur = pCur->pNext;
	}
	return count;
}

void DListClear(PDNode pHead)
{
	PDNode pCur = NULL;
	assert(pHead);
	pCur = pHead->pNext;
	while (pCur)
	{
		pHead->pNext = pCur->pNext;
		free(pCur);
		pCur = pHead->pNext;
	}
}

void DListDestory(PDNode * pHead)
{
	assert(pHead);
	DListClear(*pHead);
	free(*pHead);
	*pHead = NULL;
}

PDNode BuyDListNode(DateType data)
{
	PDNode pNewNode = (PDNode)malloc(sizeof(DLNode));
	if (pNewNode == NULL)//系统空间不足,申请空间失败就直接停止
	{
		assert(0);
		return NULL;
	}
	pNewNode->data = data;
	pNewNode->pNext = NULL;
	pNewNode->pPre = NULL;
	return pNewNode;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码,经典。 CARD *myinsert(LCARD *head, LCARD *insert) { LCARD *temp = NULL; if (head==NULL)//链表为空 { head = insert; insert->next = insert; insert->prior = insert; } else//链表非空 { temp = head; if (head->cardnum>insert->cardnum)//插入到头前边,并且把自己当作头 { head->prior->next = insert; insert->prior = head->prior; insert->next = head; head->prior = insert; head = insert; } if (insert->cardnum0<50)//小于50正向插入 { while ((temp->cardnum<insert->cardnum)&&(temp->next!=head))//循环 { temp = temp->next; } if (temp->cardnum>insert->cardnum)//第一个条件终止的 { temp->prior->next = insert; insert->prior = temp->prior; insert->next = temp; temp->prior = insert; } else//第二个条件终止的 { head->prior->next = insert; insert->prior = head->prior; insert->next = head; head->prior = insert; } } else//大于50反向插入 { while ((temp->cardnum>insert->cardnum)&&(temp->prior!=head))//循环,第二个条件禁止跑飞 { temp = temp->prior; } if (temp->cardnum<insert->cardnum)//只有第一个条件可以终止的 { temp->next->prior = insert; insert->next = temp->next; insert->prior = temp; temp->next = insert; } } } //printf("%d\t%d\n", insert->id, insert->cardnum); return head; } void swap_id(SWID *sw) { LCARD *temp = sw->head; if (sw->head->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); sw->head->id = sw->inID; return ; } if ((sw->swapcardnum0)<50) { while ((temp->cardnum!=sw->swapcardnum)&&(temp->next!=sw->head)) { temp = temp->next; } if (temp->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); temp->id = sw->inID; } } else { while ((temp->cardnum!=sw->swapcardnum)&&(temp->prior!=sw->head)) { temp = temp->prior; } if (temp->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); temp->id = sw->inID; } } } LCARD *mydel(LCARD *head, LCARD *del) { LCARD *temp = NULL; if (head==NULL)//没有链表 { printf("there is no card\n"); } else//有链表 { if(head->next==head)//链表里就有一个节点并且为头结点 { if (head->cardnum==del->cardnum) { free(head); head = NULL; } else { printf("in mydel error\n"); } } else//链表里有超过一个的节点 { temp = head; if (del->cardnum0<50)//成立则正向删除 { while ((temp->cardnum!=del->cardnum)&&(temp->next!=head)) { temp = temp->next; } if (temp->cardnum==del->cardnum) { temp->prior->next = temp->next; temp->next->prior = temp->prior; free(temp); } } else//反向删除 { while ((temp->cardnum!=del->cardnum)&&(temp->prior!=head)) { temp = temp->prior; } if (temp->cardnum==del->cardnum) { temp->prior->next = temp->next; temp->next->prior = temp->prior; free(temp); } } } } return head; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值