#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct _DLNODE
{
int data;
struct _DLNODE *pre;
struct _DLNODE *next;
}DLinkList;
#define SIZE sizeof(struct _DLNODE)
void Init_DLinkList(DLinkList **DL);
DLinkList *Create_DLinkList(DLinkList *DL);
void Disp_DLinkList(DLinkList *DL);
int Length_DLinkList(DLinkList *DL);
DLinkList *Delete_Dnode(DLinkList *DL, int key);
DLinkList *Insert_Dnode(DLinkList *DL, int pos, int key);
DLinkList *Reverse_DLinkList(DLinkList *DL);
DLinkList *Sort_DLinkList(DLinkList *DL);
DLinkList *Insert_Tail(DLinkList *DL, DLinkList *node);
DLinkList *Merger_DLinkList(DLinkList *DL_A, DLinkList *DL_B, DLinkList *DL_C);
void Remove_memory(DLinkList *DL);
int main()
{
DLinkList *DL_A = NULL, *DL_B = NULL, *DL_C = NULL;
int Len_a = 0, pos = 0;
int key = 0;
printf("Input data of DL_A:/n");
DL_A = Create_DLinkList(DL_A);
printf("/nThe DL_A list you created is:/n");
Disp_DLinkList(DL_A);
Len_a = Length_DLinkList(DL_A);
printf("/nThe length of DL_A list is: %d/n", Len_a);
printf("/nInput the key to deleting: ");
scanf("%d", &key);
DL_A = Delete_Dnode(DL_A, key);
printf("After deleted the list is :/n");
Disp_DLinkList(DL_A);
printf("/nInput the pos and the key to insert:/n");
scanf("%d%d", &pos, &key);
DL_A = Insert_Dnode(DL_A, pos, key);
printf("After inserting the list is :/n");
Disp_DLinkList(DL_A);
printf("/nAfter reverse the list is :/n");
DL_A = Reverse_DLinkList(DL_A);
Disp_DLinkList(DL_A);
printf("/nAfter sorted the list is :/n");
DL_A = Sort_DLinkList(DL_A);
Disp_DLinkList(DL_A);
printf("/nInput data of DL_B:/n");
DL_B = Create_DLinkList(DL_B);
printf("/nThe DL_B list you created is:/n");
Disp_DLinkList(DL_B);
DL_B = Sort_DLinkList(DL_B);
// DL_A = Sort_DLinkList(DL_A);
printf("/nAfter merger DL_A and DL_B the DL_C is :/n");
DL_C = Merger_DLinkList(DL_A, DL_B, DL_C);
Disp_DLinkList(DL_C);
Remove_memory(DL_C);
return 0;
}
void Init_DLinkList(DLinkList **DL)
{
(*DL) = (DLinkList *)malloc(SIZE);
if((*DL) == NULL)
{
printf("Memory assign error!!/n");
}
(*DL)->pre = (*DL)->next = NULL;
}
DLinkList *Create_DLinkList(DLinkList *DL)
{
DLinkList *pd = NULL, *qd = NULL;
int data = 0, len = 1, endflag = 1;
Init_DLinkList(&DL);
pd = DL;
while(endflag)
{
printf("Input the %dth node's data :", len);
scanf("%d", &data);
if(data != 0)
{
qd = (DLinkList *)malloc(SIZE);
qd->data = data;
pd->next = qd;
qd->pre = pd;
qd->next = NULL;
pd = qd;
len++;
}
else
{
endflag = 0;
}
}
DL = DL->next;
DL->pre = NULL;
pd->next = NULL;
return DL;
}
void Disp_DLinkList(DLinkList *DL)
{
DLinkList *pd = NULL;
int n = 1;
pd = DL;
while(pd != NULL)
{
printf("The %dth node's data is: %d/n", n, pd->data );
pd = pd->next ;
n ++;
}
}
int Length_DLinkList(DLinkList *DL)
{
DLinkList *pd = NULL;
int len = 0;
pd = DL;
while(pd != NULL)
{
pd = pd->next ;
len++;
}
return len;
}
DLinkList *Delete_Dnode(DLinkList *DL, int key)
{
DLinkList *pd = NULL, *qd = NULL;
pd = DL;
while(pd->data != key && pd->next != NULL)
{
pd = pd->next;
}
if(key == pd->data)
{
if(pd == DL)
{
DL = DL->next;
DL->pre = NULL;
free(pd);
pd = NULL;
}
else if(pd->next == NULL)
{
pd->pre->next = NULL;
free(pd);
pd = NULL;
}
else
{
pd->pre->next = pd->next;
pd->next->pre = pd->pre;
free(pd);
pd = NULL;
}
}
else
{
printf("/nNo found data : %d/n", key);
}
return DL;
}
DLinkList *Insert_Dnode(DLinkList *DL, int pos, int key)
{
int len = 0, loc = 1;
DLinkList *pd = NULL, *newnode = NULL;
newnode = (DLinkList *)malloc(SIZE);
if(newnode == NULL)
{
printf("Memory assign error!!/n");
exit(1);
}
newnode->data = key;
len = Length_DLinkList(DL);
if(pos < 1 || pos > len + 1)
{
printf("Position error!!/n");
exit(1);
}
pd = DL;
while(loc < pos && pd->next != NULL)
{
pd = pd->next;
loc++;
}
if(loc == pos)
{
if(pd == DL)
{
newnode->next = pd;
pd->pre = newnode;
newnode->pre = NULL;
DL = newnode;
}
else
{
pd->pre->next = newnode;
newnode->next = pd ;
newnode->pre = pd->pre ;
pd->pre = newnode;
}
}
else
{
pd->next = newnode;
newnode->pre = pd;
newnode->next = NULL;
}
return DL;
}
DLinkList *Reverse_DLinkList(DLinkList *DL)
{
DLinkList *p0 = NULL, *p1 = NULL, *p2 = NULL;
p1 = DL;
p2 = DL->next ;
while(p2)
{
p0 = p2->next ;
p2->next = p1;
p2->pre = p1->pre ;
p1 = p2;
p2 = p0;
}
DL->next = NULL;
DL = p1;
return DL;
}
DLinkList *Sort_DLinkList(DLinkList *DL)
{
int i = 0, j = 0, len = 0;
DLinkList *p0 = NULL, *p1 = NULL, *p2 = NULL, *t = NULL;
len = Length_DLinkList(DL);
p1 = DL;
if(p1->next == NULL)
{
DL = p1;
}
else if(p1->next->next == NULL)
{
p2 = p1->next;
if(p1->data > p2->data)
{
DL = p2;
p2->pre = NULL;
p2->next = p1;
p1->pre = p2;
p1->next = NULL;
}
}
else
{
for(i = 0; i < len -1; ++i)
{
p1 = DL;
for(j = 0; j < len -1 - i; ++j)
{
p2 = p1->next;
if(p1->data > p2->data)
{
if(p1 == DL)
{
DL = p2;
p1->next = p2->next;
p2->next = p1;
p2->pre = NULL;
p1->pre = p2;
p2->next->pre = p1;
t = p1;
p1 = p2;
p2 = t;
}
else if(p2->next == NULL)
{
p0->next = p2;
p2->next = p1;
p1->next = NULL;
p2->pre = p0;
p1->pre = p2;
break;
}
else
{
p0->next = p2;
p1->next = p2->next ;
p2->next = p1;
p2->pre = p0;
p1->pre = p2;
p2->next->pre = p1;
t = p1;
p1 = p2;
p2 = t;
}
}
p0 = p1;
p1 = p2;
p2 = p2->next;
}
}
}
return DL;
}
DLinkList *Insert_Tail(DLinkList *DL, DLinkList *node)
{
DLinkList *pd = NULL;
pd = DL;
if(pd == NULL)
{
DL = node;
node->next = NULL;
node->pre = pd;
}
else
{
while(pd->next != NULL)
{
pd = pd->next;
}
pd->next = node;
node->next = NULL;
node->pre = pd;
}
return DL;
}
DLinkList *Merger_DLinkList(DLinkList *DL_A, DLinkList *DL_B, DLinkList *DL_C)
{
DLinkList *node_a = NULL, *node_b = NULL;
int len_a = 0, len_b = 0, i = 1, j =1, k = 1;
static int flag_a = 0, flag_b = 0;
flag_a = i;
flag_b = j;
len_a = Length_DLinkList(DL_A);
len_b = Length_DLinkList(DL_B);
while(i <= len_a && j <= len_b)
{
if(k == 1 || flag_a != i)
{
flag_a = i;
node_a = DL_A;
DL_A = DL_A->next;
if(i != len_a)
{
DL_A->pre = NULL;
}
}
if(k == 1 || flag_b != j)
{
flag_b = j;
node_b = DL_B;
DL_B = DL_B->next;
if(j != len_b)
{
DL_B->pre = NULL;
}
}
if(node_a->data < node_b->data)
{
DL_C = Insert_Tail(DL_C, node_a);
i++;
k++;
}
else if(node_a->data == node_b->data)
{
DL_C = Insert_Tail(DL_C, node_a);
i++;
k++;
DL_C = Insert_Tail(DL_C, node_b);
j++;
k++;
}
else
{
DL_C = Insert_Tail(DL_C, node_b);
j++;
k++;
}
}
while(i <= len_a)
{
DL_C = Insert_Tail(DL_C, node_a);
if(i != len_a)
{
node_a = DL_A;
DL_A = DL_A->next;
}
i++;
}
while(j <= len_b )
{
DL_C = Insert_Tail(DL_C, node_b);
if(j != len_b)
{
node_b = DL_B;
DL_B = DL_B->next;
}
j++;
}
return DL_C;
}
void Remove_memory(DLinkList *DL)
{
DLinkList *pd = NULL, *qd = NULL;
pd = DL;
qd = pd->next ;
while(qd->next != NULL)
{
DLinkList *t = NULL;
t = qd;
pd->next = qd->next;
qd->next->pre = pd;
qd = qd->next;
free(t);
t = NULL;
}
free(qd);
qd = NULL;
free(DL);
DL = NULL;
}
/*
Input data of DL_A:
Input the 1th node's data :3
Input the 2th node's data :1
Input the 3th node's data :4
Input the 4th node's data :0
The DL_A list you created is:
The 1th node's data is: 3
The 2th node's data is: 1
The 3th node's data is: 4
The length of DL_A list is: 3
Input the key to deleting: 1
After deleted the list is :
The 1th node's data is: 3
The 2th node's data is: 4
Input the pos and the key to insert:
1
2
After inserting the list is :
The 1th node's data is: 2
The 2th node's data is: 3
The 3th node's data is: 4
After reverse the list is :
The 1th node's data is: 4
The 2th node's data is: 3
The 3th node's data is: 2
After sorted the list is :
The 1th node's data is: 2
The 2th node's data is: 3
The 3th node's data is: 4
Input data of DL_B:
Input the 1th node's data :1
Input the 2th node's data :3
Input the 3th node's data :8
Input the 4th node's data :6
Input the 5th node's data :5
Input the 6th node's data :9
Input the 7th node's data :0
The DL_B list you created is:
The 1th node's data is: 1
The 2th node's data is: 3
The 3th node's data is: 8
The 4th node's data is: 6
The 5th node's data is: 5
The 6th node's data is: 9
After merger DL_A and DL_B the DL_C is :
The 1th node's data is: 1
The 2th node's data is: 2
The 3th node's data is: 3
The 4th node's data is: 3
The 5th node's data is: 4
The 6th node's data is: 5
The 7th node's data is: 6
The 8th node's data is: 8
The 9th node's data is: 9
Press any key to continue
*/s