Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the first upload #21

Merged
merged 8 commits into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 2017-1/lyx/ex01/up1998.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<stdio.h>
#include"up1998.h"
int main(void)
{
LinkList La, Lb, Lc;
LinkList test1��test2;
int LENTH=5;
La = (LinkList)malloc(sizeof(LNode)); //malloc() ���ص�������void * ��, ����ͬ�����Ͳ��ܽ��и�ֵ����, ����Ҫ����ǿ������ת��
CreateList_L(La, LENTH+rand()%5, test1);//����list
printf("La:");//���la
TraverseList_L(La, print); //����La
printf("\n");

Lb = (LinkList)malloc(sizeof(LNode)); //malloc() ���ص�������void * ��, ����ͬ�����Ͳ��ܽ��и�ֵ����, ����Ҫ����ǿ������ת��
CreateList_L(Lb, LENTH+rand()%5, test2); //����list
printf("Lb:"); //���lb
TraverseList_L(Lb, print);//����lb
printf("\n");

MergeList_L(La, Lb, &Lc); //�ϲ�һ��lc &Lc������
printf("Lc:"); //���lc
TraverseList_L(Lc, print);//����lc
printf("\n");
return 0;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请在代码运行通过后再次提交:

105 changes: 105 additions & 0 deletions 2017-1/lyx/ex01/up1998.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include<stdio.h> //ͷ�ļ�
#include<stdlib.h> //ͷ�ļ�

typedef int ElemType; //�Զ���һ��������ElemType

typedef struct LNode{
ElemType data; //int ������data
struct LNode* next;
}LNode,*LinkList; //�Զ���һ���������ṹ��
//struct Lnode��һ�����壬����Lnode��һ���ṹ�����ͣ���struct Lnode�ض���ΪLnode���Ժ�Ϳ��Բ���дstruct�ˣ�ֱ��ʹ��Lnode����ṹ�������ָ��ȵȡ�

void scan(ElemType *p)//�Զ��庯�� ����
{
scanf("%d",p);//����list
}
void print(const ElemType n)//�Զ��庯�� ���
{
printf("%2d ",n); //�����ֽڴ�С ���list
}

void CreateList_L(LinkList , int , void(*)(ElemType*));
//�㷨2.11 �½�������

void TraverseList_L(const LinkList,void(*)(ElemType));
//���ζ�Linklist��ÿ������Ԫ�ص��ú���void(*)(ElemType)
void MergeList_L(LinkList, LinkList, LinkList);
//�㷨2.12 �鲢����������

int test1_iter(int k)//����test1
{
static int b = 20;
return b -= k;
}
void test1(ElemType *p)//��ʼ��test1���������
{
*p = test1_iter(rand() % 5+1);
}

int test2_iter(int k)//����test2
{
static int b = 20;
return b -= k;
}
void test2(ElemType *p)//��ʼ��test2���������
{
*p = test2_iter(rand() % 5+1);
}
//����4���������ڲ��������������



void CreateList_L(LinkList L, int n, void(*func)(ElemType*))//�½�����
{
LNode* p; //ָ��
L->next = NULL; //�����е�ָ����һ���ڵ㡣->�DZ�ʾָ��ṹ���еij�Ա
for (int i = 0; i < n; ++i)
{
p = (LinkList)malloc(sizeof(LNode));//malloc() ���ص�������void * ��, ����ͬ�����Ͳ��ܽ��и�ֵ����, ����Ҫ����ǿ������ת��
func(&p->data);
p->next = L->next;
L->next = p; //����һ���µĽڵ㣬��������뵽��������
}
};//CraeteList_L
//�½�����Ϊn�ĵ�����L��������ÿ������ʹ��func����������


void TraverseList_L(const LinkList L,void(*func)(ElemType))//���ζ�Linklist��ÿ������Ԫ�ص��ú���void(*)(ElemType)
{
LNode *p = L->next;
while (p)//�ɹ�ʱ
{
func(p->data); //ִ��func����
p = p->next; //ָ��ָ����һ������Ԫ��
}
}
//����������L��������ÿ������ִ��func����


void MergerList_L(LinkList &La,LinkList &Lb,LinkList &Lc)
{ //��֪�������Ա�La��Lb��Ԫ�ذ�ֵ�ǵݼ�����
//�鲢La��Lb�õ��µĵ������Ա�Lc��Lc��Ԫ��Ҳ����ֵ�ǵݼ�����
/* pa = La->next;
pb = Lb->next;
Lc = pc = La;*/
LNode *pa = La->next, *pb = Lb->next, *pc; //paָ��La���е�ǰ�Ƚϲ���Ľ�� pbָ��Lb���е�ǰ�Ƚϲ���Ľ��
(*Lc) = pc = La;// ��La��ͷ�����ΪLc��ͷ��㣬pcʼ��ָ��Lc��ǰ���һ�����
while(pa && pb)//LaLbΪ�ǿձ�
{ if(pa->data <= pb->data)//paָ��Ԫ��С��pbָ��Ԫ��
{pc->next = pa;//pc���pa��ָ��Ԫ�ص���һ��
pc = pa;//pc���pa��ָ��Ԫ��
// �ȼ��� pc = pc->next
pa = pa->next;//pa���pa��ָ��Ԫ�ص���һ��
}
else{ pc->next = pb;//paָ��Ԫ�ش���pbָ��Ԫ��
pc = pb; //pc���pb��ָ��Ԫ��
// �ȼ��� pc = pc->next
pb = pb->next; //pb���pb��ָ��Ԫ�ص���һ��
}
}
pc->next = pa?pa:pb;
// ����ʣ��Σ�?������Ŀ�����
free(Lb); // �ͷ�Lb��ͷ���
}// MergeList L