数据结构复习 双链表
作者:
liugua
,
2022-06-03 19:42:29
,
所有人可见
,
阅读 228
#include <bits/stdc++.h>
using namespace std;
typedef struct DNode{
int data;
DNode *prior,*next;
}DNode,*DLinkList;
bool InitDLinkList(DLinkList &L)
{
L = (DNode*)malloc(sizeof(DNode));
if(L == NULL) return false;
L -> prior = NULL;
L -> next = NULL;
return true;
}
bool Empty(DLinkList L)
{
if(L -> next == NULL)
return true;
return false;
}
bool InsertNextDNode(DLinkList &L,DNode *p,DNode *s)
{
if(p == NULL || s == NULL)
return false;
s -> next = p -> next;
if(p -> next != NULL)
p -> next -> prior = s;
s -> prior = p;
p -> next = s;
return true;
}
bool InsertPriorDNode(DLinkList &L,DNode *p,DNode *s)
{
return InsertNextDNode(L,p->prior,s);
}
bool DeleteNextNode(DNode *p)
{
if(p == NULL) return false;
DNode *q = p -> next;
if(q == NULL) return false;
p -> next = q -> next;
if(q -> next != NULL)
q -> next -> prior = p;
free(q);
return true;
}
void DestoryList(DLinkList &L)
{
while(L -> next != NULL)
DeleteNextNode(L);
free(L);
L = NULL;
}
void print(DLinkList &L)
{
DNode *p = L;
while(p -> next != NULL)
{
p = p -> next;
cout << p -> data << ' ';
}
cout << endl;
return;
}
int main()
{
DLinkList L;
InitDLinkList(L);
cout << Empty(L) << endl;
DNode *s = (DNode*)malloc(sizeof(DNode));
s -> data = 6;
s -> next = NULL;
s -> prior = NULL;
InsertNextDNode(L,L,s);
print(L);
DNode *p = (DNode*)malloc(sizeof(DNode));
p -> data = 3;
InsertPriorDNode(L,s,p);
print(L);
DeleteNextNode(L);
print(L);
DestoryList(L);
return 0;
}