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

hx #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

hx #195

Show file tree
Hide file tree
Changes from all 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
Binary file added 2017-1/hx/.DS_Store
Binary file not shown.
Binary file added 2017-1/hx/._.DS_Store
Binary file not shown.
Binary file added 2017-1/hx/第一次作业/._Header.h
Binary file not shown.
Binary file added 2017-1/hx/第一次作业/._test.c
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions 2017-1/hx/第一次作业/Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#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 *);
void ShowList(LinkList *,LinkList *);
13 changes: 13 additions & 0 deletions 2017-1/hx/第一次作业/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include<stdio.h>
#include"Header.h"
#include<stdlib.h>

int main()
{
int i,j;
LinkList L1,L2;
ShowList (&L1,&L2);
return OK;

}

115 changes: 115 additions & 0 deletions 2017-1/hx/第一次作业/书上算法2.12.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include<stdio.h>
#include"Header.h"
#include<time.h>
//建立线性表并将其置空
void CreateList(LinkList *L,int n)
{
LinkList p = NULL;
int i ;
*L = (LinkList)malloc(sizeof(LNode));
(*L)->next = NULL;
srand((unsigned)time( NULL ) );
for(i=0;i<n;i++){
p = (LinkList)malloc(sizeof(LNode));
//scanf("%d",&p->data);//
p->data = rand()%1024;
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);
}

void ShowList(LinkList *L1,LinkList *L2)
{
int i,j;
LinkList Lc;
*L1 =(LinkList) malloc(sizeof(LNode));
*L2 =(LinkList) malloc(sizeof(LNode));

printf("L1的长度为:");
scanf("%d",&i);

CreateList(L1,i);
printf("获取L1中的数据:\n");
Traverse(*L1);
printf("\n");

SortList(*L1,i);
printf("将L1中的数据由小到大排列:\n");
Traverse(*L1);
printf("\n");

printf("\nL2的长度为:");
scanf("%d",&j);

CreateList(L2,j);
printf("\n获取L2中的数据:");
Traverse(*L2);
printf("\n");

SortList(*L2,j);
printf("将L2中的数据由小到大排列:\n");
Traverse(*L2);
printf("\n");

printf("将所有数据由小到大排列:\n");
MergeList_L(*L1,*L2,&Lc);
Traverse(Lc);
}



Binary file added 2017-1/hx/第七次作业/._图的遍历.cpp
Binary file not shown.
225 changes: 225 additions & 0 deletions 2017-1/hx/第七次作业/图的遍历.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#include<stdio.h>
#include<stdlib.h>
#define MAX_VEX 20
#define Queue_Size 100
int visited[MAX_VEX];
typedef int ElemType;
typedef int VerType;
typedef enum{
OK,
ERROR,
OVERFLOW
}Status;

typedef struct q_node{
ElemType data;
struct q_node *previous;
struct q_node *next;
}Qnode,*Pq_node;

typedef struct link_queue{
Pq_node front;
Pq_node rear;
}LinkQueue;

typedef struct ArcCell{
ElemType eadj;
}AdjMatrix[MAX_VEX][MAX_VEX];

typedef struct graph{
AdjMatrix arcs;
int arcs_num;
int vers_num;
}Graph;


Status InitQueue(LinkQueue *q);
Status EnQueue(LinkQueue*q, ElemType e);
Status DeQueue(LinkQueue*q,ElemType *e);
bool EmptyQueue(LinkQueue q);


Status Add(Graph*g,int i,int j);
Status CreatGraph(Graph*g);
VerType ToFirstAdjVex(Graph g,int num);
VerType ToNextAdjVex(Graph g,int i,int j);
void BFSTraverse(Graph g,int a,int b);
Status PrintFoot(LinkQueue q,VerType start);



Status InitQueue(LinkQueue *q){
q->front=(Pq_node)malloc(Queue_Size*sizeof(Qnode));
q->rear=(Pq_node)malloc(Queue_Size*sizeof(Qnode));
q->front=q->rear;
if(!(q->front)){
return ERROR;
}
q->front->next=q->rear->next=NULL;
return OK;
}

