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

Homework1 线性表 Homework2 作业13题链表的拆分 Homework3 链栈的三个相关应用 #23

Merged
merged 7 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
102 changes: 102 additions & 0 deletions 2017-1/Jasmine/exp01/Homework.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include<stdio.h>
#include<stdlib.h>
typedef struct list
{
int data;
struct list *next;
}List;
List *Creat(int m[],int n)//����һ����֪���ȵ���������
{
List *head, *next, *present;
int a;
head = (List*)malloc(sizeof(List));
head->next = NULL;
present = head;
next = NULL;
for (int i = 0;i < n;i++)
{
a=m[i];
next = (List*)malloc(sizeof(List));
next->data = a;
present->next = next;
present = next;
}
present->next = NULL;
return head;
}
List* MergeList(List *LA, List *LB)//����β�巨�����������鲢 �㷨 (A��BΪ����������CҪ��Ϊ������������)
{
List *pc = NULL;
List *pa = LA->next;
List *pb = LB->next;
List *LC;
LC = LA;
LC->next = NULL;
pc = LC;
free(LB);
//LB=NULL;

while (pa != NULL && pb != NULL)
{
if (pa->data <= pb->data)
{
pc->next = pa;

pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;

pc = pb;
pb = pb->next;
}
pc->next = NULL;
}

if (pa == NULL)
{
pc->next = pb;

}
else
{
pc->next = pa;
}
return LC;
}
void Print(List *head)
{
List *p;
p = head->next;
while (p != 0)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
List *head1, *head2, *head3;
int a[4] = { 3,5,8,11 };
int b[7] = { 2,6,8,9,11,15,20 };
printf("���Ա�LA��");
head1 = Creat(a,4);
Print(head1);

printf("���Ա�LB��");
head2 = Creat(b,7);
Print(head2);




head3=MergeList(head1, head2);
Print(head3);


return 0;

}
107 changes: 107 additions & 0 deletions 2017-1/Jasmine/exp01/Homework.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct list
{
int data;
struct list *next;
}List;
List *Creat(int n)//����һ����֪���ȵ���������
{
List *head, *next, *present;
int a;
head = (List*)malloc(sizeof(List));
head->next = NULL;
present = head;
next = NULL;
for (int i = 0;i < n;i++)
Copy link
Collaborator

Choose a reason for hiding this comment

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

仅接受C语言版的代码实现,不接受C++代码
建议使用后缀名.c去调试无误后再行提交

{
a = rand() % 20;
next = (List*)malloc(sizeof(List));
next->data = a;
present->next = next;
present = next;
}
present->next = NULL;
return head;
}
List* MergeList(List *LA, List *LB)//����β�巨�����������鲢 �㷨 (A��BΪ����������CҪ��Ϊ������������)
{
List *pc = NULL;
List *pa = LA->next;
List *pb = LB->next;
List *LC;
LC = LA;
LC->next = NULL;
pc = LC;
free(LB);
//LB=NULL;

while (pa != NULL && pb != NULL)
{
if (pa->data <= pb->data)
{
pc->next = pa;

pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;

pc = pb;
pb = pb->next;
}
pc->next = NULL;
}

if (pa == NULL)
{
pc->next = pb;

}
else
{
pc->next = pa;
}
return LC;
}
void Print(List *head)
{
List *p;
p = head->next;
while (p != 0)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}

int main()
{
srand((unsigned)time(NULL));
List *head1, *head2, *head3;
int len1, len2;
printf("����������Ա�LA�ij��ȣ�");
len1 = rand() % 5;
head1 = Creat(len1);

Print(head1);

printf("����������Ա�LB�ij��ȣ�");
len2 = rand() % 10;
head2 = Creat(len2);
Print(head2);




head3 = MergeList(head1, head2);
Print(head3);


return 0;

}
2 changes: 2 additions & 0 deletions 2017-1/Jasmine/exp01/作业说明.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
homework2.cpp为修改后结果,使用随机函数来生成测试数据,但是这样的话我不会在创建链表的同时保证它有序,因此创建的两条链表是无序的。因此归并的链表并非从小到大,而是简单地一一对比归并
homework.c是限制了测试数据,即已知两条有序链表的归并。
81 changes: 81 additions & 0 deletions 2017-1/Jasmine/exp02/3-2-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//������ת��

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef enum
{Error,OK}Status;
//#define Status int
//#define Error 0
//#define OK 1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
Status InitStack(SqStack *s)
{
s->base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!s->base)
return Error;
else
{
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
}
Status Push(SqStack *s,int e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(int*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(int));
if(!s->base)
return Error;
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top=e;
s->top++;
return OK;
}
int StackEmpty(SqStack *s)
{
if(s->top==s->base)
{
return 0;
}
else
return 1;
}
int Pop(SqStack *s)
{
if(s->top==s->base)
return 0;
else
{
s->top--;
return *s->top;
}
}
int main()
{
int n;
SqStack S;
InitStack(&S);
srand(time(NULL));
n = rand() % 1024;
printf("��ʮ������%dת��Ϊ�˽�����\n", n);
while(n)
{
Push(&S,n%8);
n=n/8;
}
while(StackEmpty(&S))
{
printf("%d",Pop(&S));
}
return 0;
}
Loading