双向链表的实现

#define _CRT_SECURE_NO_WARNINGS 1

 

#include<stdio.h>

#include<stdlib.h>

#include<assert.h>

 

typedef int DataType;

 

typedef struct DListNode

{

struct DListNode* _next;

struct DListNode* _prev;

DataType _data;

}DListNode;

 

 

DListNode* BuyDListNode(DataType x)

{

DListNode *NewNode;

NewNode = (DListNode*)malloc(sizeof(DListNode));

if (NewNode == NULL)

{

printf("空间分配失败\n");

}

else

{

 

NewNode->_data = x;

NewNode->_next = NULL;

NewNode->_prev = NULL;

}

return NewNode;

}

DListNode* DListInit()

{

DListNode *head;

head = BuyDListNode(0);

head->_next = head;

head->_prev = head;

return head;

 

}

void DListDestory(DListNode* head)

{

assert(head);

DListNode *cur;

cur = head;

while (head->_next != head)

{

cur = head;

 

head = (head)->_next;

free(cur);

 

}

}

 

 

void DListPushBack(DListNode* head, DataType x)

{

assert(head);

DListNode *cur = NULL;

DListNode *NewNode;

NewNode = BuyDListNode(x);

if (head->_next == head)

{

head->_next = NewNode;

head->_prev = NewNode;

NewNode->_next = head;

NewNode->_prev = head;

}

cur = head->_prev;

cur->_next = NewNode;

head->_prev = NewNode;

NewNode->_next = head;

NewNode->_prev = cur;

 

}

void DListPushFront(DListNode* head, DataType x)

{

assert(head);

DListNode *cur = NULL;

DListNode *NewNode;

NewNode = BuyDListNode(x);

cur = head->_next;

head->_next = NewNode;

NewNode->_prev = head;

NewNode->_next = cur;

cur->_prev = NewNode;

}

void DListPopBack(DListNode* head)

{

assert(head);

DListNode *cur = NULL;

/*DListNode *cur2 = NULL;*/

/*cur = head->_prev->_prev;

cur2 = head->_prev;

head->_prev = cur;

cur->_next = head;

free(cur2);

cur2->_next = NULL;

cur2->_prev = NULL;*/

cur = head->_prev;

head->_prev = head->_prev->_prev;

head->_prev->_next = head;

free(cur);

 

 

 

}

void DListPopFront(DListNode* head)

{

assert(head);

DListNode *cur = NULL;

/*DListNode *cur2 = NULL;

cur = head->_next->_next;

cur2 = head->_next;

head->_next = cur;

cur->_prev = head;

free(cur2);

cur2->_next = NULL;

cur2->_prev = NULL;*/

cur = head->_next;

head->_next = head->_next->_next;

head->_next->_prev = head;

free(cur);

}

 

DListNode* DListFind(DListNode* head, DataType x)

{

assert(head);

DListNode *cur = NULL;

cur = head;

while (cur->_data != x)

{

cur = cur->_next;

}

return cur;

}

void DListInsert(DListNode* pos, DataType x)

{

DListNode *cur = NULL;

DListNode *NewNode;;

NewNode = BuyDListNode(x);

cur = pos->_prev;

cur->_next = NewNode;

NewNode->_prev = cur;

NewNode->_next = pos;

pos->_prev = NewNode;

 

 

 

}

void DListErase(DListNode* pos)

{

DListNode *cur;

DListNode *cur2;

cur = pos->_prev;

cur2 = pos->_next;

cur->_next = cur2;

cur2->_prev = cur;

free(pos);

}

 

void DListPrint(DListNode* head)

{

assert(head);

DListNode *cur;

cur = head;

while (cur->_next != head)

{

printf("%d", cur->_data);

cur = cur->_next;

}

printf("%d", cur->_data);

 

}

 

 

void TestDList1()

{

DListNode* list=DListInit();

DListPushBack(list, 1);

DListPushBack(list, 2);

DListPushBack(list, 3);

DListPushBack(list, 4);

DListPushBack(list, 5);

DListPrint(list);

 

DListPopBack(list);

DListPopBack(list);

DListPopBack(list);

DListPopBack(list);

DListPopBack(list);

//DListPrint(list);

}

 

void TestDList2()

{

DListNode *list=DListInit();

DListPushFront(list, 1);

DListPushFront(list, 2);

DListPushFront(list, 3);

DListPushFront(list, 4);

DListPushFront(list, 5);

DListPrint(list);

 

DListPopFront(list);

DListPopFront(list);

DListPopFront(list);

DListPopFront(list);

DListPopFront(list);

DListPrint(list);

}

int main()

{

TestDList1();

system("pause");

TestDList2();

system("pause");

return 0;

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值