-
Notifications
You must be signed in to change notification settings - Fork 46
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b73ff5
first homework upload
Jasmine2020 717df89
second homework upload
Jasmine2020 f002c75
first homework modified
Jasmine2020 a3211ec
一二次作业修改提交
Jasmine2020 6aacc50
third homework upload
Jasmine2020 8a480e5
third homework upload
Jasmine2020 a30ed3d
third homework modified
Jasmine2020 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) | ||
{ | ||
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
homework2.cpp为修改后结果,使用随机函数来生成测试数据,但是这样的话我不会在创建链表的同时保证它有序,因此创建的两条链表是无序的。因此归并的链表并非从小到大,而是简单地一一对比归并 | ||
homework.c是限制了测试数据,即已知两条有序链表的归并。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
仅接受C语言版的代码实现,不接受C++代码
建议使用后缀名
.c
去调试无误后再行提交