双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。
双向链表的实现代码
函数的声明:(头文件名DSeqList.h)
#include<stdio.h>
typedef int ElemType;
typedef struct Node// 创建结点结构
{
ElemType data;
struct Node* prev;
struct Node* next;
}Node,*DPList;
void init(DPList plist);//初始化结点
int getlength(DPList plist);//获取双向链表的长度
Node* buyNode(ElemType val);//申请结点
int insertHead(DPList plist,ElemType val);//头插
int insertTail(DPList plist,ElemType val);//尾插
int insertPos(DPList plist,int pos,ElemType val);//按位置插入
int empty(DPList plist);//判空
int deleteHead(DPList plist);//头删
int deleteTail(DPList plist);//尾删
int deletePos(DPList plist,int pos);//按位置删除
void Show(DPList plist);//打印
void destory(DPList plist);//销毁
函数的实现:(源文件名DSeqLisr.cpp)
#include<stdio.h>
#include<stdlib.h>
#include"DSeqlist.h"
void init(DPList plist)//初始化结点
{
if(plist ==NULL)
{
exit(0);
}
plist->prev = NULL;
plist->next = NULL;
}
Node* buyNode(ElemType val)//申请结点
{
Node* pnewnode = (Node*)malloc(sizeof(Node));
pnewnode->prev = pnewnode->next = NULL;
pnewnode->data = val;
return pnewnode;
}
int insertHead(DPList plist,ElemType val)//头插
{
Node* pnewnode = buyNode(val);
pnewnode->next = plist->next;
pnewnode->prev = plist;
if(plist->next != NULL)
{
plist->next->prev = pnewnode;
}
plist->next = pnewnode;
return 1;
}
int insertTail(DPList plist,ElemType val)//尾插
{
Node* pnewnode = buyNode(val);
Node* pTail = plist;
while(pTail->next != NULL)
{
pTail = pTail->next;
}
pnewnode->prev = pTail;
pTail->next = pnewnode;
return 1;
}
int getlength(DPList plist)//获取双向链表的长度
{
int count = 0;
Node* pCur = plist->next;
while(pCur != NULL)
{
pCur = pCur->next;
count++;
}
return count;
}
int insertPos(DPList plist,int pos,ElemType val)//按位置插入
{
Node* pfront = plist;
if(pos < 0 || pos > getlength(plist))
{
return 0;
}
for(int i = 0; i < pos; i++)
{
pfront = pfront->next;
}
Node* pnewnode =buyNode(val);
pnewnode->next = pfront->next;
pnewnode->prev = pfront;
if (pfront->next != NULL)
{
pfront->next->prev = pnewnode;
}
pfront->next = pnewnode;
return 1;
}
void Show(DPList plist)//打印
{
Node* pCur = plist->next;
while(pCur != NULL)
{
printf("%d ",pCur->data);
pCur = pCur->next;
}
printf("\n");
}
int empty(DPList plist)//判空
{
return plist->next == NULL ? 1 : 0;
}
int deleteHead(DPList plist)//头删
{
if(empty(plist))
{
return 0;
}
Node* pCur = plist->next;
plist->next = pCur->next;
if(pCur->next != NULL)
{
pCur->next->prev = plist;
}
free(pCur);
return 1;
}
int deleteTail(DPList plist)//尾删
{
if(empty(plist))
{
return 0;
}
Node* ptail2 = plist;
Node* pCur = NULL;
while(ptail2->next != NULL)
{
if(ptail2->next->next == NULL)
{
break;
}
ptail2 = ptail2->next;
}
pCur = ptail2->next;
ptail2->next = NULL;
free(pCur);
return 1;
}
int deletePos(DPList plist,int pos)//按位置删除
{
if(pos < 0 || pos >= getlength(plist))
{
return 0;
}
Node* pfront = plist;
Node* pCur = NULL;
for(int i = 0; i < pos; i++)
{
pfront = pfront->next;
}
pCur = pfront->next;
pfront->next = pCur->next;
if(pCur->next != NULL)
{
pCur->next->prev = pfront;
}
free(pCur);
return 1;
}
void destory(DPList plist)//销毁
{
Node* pCur = plist->next;
Node* pNext= NULL;
while(pCur != NULL)
{
pNext = pCur->next;
delete pCur;
pCur = pNext;
}
plist->next = NULL;
}
主函数:(源文件名main.cpp)
#include<stdio.h>
#include"DSeqList.h"
int main()
{
Node head;
init(&head);
for(int i = 0; i < 3; i++)
{
insertHead(&head,i+1);//3 2 1
}
Show(&head);
for(int i = 0; i < 3; i++)
{
insertTail(&head, i + 1);//3 2 1 1 2 3
}
Show(&head);
insertPos(&head,5,100);//3 2 1 1 2 100 3
Show(&head);
deleteHead(&head);//2 1 1 2 100 3
Show(&head);
deleteTail(&head);//2 1 1 2 100
Show(&head);
deletePos(&head,4);
Show(&head);//2 1 1 2
destory(&head);//空行
Show(&head);
return 0;
}
运行结果: