#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct {
double a;
int b;
}ElemType;
typedef struct Lnode {
ElemType elem;
struct Lnode* next;
}Lnode, *LinkList;
bool compare(ElemType a, ElemType b);
int compare_size(ElemType a, ElemType b);
void visit(ElemType elem);
void CreateList_H(LinkList& L, int n); //头插法建表
void CreateList_R(LinkList& L, int n); //尾插法建表
Status InitList(LinkList &L); //初始化线性表
Status DestroyList(LinkList &L); //销毁线性表
Status ClearList(LinkList &L); //清空线性表
Status ListEmpty(LinkList L); //判断线性表是否为空
Status ListLength(LinkList L); //返回线性表中元素的个数
Status GetElem(LinkList L, int i, ElemType &e); //返回线性表中第i个元素的值
Status LocateElem(LinkList L, ElemType e); //返回元素e在线性表中的位置
Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e); //返回当前元素e的直接前驱元素
Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e); //返回当前元素e的直接后继元素
Status ListInsert(LinkList &L, int i, ElemType e); //将元素e插入线性表中第i个位置
Status ListDelete(LinkList &L, int i, ElemType &e); //将线性表中第i个位置的元素删除,并用e返回
void ListTraverse(LinkList L); //依次遍历线性表中的每一个元素
void Union(LinkList& La, LinkList Lb); //线性表的合并
void MergeList(LinkList La, LinkList Lb, LinkList& Lc); //有序链表的合并
int main()
{
LinkList La = NULL, Lb = NULL, Lc = NULL;
int sum_a = 0, sum_b = 0;
scanf("%d", &sum_a);
CreateList_H(La,sum_a); //检验头插法建表
ListTraverse(La);
printf("-------------------------------------\n");
scanf("%d", &sum_b);
CreateList_R(Lb,sum_b); //检验尾插法建表
ListTraverse(Lb);
printf("-------------------------------------\n");
//Union(La, Lb);
MergeList(La, Lb, Lc);
ListTraverse(Lc);
/*if (InitList(L) == 1)
{
int sum = 0;
scanf("%d", &sum);
ElemType elem, pre_e, next_e;
for (int i = 0; i < sum; i++)
{
scanf("%lf%d", &elem.a, &elem.b);
ListInsert(L, i + 1, elem);
}
printf("-------------------------------------\n\n");
if (GetElem(L, 2, elem))
{
printf("第2个元素是:");
visit(elem);
printf("---------------------------------\n\n");
}
else
printf("线性表中没有第2个元素\n");
int position = LocateElem(L, elem);
if (position)
{
printf("当前元素是线性表中第%d个元素\n", position);
printf("---------------------------------\n");
}
else
printf("当前元素不在线性表中\n");
if (PriorElem(L, elem, pre_e))
{
printf("当前元素的直接前驱元素是:");
visit(pre_e);
printf("---------------------------------\n\n");
}
else
printf("当前元素没有直接前驱元素!\n");
if (NextElem(L, elem, next_e))
{
printf("当前元素的直接后继元素是:");
visit(next_e);
printf("---------------------------------\n\n");
}
else
printf("当前元素没有直接后继元素!\n");
if (ListDelete(L, 1, elem))
{
printf("删除线性表中第1个位置的元素elem是:");
visit(elem);
printf("---------------------------------\n\n");
}
else
printf("删除元素失败\n");
ListTraverse(L);
printf("-------------------------------------\n\n");
int length = ListLength(L);
printf("线性表的长度为:%d\n\n", length);
ClearList(L);
if (ListEmpty(L))
printf("线性表为空!\n\n");
else
printf("线性表不为空!\n\n");
if(DestroyList(L))
printf("线性表销毁成功!\n\n");
else
printf("线性表销毁失败!\n\n");
}
else
{
printf("初始化线性表失败!");
}*/
return 0;
}
Status InitList(LinkList& L)
{
L = (LinkList)malloc(sizeof(Lnode));
L->next = NULL;
if (!L)
exit(OVERFLOW);
return OK;
}
Status DestroyList(LinkList& L)
{
Lnode* p = NULL;
while (L)
{
p = L;
L = L->next;
free(p);
}
return OK;
}
Status ClearList(LinkList& L)
{
Lnode *p = NULL, *q = NULL;
p = L->next;
while (p)
{
q = p;
p = p->next;
free(q);
}
L->next = NULL;
return OK;
}
Status ListEmpty(LinkList L)
{
if (L->next)
return FALSE;
else
return TRUE;
}
Status ListLength(LinkList L)
{
int length = 0;
Lnode* p = L->next;
while (p)
{
length++;
p = p->next;
}
return length;
}
Status GetElem(LinkList L, int i, ElemType& e)
{
if (i < 1)
return ERROR;
int j = 1;
Lnode* p = L->next;
while (p && j < i)
{
j++;
p = p->next;
}
if (!p || j > i)
return ERROR;
e = p->elem;
return OK;
}
bool compare(ElemType a, ElemType b)
{
if (a.a == b.a && a.b == b.b)
return TRUE;
else
return FALSE;
}
Status LocateElem(LinkList L, ElemType e)
{
int count = 0;
Lnode* p = L->next;
while (p)
{
count++;
if (compare(p->elem, e))
return count;
p = p->next;
}
return FALSE;
}
Status PriorElem(LinkList L, ElemType cur_e, ElemType& pre_e)
{
Lnode* p = L->next, * q = L;
while (p)
{
if (compare(p->elem, cur_e))
{
pre_e = q->elem;
return OK;
}
q = p;
p = p->next;
}
return ERROR;
}
Status NextElem(LinkList L, ElemType cur_e, ElemType& next_e)
{
Lnode* p = L->next;
while (p)
{
if (compare(p->elem, cur_e))
{
next_e = p->next->elem;
return OK;
}
p = p->next;
}
return ERROR;
}
Status ListInsert(LinkList& L, int i, ElemType e)
{
Lnode* p = L;
int j = 0;
while (p && j < i - 1)
{
p = p->next;
j++;
}
if (!p || j > i - 1)
return ERROR;
Lnode* s = (Lnode*)malloc(sizeof(Lnode));
s->elem = e;
s->next = p->next;
p->next = s;
return OK;
}
Status ListDelete(LinkList& L, int i, ElemType &e)
{
Lnode* p = L->next, * q = L;
int j = 1;
while (p && j < i)
{
q = p;
p = p->next;
j++;
}
if (!p || j > i)
return ERROR;
e = p->elem;
q->next = p->next;
free(p);
return OK;
}
void visit(ElemType elem)
{
printf("%lf %d\n", elem.a, elem.b);
}
void ListTraverse(LinkList L)
{
Lnode* p = L->next;
while (p)
{
visit(p->elem);
p = p->next;
}
}
void CreateList_H(LinkList& L, int n)
{
L = (LinkList)malloc(sizeof(Lnode));
L->next = NULL;
for (int i = 0; i < n; i++)
{
Lnode* p = (Lnode*)malloc(sizeof(Lnode));
ElemType e;
scanf("%lf%d", &e.a, &e.b);
p->elem.a = e.a;
p->elem.b = e.b;
p->next = L->next;
L->next = p;
}
}
void CreateList_R(LinkList& L, int n)
{
L = (LinkList)malloc(sizeof(Lnode));
L->next = NULL;
Lnode* q = L;
for (int i = 0; i < n; i++)
{
Lnode* p = (Lnode*)malloc(sizeof(Lnode));
ElemType e;
scanf("%lf%d", &e.a, &e.b);
p->elem.a = e.a;
p->elem.b = e.b;
p->next = NULL;
q->next = p;
q = p;
}
}
void Union(LinkList& La, LinkList Lb)
{
int La_len = ListLength(La);
int Lb_len = ListLength(Lb);
for (int i = 1; i <= Lb_len; i++)
{
ElemType elem;
GetElem(Lb, i, elem);
if (!LocateElem(La, elem))
{
ListInsert(La, ++La_len, elem);
}
}
}
int compare_size(ElemType a, ElemType b)
{
if (a.b > b.b)
return 1;
else if (a.b < b.b)
return -1;
else
{
if (a.a > b.a)
return 1;
else if (a.a < b.a)
return -1;
else
return 0;
}
}
void MergeList(LinkList La, LinkList Lb, LinkList& Lc)
{
Lnode* pa = La->next;
Lnode* pb = Lb->next;
Lnode* pc = Lc = La;
while (pa && pb)
{
if (compare_size(pa->elem, pb->elem) == 1)
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
else
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
}
pc->next = pa ? pa : pb;
}
04-10
2827
04-09
3098
09-05
2600
07-18
1363
10-22
3556