您的位置:首页 > 其它

双向链表——基本操作

2016-06-02 13:43 357 查看
"test.c"

<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1
#include "DoubleSLishtNode.h"

void Test1()//InitDSList  PushBack PrintfDSList  PopBack
{
PDoubleSListNode pHead = NULL;

InitDSList(&pHead);
PushBack(&pHead,0);
PushBack(&pHead,1);
PushBack(&pHead,2);
PushBack(&pHead,3);
PrintfDSList(&pHead);

PopBack(&pHead);
PopBack(&pHead);
//PopBack(&pHead);
//PopBack(&pHead);
//PopBack(&pHead);

PrintfDSList(&pHead);
}

void Test2()//PushFront  PopFront
{
PDoubleSListNode pHead = NULL;
InitDSList(&pHead);
PushFront(&pHead,0);
PushFront(&pHead,1);
PushFront(&pHead,2);
PushFront(&pHead,3);
PrintfDSList(&pHead);

PopFront(&pHead);
PopFront(&pHead);
//PopFront(&pHead);
//PopFront(&pHead);
//PopFront(&pHead);

PrintfDSList(&pHead);
}

void Test3()//Find   Erase
{
PDoubleSListNode pHead = NULL;

InitDSList(&pHead);
PushBack(&pHead,0);
PushBack(&pHead,1);
PushBack(&pHead,2);
PushBack(&pHead,3);
PrintfDSList(&pHead);

//Erase(&pHead,Find(&pHead,0));
//Erase(&pHead,Find(&pHead,2));
//PrintfDSList(&pHead);

Insert(&pHead,Find(&pHead,2),10);
//Insert(&pHead,Find(&pHead,3),11);
//Insert(&pHead,Find(&pHead,3),12);

PrintfDSList(&pHead);

}
int main()
{
//Test1();
//Test2();
Test3();
system("pause");
return 0;
}</span></strong>


"DoubleSListNode.h"

<strong><span style="font-size:18px;">#ifndef __DOUBLESLISTNODE_H__
#define __DOUBLESLISTNODE_H__
#pragma once;

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;

typedef struct DoubleSListNode
{
DataType data;
struct DoubleSListNode *_LeftNode;
struct DoubleSListNode *_RightNode;
}DoubleSListNode,*PDoubleSListNode;

//初始化双向链表
void InitDSList(PDoubleSListNode* pHead);
//打印双向链表
void PrintfDSList(PDoubleSListNode* pHead);
//开辟结点内存
PDoubleSListNode ByeNode(DataType data);
//尾插法
void PushBack(PDoubleSListNode* pHead,DataType data);
//尾删法
void PopBack(PDoubleSListNode* pHead);
//头插法
void PushFront(PDoubleSListNode* pHead,DataType data);
//头删法
void PopFront(PDoubleSListNode* pHead);
//在双向链表中查找一个元素
PDoubleSListNode Find(PDoubleSListNode* pHead,DataType x);
//删除pos位置上的结点
void Erase(PDoubleSListNode* pHead,PDoubleSListNode pos);
//在双向链表的pos位置插入x
void Insert(PDoubleSListNode* pHead,PDoubleSListNode pos,DataType x);
#endif//__DOUBLESLISTNODE_H__</span></strong>


"DoubleSListNode.c"

<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1
#include "DoubleSLishtNode.h"

void InitDSList(PDoubleSListNode* pHead)
{
assert(pHead);
pHead = NULL;
}

PDoubleSListNode ByeNode(DataType data)
{
PDoubleSListNode pNewNode = (PDoubleSListNode)malloc(sizeof(struct DoubleSListNode));

if (pNewNode == NULL)
{
printf("开辟结点失败\n");
}
else
{
pNewNode->data = data;
pNewNode->_LeftNode = NULL;
pNewNode->_RightNode = NULL;
}

return pNewNode;
}

void PrintfDSList(PDoubleSListNode* pHead)
{
PDoubleSListNode pNode = NULL;
assert(pHead);

if (*pHead == NULL)
{
return;
}

pNode = *pHead;

while (NULL !=pNode)
{
printf("%d ",pNode->data);
pNode = pNode->_RightNode;
}
printf("\n");
}

