#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct _LNODE
{
int data;
struct _LNODE *next;
}LinkList;
#define SIZE sizeof(struct _LNODE)
void Init_List(LinkList **L);
LinkList *Create_List(LinkList *L);
void Disp_List(LinkList *L);
int length_List(LinkList *L);
LinkList *Insert_node(LinkList *L, int pos, int key);
LinkList *Delete_node(LinkList *L, int key);
LinkList *Reverse_List(LinkList *L);
LinkList *Sort_List(LinkList *L);
LinkList *Get_head(LinkList *L);
LinkList *Inert_tail(LinkList *L, LinkList *node);
LinkList *Merge_List(LinkList *LA, LinkList *LB, LinkList *LC);
void Remove_Memory(LinkList *L);
int main()
{
LinkList *LA = NULL, *LB = NULL, *LC = NULL;
int length = 0;
int pos = 0, key = 0;
printf("Input the list LA's data:/n");
LA = Create_List(LA);
printf("/nThe list LA you created is :/n");
Disp_List(LA);
length = length_List(LA);
printf("/nThe length of the list is : %d/n", length);
printf("/nInput the position and key to insert:/n");
scanf("%d%d", &pos, &key);
LA = Insert_node(LA, pos, key);
printf("After insert the list is :/n");
Disp_List(LA);
printf("/nInput the key to delete:/n");
scanf("%d",&key);
LA = Delete_node(LA, key);
printf("/nAfter delte the list is :/n");
Disp_List(LA);
printf("/nAfter reverse the list is :/n");
LA = Reverse_List(LA);
Disp_List(LA);
printf("/nAfter sorted the list is :/n");
LA = Sort_List(LA);
Disp_List(LA);
printf("/nInput the list LB's data:/n");
LB = Create_List(LB);
printf("/nThe list LA you created is :/n");
Disp_List(LB);
printf("/nAfter merger List LA and List LB the LC is:/n");
LA = Sort_List(LA);
LB = Sort_List(LB);
LC = Merge_List(LA, LB, LC);
Disp_List(LC);
Remove_Memory(LC);
return 0;
}
void Init_List(LinkList **L)
{
(*L) = (LinkList *)malloc(SIZE);
if((*L) == NULL)
{
printf("Memory assign error!!/n");
exit(1);
}
(*L)->next = NULL;
}
LinkList *Create_List(LinkList *L)
{
LinkList *head = NULL, *p = NULL;
int endflag = 1, num = 1, data;
Init_List(&L);
head = L;
while(endflag)
{
printf("Inpput the %dth node's data: ", num);
scanf("%d", &data );
if(data != 0)
{
p = (LinkList *)malloc(SIZE);
p->data = data;
head->next = p;
p->next = NULL;
head = p;
num++;
}
else
{
endflag = 0;
}
}
L = L->next;
head->next = NULL;
return L;
}
void Disp_List(LinkList *L)
{
LinkList *p = NULL;
int len = 1;
p = L;
while(p != NULL)
{
printf("The %dth node's data is : %d/n", len, p->data );
p = p->next ;
len++;
}
}
int length_List(LinkList *L)
{
int len = 0;
LinkList *p = NULL;
p = L;
while(p != NULL)
{
p = p->next ;
len++;
}
return len;
}
void Remove_Memory(LinkList *L)
{
LinkList *p = NULL, * q =NULL;
p = L;
q = L->next;
while(q != NULL)
{
LinkList * t= NULL;
t = q;
p->next = q->next;
q = q->next ;
free(t);
t = NULL;
}
free(L);
L = NULL;
}
LinkList *Insert_node(LinkList *L, int pos, int key)
{
int maxlen = 0, index = 1;
LinkList *p = NULL, *q = NULL, *newnode = NULL;
p = L;
maxlen = length_List(L);
newnode = (LinkList *)malloc(SIZE);
newnode->data = key;
if(pos < 1 || pos > maxlen + 1)
{
printf("Posotion error!!/n");
exit(1);
}
while(index < pos && p->next != NULL)
{
index++;
q = p;
p = p->next;
}
if( index == pos)
{
if(L == p)
{
newnode->next = p;
L = newnode;
}
else
{
q->next = newnode;
newnode->next = p;
}
}
else
{
p->next = newnode;
newnode->next = NULL;
}
return L;
}
LinkList *Delete_node(LinkList *L, int key)
{
LinkList *p = NULL, *q = NULL;
p = L;
while(key != p->data && p->next != NULL)
{
q = p;
p = p->next ;
}
if(key == p->data )
{
if(L == q)
{
L = p;
free(q);
q = NULL;
}
else if(p->next == NULL)
{
q->next = NULL;
free(p);
p = NULL;
}
else
{
q->next = p->next;
free(p);
p = NULL;
}
}
else
{
printf("No found data : %d in the list!/n", key);
}
return L;
}
LinkList *Reverse_List(LinkList *L)
{
LinkList *p1 = NULL, *p2 = NULL, *p3 = NULL;
p1 = L;
p2= p1->next ;
while(p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
L->next = NULL;
L = p1;
return L;
}
LinkList *Sort_List(LinkList *L)
{
int len = 0, i = 0, j = 0;
LinkList *p1 = NULL, *p2 = NULL, *p3 = NULL;
LinkList *t = NULL;
p1 = L;
len = length_List(L);
if(p1->next == NULL)
{
L = p1;
}
else if(p1->next->next == NULL)
{
p2 = p1->next ;
if(p1->data > p2->data )
{
L = p2;
p2->next = p1;
p1->next = NULL;
}
}
else
{
for(i = 0; i < len - 1; ++i)
{
p1 = L;
for(j = 0; j < len - i -1; ++j)
{
p2 = p1->next ;
if(p1->data > p2->data )
{
if(p1 == L)
{
L = p2;
p1->next = p2->next;
p2->next = p1;
t = p1;
p1 = p2;
p2 = t;
}
else if(p2->next == NULL)// 結束條件
{
p3->next = p2;
p2->next = p1;
p1->next = NULL;
break;
}
else
{
p3->next = p2;
p1->next = p2->next;
p2->next = p1;
t = p1;
p1 = p2;
p2 = t;
}
}
p3 = p1;
p1 = p2;
p2 = p2->next;
}
}
}
return L;
}
LinkList *Get_head(LinkList *L)
{
LinkList *q = NULL;
q = L;
if(q != NULL)
{
L = L->next ;
return q;
}
else
{
return NULL;
}
}
LinkList *Inert_tail(LinkList *L, LinkList *node)
{
LinkList *p = NULL;
p = L;
if(L == NULL)
{
L = node;
node->next = NULL;
}
else
{
while(p->next != NULL)
{
p = p->next;
}
p->next = node;
node ->next = NULL;
}
return L;
}
LinkList *Merge_List(LinkList *LA, LinkList *LB, LinkList *LC)
{
int len_a = 0, len_b = 0;
LinkList *V_a = NULL, *V_b = NULL;
int i = 1, j = 1, k = 1;
static int flag1 = 0, flag2 = 0;
len_a = length_List(LA);
len_b = length_List(LB);
flag1 = i;
flag2 = j;
while((i <= len_a) && (j <= len_b))
{
if((k == 1) || flag1 != i)
{
flag1 = i;
V_a = LA;
LA = LA->next;
}
if((k == 1) || flag2 != j)
{
flag2 = j;
V_b = LB;
LB = LB->next;
}
if(V_a->data < V_b->data )
{
LC = Inert_tail(LC, V_a);
i++;
k++;
}
else if(V_a->data == V_b->data )
{
LC = Inert_tail(LC, V_a);
i++;
k++;
LC = Inert_tail(LC, V_b);
j++;
k++;
}
else
{
LC = Inert_tail(LC, V_b);
j++;
k++;
}
}
while(i <= len_a)
{
LC = Inert_tail(LC, V_a);
V_a = LA;
LA = LA->next;
i++;
k++;
}
while(j <= len_b)
{
LC = Inert_tail(LC, V_b);
V_b = LB;
LB = LB->next;
j++;
}
return LC;
}
/*
Input the list LA's data:
Inpput the 1th node's data: 2
Inpput the 2th node's data: 1
Inpput the 3th node's data: 4
Inpput the 4th node's data: 7
Inpput the 5th node's data: 0
The list LA you created is :
The 1th node's data is : 2
The 2th node's data is : 1
The 3th node's data is : 4
The 4th node's data is : 7
The length of the list is : 4
Input the position and key to insert:
2
5
After insert the list is :
The 1th node's data is : 2
The 2th node's data is : 5
The 3th node's data is : 1
The 4th node's data is : 4
The 5th node's data is : 7
Input the key to delete:
1
After delte the list is :
The 1th node's data is : 2
The 2th node's data is : 5
The 3th node's data is : 4
The 4th node's data is : 7
After reverse the list is :
The 1th node's data is : 7
The 2th node's data is : 4
The 3th node's data is : 5
The 4th node's data is : 2
After sorted the list is :
The 1th node's data is : 2
The 2th node's data is : 4
The 3th node's data is : 5
The 4th node's data is : 7
Input the list LB's data:
Inpput the 1th node's data: 3
Inpput the 2th node's data: 4
Inpput the 3th node's data: 8
Inpput the 4th node's data: 1
Inpput the 5th node's data: 0
The list LA you created is :
The 1th node's data is : 3
The 2th node's data is : 4
The 3th node's data is : 8
The 4th node's data is : 1
After merger List LA and List LB the LC 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 : 4
The 5th node's data is : 4
The 6th node's data is : 5
The 7th node's data is : 7
The 8th node's data is : 8
Press any key to continue
*/