“DLinkList.h”
#include<stdio.h>
#include<stdlib.h>
typedef char DLinkType;
typedef struct DLinkNode {
DLinkType data;
struct DLinkNode* next;
struct DLinkNode* prev;
} DLinkNode, DLinkList;
DLinkNode* CreateNode(DLinkType value);
void DLinkListInit(DLinkNode** head);
void DLinkListPushBack(DLinkNode** head, DLinkType value);
void print(DLinkNode* head);
void DLinListPopBack(DLinkNode* head);
void DLinkListPushFront(DLinkNode* head, DLinkType value);
void DLinkListPopFront(DLinkNode* head);
void DLinkListInsert(DLinkNode* head, DLinkNode* pos, DLinkType value);
DLinkList.cpp
#include"DLinkList.h"
DLinkNode* CreateNode(DLinkType value)
{
DLinkNode *new_Node = (DLinkNode*)malloc(sizeof(DLinkNode));
new_Node->data = value;
new_Node->next = NULL;
new_Node->prev = NULL;
return new_Node;
}
void DLinkListInit(DLinkNode** head)
{
DLinkNode* newnode = CreateNode();
if (head == NULL)
return;
*head = newnode;
(*head)->next = *head ;
(*head)->prev = *head;
}
void DLinkListPushBack(DLinkNode** head, DLinkType value)
{
if (head == NULL)
return;
DLinkNode* cur = *head;
DLinkNode* newnode = CreateNode(value);
if ((*head) == NULL)
cur = newnode;
else
{
while (cur->next != *head)
cur = cur->next;
cur->next = newnode;
newnode->prev = cur;
newnode->next = *head;
(*head)->prev = newnode;
}
}
void print(DLinkNode* head)
{
if (head == NULL)
return;
DLinkNode* cur = head->next;
if (cur == head)
return;
else
{
while (cur != head)
{
printf("%c ", cur->data);
cur = cur->next;
}
}
}
void DLinListPopBack(DLinkNode* head)
{
DLinkNode* cur = head->next;
DLinkNode* to_delete;
if (head == NULL)
return;
else
{
if (cur == head)
{
cur->next = head;
head->prev = cur;
}
else
{
while (cur->next != head)
cur = cur->next;
to_delete = cur;
cur->next->prev = cur->prev;
cur->prev->next = cur->next;
free(to_delete);
}
}
}
void DLinkListPushFront(DLinkNode* head, DLinkType value)
{
DLinkNode* cur = head->next;
DLinkNode* newnode = CreateNode(value);
if (cur == NULL)
{
cur = newnode;
}
else
{
newnode->next = head->next;
newnode->prev=head->next->prev;
head->next = newnode;
newnode->prev = head;
}
}
void DLinkListPopFront(DLinkNode* head)
{
if (head == NULL)
return;
DLinkNode* cur = head->next;
DLinkNode* to_delete;
if (cur == head)
return;
else
{
if (cur == head)
{
cur->next = head;
head->next = cur;
free(head);
}
else
{
to_delete = cur;
head->next=cur->next;
cur->next->prev = head;
free(to_delete);
}
}
}
void DLinkListInsert(DLinkNode* head,DLinkNode* pos, DLinkType value)
{
if (head == NULL&&pos == NULL)
return;
DLinkNode* cur = head->next;
DLinkNode* newnode = CreateNode(value);
if (cur->next == head)
{
if (cur->next == pos)
{
head->next = newnode;
newnode->prev = head;
newnode->next = cur;
cur->prev = newnode;
}
else
{
return;
}
}
else
{
while (cur != pos)
cur = cur->next;
if (cur != head)
{
newnode->next = cur->next;
newnode->prev = cur->next->prev;
cur->next = newnode;
newnode->prev = cur;
}
else
return;
}
}
text.cpp
#include"DLinkList.h"
int main()
{
DLinkList *L;
DLinkListInit(&L);
DLinkListPushBack(&L, '9');
DLinkListPushBack(&L, '5');
DLinkListPushBack(&L, '+');
DLinListPopBack(L);
DLinkListPushFront(L, '5');
DLinkListPopFront(L);
DLinkListPopFront(L);
DLinkListPopFront(L);
DLinkListPopFront(L);
DLinkListInsert(L, L, '1');
print(L);
system("pause");
return 0;
}