带头节点的双向链表,不循环,尾节点的后继为空,头节点的前驱为空
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct DNode
{
int data;
struct DNode* next;//后继指针
struct DNode* prio;//前驱指针
}DNode,*DList;
//初始化
void InitList(DList plist)
{
assert(plist != NULL);
if (plist == NULL)
return;
plist->prio = NULL;
plist->next = NULL;
}
//头插
bool Insert_head(DList plist, int val)
{
//创建新节点
DNode* pnewnode = (DNode*)malloc(sizeof(DNode));
assert(pnewnode != NULL);
if (pnewnode == NULL)
return false;
pnewnode->data = val;
pnewnode->prio = NULL;
pnewnode->next = NULL;
//插入新节点
pnewnode->next = plist->next;
if (pnewnode->next != NULL)
{
pnewnode->next->prio = pnewnode;
}
pnewnode->prio = plist;
plist->next = pnewnode;
return true;
}
//尾插
bool Insert_tail(DList plist, int val)
{
//创建新节点
DNode* pnewnode = (DNode*)malloc(sizeof(DNode));
assert(pnewnode != NULL);
if (pnewnode == NULL)
return false;
pnewnode->data = val;
pnewnode->prio = NULL;
pnewnode->next = NULL;
//找尾巴
DNode* q;
for (q = plist; q->next != NULL; q = q->next);
//将p插入在q的后面
pnewnode->prio = q;
q->next = pnewnode;
return true;
}
//获取有效数据节点个数
int GetLength(DList plist)
{
int count = 0;
for (DNode* p = plist->next; p != NULL; p = p->next)
{
count++;
}
return count;
}
//查找第一个key,找到返回节点地址,没有找到返回NULL
DNode* Search(DList plist, int key)
{
for (DNode* p = plist->next; p != NULL; p = p->next)
{
if (p->data == key)
return p;
}
return NULL;
}
//返回key的前驱地址,如果不存在返回NULL
DNode* GetPrio(DList plist, int key)
{
DNode* p = Search(plist, key);
return p == NULL ? NULL : p->prio;
}
//返回key的后继地址,如果不存在返回NULL
DNode* GetNext(DList plist, int key)
{
DNode* p = Search(plist, key);
return p == NULL ? NULL : p->next;
}
//删除第一个val值
bool Delval(DList plist, int val)
{
DNode* p = Search(plist, val);
if (p == NULL)
return false;
//将p从链表中删除
p->prio->next = p->next;
if (p->next != NULL)
{
p->next->prio = p->prio;
}
free(p);
return true;
}
//按位插
bool Insert_pos(DList plist,int pos,int val)
{
//创建新节点
DNode* pnewnode = (DNode*)malloc(sizeof(DNode));
assert(pnewnode != NULL);
if (pnewnode == NULL)
return false;
pnewnode->data = val;
pnewnode->prio = NULL;
pnewnode->next = NULL;
//判断位置是否合法
if (pos < 0 || pos >= GetLength(plist))
return false;
//插入新节点
DNode* p = plist;
for (int i = 0; i < pos; i++)
{
p = p->next;
}
pnewnode->next = p->next;
//pnewnode可能最后一个节点
if (pnewnode->next != NULL)
{
pnewnode->next->prio = pnewnode;
}
pnewnode->prio = p;
p->next = pnewnode;
return true;
}
bool IsEmpty(DList plist)
{
return plist->next == NULL;
}
//头删
bool DelHead(DList plist)
{
if (IsEmpty(plist))
return false;
DNode* p = plist->next;
plist->next = p->next;
if (p->next != NULL)
{
p->next->prio = plist;
}
free(p);
}
//尾删
bool DelTail(DList plist)
{
if (IsEmpty(plist))
return false;
DNode* pTail2 = plist;
DNode* p = NULL;
while (pTail2->next->next != NULL)
{
pTail2 = pTail2->next;
}
p = pTail2->next;
pTail2->next = NULL;
free(p);
return true;
}
//按位删
bool DelPos(DList plist,int pos)
{
if (IsEmpty(plist))
return false;
if (pos < 0 || pos >= GetLength(plist))
return false;
DNode* p;
int i;
for (p = plist, i = 0; i < pos;i++, p = p->next);
DNode* q = p->next;//需要删除的点
p->next = q->next;
if (q->next != NULL)
{
q->next->prio = p;
}
free(q);
return true;
}
//销毁
void Destroy(DList plist)
{
DNode* p;
while (plist->next != NULL)
{
p = plist->next;
plist->next = p->next;
free(p);
}
}
void Show(DList plist)
{
DNode* pCur = plist->next;
while (pCur != NULL)
{
printf("%d ", pCur->data);
pCur = pCur->next;
}
printf("\n");
}
int main()
{
DNode head;
InitList(&head);
for (int i = 0; i < 10; i++)
{
Insert_head(&head, i);
}
Show(&head);
Delval(&head, 5);
Show(&head);
for (int i = 0; i < 10; i++)
{
Insert_tail(&head, i);
}
Show(&head);
Insert_pos(&head, 3, 99);
Show(&head);
DelHead(&head);
Show(&head);
DelTail(&head);
Show(&head);
DelPos(&head, 2);
Show(&head);
Destroy(&head);
return 0;
}