# 双向链表边的基本操作

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct _DLNODE
{
int data;
struct _DLNODE *pre;
struct _DLNODE *next;

#define SIZE sizeof(struct _DLNODE)

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");
printf("/nThe DL_A list you created is:/n");

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");

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");

printf("/nAfter reverse the list is :/n");

printf("/nAfter sorted the list is :/n");

printf("/nInput data of DL_B:/n");
printf("/nThe DL_B list you created is:/n");
printf("/nAfter merger DL_A and DL_B the DL_C is :/n");

Remove_memory(DL_C);
return 0;
}

{
if((*DL) == NULL)
{
printf("Memory assign error!!/n");
}
(*DL)->pre = (*DL)->next = NULL;
}

{
DLinkList *pd = NULL, *qd = NULL;
int data = 0, len = 1, endflag = 1;

pd = DL;
while(endflag)
{
printf("Input the %dth node's data :", len);
scanf("%d", &data);
if(data != 0)
{
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;
}

{
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 len = 0;

pd = DL;
while(pd != NULL)
{
pd = pd->next ;
len++;
}
return len;
}

{
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;
}

{
int len = 0, loc = 1;
DLinkList *pd = NULL, *newnode  = NULL;

if(newnode == NULL)
{
printf("Memory assign error!!/n");
exit(1);
}
newnode->data = key;

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 *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;
}

{
int i = 0, j = 0, len = 0;
DLinkList *p0 = NULL, *p1 = NULL, *p2 = NULL, *t = NULL;

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;
}

{

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 *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;

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;
}

{
DLinkList *pd = NULL, *qd = NULL;

pd = DL;
qd = pd->next ;
while(qd->next != 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