#include < stdio.h >
#include < stdlib.h >
#include < conio.h >
#define OK 1
#define OVERFLOW 0
#define ERROR 0
#define NULL 0
typedef struct LNode
{
int data;
struct LNode * next;
}LNode, * LinkList;
// 顺序创建一个链表
LinkList CreateListOrdinal_L( int num)
{
LinkList h,head,end;
int counter;
if ( ! (h = head = (LinkList)malloc( sizeof (LNode))))
{
return NULL;
}
h -> next = head -> next = NULL;
for (counter = 0 ;counter < num;counter ++ )
{
if (end = ((LinkList)malloc( sizeof (LNode))))
{
end -> data = rand();
end -> next = h -> next;
h -> next = end;
h = end;
}
}
return head;
}
// 逆序创建一个链表
LinkList CreateList_L( int num)
{
LinkList head,end;
int counter;
if ( ! (head = (LinkList)malloc( sizeof (LNode))))
{
return NULL;
}
head -> next = NULL;
for (counter = 0 ;counter < num;counter ++ )
{
if (end = (LinkList)malloc( sizeof (LNode)))
{
end -> data = rand();
end -> next = head -> next;
head -> next = end;
}
}
return head;
}
// 获取某个指定结点的值
int GetElement_L(LinkList list, int location)
{
int counter = 0 ;
LinkList point = list -> next;
while (point && counter < location)
{
point = point -> next;
counter ++ ;
}
if ( ! point || counter > location)
return ERROR;
return point -> data;
}
// 插入元素
int ListInsert_L(LinkList list, int location, int arg)
{
LinkList point = list -> next,node;
int counter = 1 ;
while (counter < location)
{
point = point -> next;
counter ++ ;
}
if ( ! point || (counter > location))
{
printf( " /nInsert operatoin failed " );
return ERROR;
}
node = (LinkList)malloc( sizeof (LNode));
if ( ! node)
exit(OVERFLOW);
node -> data = arg;
node -> next = point -> next;
point -> next = node;
return OK;
}
// 删除元素
void ListDelete_L(LinkList list, int location)
{
LinkList point = list -> next;
int counter = 1 ;
while (counter < location)
{
point = point -> next;
list = list -> next;
counter ++ ;
}
if ( ! point || (counter > location))
{
printf( " /nDelete operation failed!/n " );
return ;
}
list -> next = point -> next;
free(point);
}
// 快速排序
void BubbleSort(LinkList list)
{
int temp;
LinkList pa,pb;
pa = list -> next;
pb = pa -> next;
while (pa -> next)
{
if (pa -> data > pb -> data)
{
temp = pa -> data;
pa -> data = pb -> data;
pb -> data = temp;
}
pb = pb -> next;
if ( ! pb)
{
pa = pa -> next;
pb = pa -> next;
}
}
}
// 获取链表元素个数
int GetListLength_L(LinkList list)
{
int counter = 0 ;
LinkList point = list -> next;
while (point)
{
counter ++ ;
point = point -> next;
}
return counter;
}
// 合并两个链表,并将操作结果保留在第一个链表中。此操作结束后就只剩一个链表了
void MergeList_L(LinkList listA,LinkList listB)
{
LinkList pa = listA,pb = listB;
listA = listA -> next;
listB = listB -> next;
while (listA && listB)
{
if (listA -> data < listB -> data)
{
pa -> next = listA;
pa = listA;
listA = listA -> next;
}
else
{
pa -> next = listB;
pa = listB;
listB = listB -> next;
}
}
pa -> next = listA ? listA:listB;
free(pb);
}
// 列出列表的所有元素
void DisplayAllElements(LinkList list)
{
int flag = 1 ;
LinkList point = list -> next;
while (point)
{
printf( " %d/t " ,point -> data);
point = point -> next;
if (flag ++ == 5 )
printf( " /n " );
}
printf( " /n " );
}
//主函数
#include "head.h"
void main()
{
LinkList listA,listB;
printf("Create LinkList A/n");
listA = CreateListOrdinal_L(4);
ListInsert_L(listA,4,123);
ListDelete_L(listA,3);
BubbleSort(listA);
printf("After sort,the elements of listA are:/n");
DisplayAllElements(listA);
printf("Create LinkList B/n");
listB = CreateList_L(6);
BubbleSort(listB);
printf("After sort,the elements of listB are:/n");
DisplayAllElements(listB);
MergeList_L(listA,listB);
printf("/nAfter merge the two list together,the elements are:/n");
DisplayAllElements(listA);
printf("/nThe length of elements of listA is:%d",GetListLength_L(listA));
getch();
}