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

两次作业 #41

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
74 changes: 74 additions & 0 deletions 2017-1/tqh/2-1算法/Header.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include<stdio.h>
#include"Header.h"

void CreateList(LinkList *L,int n)
{
LinkList p = NULL;
int i ;
*L = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL;
for(i=0;i<n;i++){
p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = (*L)->next;
(*L)->next = p;
}
}
//createlist

void sortList(LinkList L,int n)
{
int i=0;
int j=0;
LinkList head;
LinkList p1;
LinkList p2;

for(i=0;i<n;i++)
{
head = L->next;
for( j=0;j<n-1;j++)
{
p1=head;
p2=head->next;
if( p1->data > p2->data){
Elemtype temp = p1->data;
p1->data = p2 -> data;
p2->data = temp;
}
head = head->next;
}
}
}
void Traverse(LinkList L)
{
LNode *p = L->next;
while(p){
printf("%d ",p->data);
p = p->next;
}
}

void MergeList_L(LinkList La,LinkList Lb,LinkList *Lc)
{
LinkList pa,pb,pc;
pa = La->next;
pb = Lb->next;
*Lc = pc = La;
while(pa&&pb){
if(pa->data <= pb->data){
pc->next = pa;
pc = pa;
pa = pa->next;
}
else{
pc->next = pb;
pc = pb;
pb =pb ->next;
}
}
pc->next = pa? pa:pb;
free(Lb);
}


20 changes: 20 additions & 0 deletions 2017-1/tqh/2-1算法/Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<stdio.h>
#include<stdlib.h>

#define OK 0
#define ERROR 1
#define OVERFLOW 2

typedef int Elemtype;

typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
//�����ṹ�弰�ṹ��ָ��

void CreateList(LinkList* ,int );
void sortList(LinkList,int);
void Traverse(LinkList );
void MergeList_L(LinkList ,LinkList ,LinkList *);

31 changes: 31 additions & 0 deletions 2017-1/tqh/2-1算法/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<stdio.h>
#include"Header.h"
#include<stdlib.h>

int main()
{
int i,j;
LinkList L1,L2,Lc;

L1 =(LinkList) malloc(sizeof(LNode));
L2 =(LinkList) malloc(sizeof(LNode));

printf("L1��ֵ�ø�����");
scanf("%d",&i);
CreateList(&L1,i);
sortList(L1,i);
Traverse(L1);
printf("\n");

printf("L2��ֵ�ø�����");
scanf("%d",&j);
CreateList(&L2,j);
sortList(L2,j);
Traverse(L2);
printf("\n");

MergeList_L(L1,L2,&Lc);
Traverse(Lc);
return OK;
}

105 changes: 105 additions & 0 deletions 2017-1/tqh/3-1-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <stdio.h>
#include<stdlib.h>
//#include<time.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int SElemType;
typedef struct _SqStack {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
typedef enum {
false ,
true
}bool;

typedef enum {
OK,
ERROR,
OVERFLOW
} Status;

Status InitStack (SqStack *S);
Status conversion (SqStack *, int , int);
Status Push(SqStack *S, SElemType e);
Status Pop (SqStack *S,SElemType *e);
bool IsStackEmpty(SqStack *S);

Status InitStack (SqStack *S)
{
S->base = (SElemType *)malloc(STACK_INIT_SIZE *
sizeof(SElemType));
if(!S->base) {
return OVERFLOW;
}

S->top = S->base;
S->stacksize = STACK_INIT_SIZE;

return OK;
}

Status Push(SqStack *S,SElemType e) {
if((S->top-S->base)>=S->stacksize) {
return ERROR;
}
*S->top++ = e;
return OK;
}//��ջ

Status Pop (SqStack *S,SElemType *e)
{
if(IsStackEmpty(S))
{
return ERROR;
}

*e = *--S->top;
return OK;
}

bool IsStackEmpty(SqStack *S)
{
if(S){
return S->base ==S->top;
}
return true;
}
Status conversion (SqStack *S, int input, int d)
{
SElemType e;
if(d > 10) {
return ERROR;
}

while (input)
{
Push(S,input % d);
input = input / d;
}
while (!IsStackEmpty(S))
{
Pop(S,&e);
printf(" %d", e);
}//conversion
printf("\n");
return OK;
}
int main(int argc,char* argv[])
{
SqStack S;
int input=1348;
int d = 8;

/*srand(time(NULL));
input = rand()%1024;*/

InitStack(&S);//�����ջ
conversion (&S, input, d);

return 0;
}

118 changes: 118 additions & 0 deletions 2017-1/tqh/3-1-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <stdio.h>

#define M 1000

typedef int Elemtype;
typedef enum{
ERROR,
OK
}Status;

typedef struct Stack {
char string[M];
Elemtype top;
}Stack;
//����stack�ṹ������
void InitStack(Stack *s)
{
s->top = -1;
}
//�ṹ���ʼ��
Status PushStack(Stack *s, char ch)
{
if(s->top == M-1) {
return ERROR;
}
s->top++;
s->string[s->top] = ch;
return OK;
}
//��ջ
Status PopStack(Stack *s, char *ch)
{
if(s->top == -1) {
return ERROR;
}
*ch = s->string[s->top];
s->top--;
return OK;
}
//��ջ

Status GetTop(Stack *s, char *ch)
{
if(s->top == -1) {
return ERROR;
}
*ch = s->string[s->top];
return OK;
}
//�õ�ָ��Ԫ�ص�ֵ
Status IsEmpty(Stack *s)
{
if(s->top == -1)
{
return OK;
}
else {
return ERROR;
}
}
//�ж��Ƿ�ջΪ��
Elemtype MatchBracket(char ch1, char ch2)
{
if(ch1 == '(' && ch2 == ')') {
return 1;
}
if(ch1 == '[' && ch2 == ']') {
return 1;
}
if(ch1 == '{' && ch2 == '}') {
return 1;
}
return 0;
}
//�ж������Ƿ�ƥ��
int main()
{
Stack s;
char str[M] = {0}, ch;
int i;

InitStack(&s);
fprintf(stdout, "Input brackets:");
fscanf(stdin, "%s", str);
for(i = 0; str[i] != 0; i++) {
switch(str[i]) {
case '(':
case '[':
case '{':
PushStack(&s, str[i]);
break;
case ')':
case ']':
case '}':
if(IsEmpty(&s)) {
printf("right bracket spilth.\n");
return 0;
} else {
GetTop(&s, &ch);
if(MatchBracket(ch , str[i])) {
PopStack(&s, &ch);
} else{
printf("left and right brackets are different.\n");
return 0;
}
}
break;
default:
printf("Input error\n");
}
}
if(IsEmpty(&s)) {
printf("Left and right brackets are matched.\n");
} else {
printf("Left bracket spilth..\n");
}
return 0;
}
Loading