Status EnQueue(LinkQueue*q, ElemType e) {
Pq_node ptr;
ptr=(Pq_node)malloc(sizeof(Qnode));
if(!ptr)
{
return ERROR;
}
ptr->data=e;
ptr->next=NULL;
ptr->previous=q->front;
q->rear->next=ptr;
q->rear=ptr;
return OK;
}

Status DeQueue(LinkQueue *q,ElemType *e){
if(EmptyQueue(*q)){
return ERROR;
}
q->front=q->front->next;
*e=q->front->data;
return OK;
}

bool EmptyQueue(LinkQueue q){
if(q.front==q.rear){
return true;
}

return false;

}
Status Add(Graph*g,int i,int j) {
if(i>=MAX_VEX||j>=MAX_VEX)
{
return ERROR;
}
g->arcs[i][j].eadj=1;
g->arcs[j][i].eadj=1;

return OK;
}
Status CreatGraph(Graph*g) {
g->vers_num=9;
g->arcs_num=12;
int i=0;
int j=0;
for(i=0;i<g->vers_num;i++)
{
for(j=0;j<g->vers_num;j++)
{
g->arcs[i][j].eadj=0;
}
}
Add(g, 0, 1); Add(g, 0, 2); Add(g, 0, 3); Add(g, 0, 6);
Add(g, 1, 2); Add(g, 3, 4); Add(g, 3, 5); Add(g, 4, 5);
Add(g, 5, 7); Add(g, 6, 7); Add(g, 6, 8); Add(g, 7, 8);
return OK;

}

VerType ToFirstAdjVex(Graph g,int num) {
int i;
for(i=0;i<g.vers_num;i++)
{
if(g.arcs[num][i].eadj==1){
return i;
}
}
return -1;
}

VerType ToNextAdjVex(Graph g,int i,int j){
int k;
for(k=j+1;k<g.vers_num;k++)
{
if(g.arcs[i][k].eadj==1){
return k;
}
}
return -1;
}

void BFSTraverse(Graph g,int a,int b) {
int i=0,j,k;
int e;
LinkQueue q;
bool flag;
for(i=0;i<g.vers_num;++i){
visited[i]=0;

}
InitQueue(&q);
EnQueue(&q,a);
visited[a]=1;
flag=false;
while(!EmptyQueue(q)){
DeQueue(&q,&e);

for(j=ToFirstAdjVex(g,e);j>=0;j=ToNextAdjVex(g,e,j))
{
if(j==b){
EnQueue(&q,j);
PrintFoot(q,a);
flag=true;
break;
}
if(!visited[j]){
EnQueue(&q,j);
visited[j]=true;
}

}
if(flag){
break;
}

}

}
Status PrintFoot(LinkQueue q,VerType start) {
int track[MAX_VEX];
Pq_node ptr;
int i=0,j;

ptr=q.rear;
for(i=0;i<MAX_VEX;i++){
track[i]=-1;
}
track[0]=ptr->data;
ptr=ptr->previous;
for(i=1;ptr->data!=start;i++){
track[i]=ptr->data;
ptr=ptr->previous;
}
track[i]=ptr->data;
for( j=i;j>=0;j--)
{if(track[j]>=0)
{
printf("%d ",track[j]+1);
}

}
return OK;
}
int main()
{
Graph graph;
CreatGraph(&graph);
int i=0,j=0;
printf("到其他结点的最短路径为:\n");
for(i=0;i<9;i++){
for(j=0;j<9;j++)
{
if(j!=i)
{
printf("%d<->%d:",i+1,j+1);
BFSTraverse(graph,i,j);
printf("\n");
}
}
}
return 0;
}
Binary file not shown.
Binary file added 2017-1/hx/第三次作业/._数制转换.c
Binary file not shown.
Binary file added 2017-1/hx/第三次作业/._行编辑.c
Binary file not shown.
Loading