#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;
typedef struct DListNode
{
struct DListNode* _prev;
DataType _data;
struct DListNode* _next;
}DListNode;
----------------------------------------------------------------
DListNode* DListInit()//链表初始化
{
DListNode *head = (DListNode*)malloc(sizeof(DListNode));
assert(head);
head->_data = 0;
head->_next = NULL;
head->_prev = NULL;
return head;
}
------------------------------------------------------------------
DListNode* BuyDListNode(DataType x)//建立新节点
{
DListNode *newnode = (DListNode*)malloc(sizeof(DListNode));
assert(newnode);
newnode->_data = x;
newnode->_next = NULL;
newnode->_prev = NULL;
return newnode;
}
--------------------------------------------------------------
void DListDestory(DListNode* head)//销毁链表
{
assert(head);
DListNode *flag = head;
while (flag)
{
flag = flag->_next;
free(head);
head = flag;
}
}
--------------------------------------------------------
void DListPrint(DListNode* head)//打印链表
{
if (head->_next == NULL)
{
printf("the dlist is empty.");
return;
}
head = head->_next;
while (head)
{
printf("%d ", head->_data);
head = head->_next;
}
}
-------------------------------------------------------------
void DListPushBack(DListNode* head, DataType x)//尾插
{
assert(head);
DListNode *tail = head;
while (tail->_next)
{
tail = tail->_next;
}
DListNode *newnode = BuyDListNode(x);
assert(newnode);
tail->_next = newnode;
newnode->_prev = tail;
}
----------------------------------------------------------
void DListPushFront(DListNode* head, DataType x)//头插
{
assert(head);
DListNode *p = head->_next;
DListNode *newnode = BuyDListNode(x);
newnode->_next =p;
p->_prev = newnode;
head->_next = newnode;
newnode->_prev = head;
}
--------------------------------------------------------
void DListPopBack(DListNode* head)//尾删
{
assert(head);
if ((head)->_next == NULL)
{
return;
}
DListNode *p = ((head)->_next);
DListNode *next = p->_next;
while (next->_next)
{
next = next->_next;
p = p->_next;
}
p->_next = NULL;
free(next);
}
--------------------------------------------------
void DListPopFront(DListNode* head)//头删
{
assert(head);
DListNode *p = head->_next;
if (p == NULL)
{
return;
}
DListNode *next = p->_next;
head->_next = next;
next->_prev = head;
free(p);
}
-----------------------------------------------------------
DListNode* DListFind(DListNode* head, DataType x)//查询
{
assert(head);
while (head->_next)
{
if ((head->_next)->_data == x)
{
return head->_next;
}
head = head->_next;
}
return NULL;
}
----------------------------------------------------------
void DListInsert(DListNode* pos, DataType x)//任意位置插
{
assert(pos);
DListNode *prev = pos->_prev;
DListNode *newnode = BuyDListNode(x);
newnode->_next = pos;
pos->_prev = newnode;
prev->_next = newnode;
newnode->_prev = prev;
}
------------------------------------------------------
void DListErase(DListNode* pos)//任意位置删
{
assert(pos);
DListNode *prev = pos->_prev;
DListNode *next = pos->_next;
if (next == NULL)
{
prev->_next = NULL;
free(pos);
return;
}
prev->_next = next;
next->_prev = prev;
free(pos);
}
双向链表 练习
猜你喜欢
转载自blog.csdn.net/ferlan/article/details/79721653
今日推荐
周排行