void PushBack(PDoubleSListNode* pHead,DataType data)
{
PDoubleSListNode pNode = NULL;
PDoubleSListNode pNewNode = NULL;
assert(pHead);
if (*pHead == NULL)
{
*pHead = ByeNode(data);
}
else
{
pNode = *pHead;

while (NULL !=pNode->_RightNode)
{
pNode = pNode->_RightNode;
}

pNewNode = ByeNode(data);
pNewNode->_RightNode = NULL;
pNewNode->_LeftNode = pNode;
pNode->_RightNode = pNewNode;
}
}

void PopBack(PDoubleSListNode* pHead)
{
PDoubleSListNode pPurNode = NULL;
PDoubleSListNode pCurNode = NULL;
assert(pHead);

if (*pHead == NULL)
{
printf("DoubleSListNode is empty\n");
return;
}
else if ((*pHead)->_RightNode == NULL)
{
pPurNode = *pHead;
free(pPurNode);
pPurNode = NULL;
*pHead = NULL;
}
else
{
pPurNode = *pHead;
while (NULL != pPurNode->_RightNode)
{
pPurNode = pPurNode->_RightNode;
}

pCurNode = pPurNode;
pPurNode->_LeftNode->_RightNode = NULL;
free(pCurNode);
pCurNode = NULL;
}
}

void PushFront(PDoubleSListNode* pHead,DataType data)
{
PDoubleSListNode pNewNode = NULL;
assert(pHead);

if (*pHead == NULL)
{
*pHead = ByeNode(data);
}
else
{
pNewNode = ByeNode(data);
pNewNode->_RightNode = (*pHead);
pNewNode->_LeftNode = NULL;
*pHead = pNewNode;
}
}

void PopFront(PDoubleSListNode* pHead)
{
PDoubleSListNode pNode = NULL;
assert(pHead);
if (*pHead == NULL)
{
printf("DoubleSListNode is emtpy\n");
return;
}

//if ((*pHead)->_RightNode = NULL)
//{
//	pNode = *pHead;
//	free(pNode);
//	pNode = NULL;
//	*pHead = NULL;
//}
else
{
if (NULL != (*pHead)->_RightNode)
{
pNode = *pHead;
*pHead = (*pHead)->_RightNode;
free(pNode);
pNode = NULL;
}
else
{
//pNode = *pHead;
//free(pNode);
//pNode = NULL;
//*pHead = NULL;
free(*pHead);
*pHead = NULL;
}
}
}

PDoubleSListNode Find(PDoubleSListNode* pHead,DataType x)
{
PDoubleSListNode pNode = NULL;
assert(pHead);
if (*pHead == NULL)
{
printf("PDoubleSListNode is empty\n");
return *pHead;
}

pNode = *pHead;
while (NULL !=pNode)
{
if (pNode->data == x)
{
return pNode;
}
else
{
pNode = pNode->_RightNode;
}
}

return NULL;
}

void Erase(PDoubleSListNode* pHead,PDoubleSListNode pos)
{
PDoubleSListNode pNode = NULL;
assert(pHead);

if (pos == NULL)
{
printf("Erase is error\n");
return;
}
else
{
if (pos == *pHead)
{
//pNode = pos;
//pos = pos->_RightNode;
//free(pNode);
//pNode = NULL;
pNode = *pHead;
*pHead = (*pHead)->_RightNode;
free(pNode);
pNode = NULL;
}
else
{
pNode = pos;
pos->_LeftNode->_RightNode = pos->_RightNode;
free(pNode);
pNode = NULL;
}
}
}

//还没屡清楚,等我弄明白了后面再补上
//void Insert(PDoubleSListNode* pHead,PDoubleSListNode pos,DataType x)
//{
//	PDoubleSListNode pNode = NULL;
//	PDoubleSListNode pNewNode = NULL;
//	assert(pHead);
//
//	if (pos == NULL)
//	{
//		printf("no find x\n");
//		return;
//	}
//
//	pNode = *pHead;
//	while (NULL != pNode)
//	{
//		if (pNode == pos)
//		{
//			pNewNode = ByeNode(x);
//			if (pos->_RightNode == NULL)
//			{
//				PushBack(pHead,x);
//			}
//			else
//			{
//				pNewNode->_RightNode = pos->_RightNode;
//				pos->_RightNode->_LeftNode = pNewNode;
//				pos->_RightNode = pNewNode;
//				pNewNode->_LeftNode = pos;
//			}
//		}
//		else
//		{
//			pNode = pNode->_RightNode;
//		}
//	}
//}</span></strong>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: