Skip to content
Merged
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
213 changes: 213 additions & 0 deletions 2017-1/Jasmine/exp07/Picture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#include <stdio.h>
#include <stdlib.h>

#include"graph.h"
#include"queue.h"

typedef enum
{
OK,
ERROR,
OVERFLOW
}Status;
typedef enum
{
false,
true
}bool;

//----------------ͼ�Ļ�������-----------------------

//���㸳ֵ
Status Add(Graph *G, int x, int y)
{
if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM)
{
return ERROR;
}
G->arcs[x][y].adj = G->arcs[y][x].adj = 1; //����ͼ���ڽӾ����ǶԳƵģ�Ϊ����ͼ���㸳ֵ����ֵΪ1
return OK;
}
//����ͼ���������ʾ����
Status CreateGraph(Graph *G)
{
int i, j;
G->vexnum = 9;//������
G->arcnum = 12;//����

for (i = 0; i < G->vexnum; i++)
{
for (j = 0; j < G->arcnum; j++)
{
G->arcs[i][j].adj = INFINITY; //��ʼ���ڽӾ���
}
}
Add(G, 0, 1); Add(G, 0, 2); Add(G, 0, 3); Add(G, 0, 6); //Add����Ϊ�ظ��ģ���һ����������װ�������
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;
}
//���ص�һ���ڽӶ��㣬���򷵻�-1
int FirstAdjVex(Graph G, int i)
{
int k;
for (k = 0; k < G.vexnum; k++)
{
if (G.arcs[i][k].adj == 1) //�ڽӾ���ͬһ����Ϊ1�ĵ㶼�������ڽӵ㣬��0��ʼ��������һ��Ϊ1�ľ����ڽӵ�
{
return k;
}
}
return -1;
}
//������һ���ڽӶ��㣬���򷵻�-1
int NextAdjVex(Graph G, int i, int j)
{
int k;
for (k = j + 1; k < G.vexnum; k++)
{
if (G.arcs[i][k].adj == 1)//k��j+1��ʼ����һ��Ϊ1�ľ���������һ���ڽӵ�
{
return k;
}
}
return -1;
}
//���������·��
void ShortestPath(Graph G, int a, int b)
{
int u, v, w;
bool flag = false;
LinkQueue Q;
for (v = 0; v < G.vexnum; ++v)
{
visited[v] = false; //�ȳ�ʼ�����ʱ�־����ΪFALSE
}
InitQueue(&Q);
EnQueue(&Q, a);
visited[a] = true;//������a,������ֵΪTRUE����ʾ�Ѿ�������
while (!QueueEmpty(Q))
{
DeQueue(&Q, &u);
for (w = FirstAdjVex(G, u);w >= 0;w = NextAdjVex(G, u, w)) //wΪu���ڽӵ㣬ֱ��������bʱforѭ��ֹͣ
{
if (w == b)//�ҵ���С·�����
{
EnQueue(&Q, w);
PrintFoot(Q, a);
flag = true;
}
if (!visited[w])//��u���ڽӵ�û�б�����
{
EnQueue(&Q, w);
visited[w] = true;
}
}

if (flag)
{
break;
}
}
}

//���·��
Status PrintFoot(LinkQueue Q, int start)
{
int foot[MAX_VERTEX_NUM];
int i;
QueuePtr p;
p = Q.rear;
for (i = 0;i < MAX_VERTEX_NUM; i++)
{
foot[i] = -1;//��ʼ��foot����
}
foot[0] = p->data;
p = p->prious;
for (i = 1;p->data != start; i++)
{
foot[i] = p->data;
p = p->prious;
}
foot[i] = start;//foot[i] = p->data;
for (;i >= 0; i--)
{
if (foot[i] >= 0)
printf("%d ", foot[i] + 1);//���·��
}
}

//---------------------���������-----------------
//��ʼ������
Status InitQueue(LinkQueue *Q)
{
Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode));
if (!(Q->front))
{
return ERROR;
}
Q->front->next = Q->rear->next = NULL;
return OK;
}
//�ж��Ƿ�Ϊ�ն���
bool QueueEmpty(LinkQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
return false;
}
//����
Status EnQueue(LinkQueue *Q, int e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
{
return ERROR;
}
p->data = e;
p->next = NULL;
p->prious = Q->front;
Q->rear->next = p;
Q->rear = p;
return OK;
}
//����
Status DeQueue(LinkQueue *Q, int *e)
{
if (QueueEmpty(*Q))
{
return ERROR;
}
Q->front = Q->front->next;
*e = Q->front->data;
return OK;
}





int main()
{
int i, j;
Graph h;
CreateGraph(&h);//����һ������ͼ�������ڽӾ����ʼ��ͼ

for (i = 0;i < 9; i++)
{
for (j = 0;j < 9;j++)
{
if (i != j)
{
printf("%d -> %d :", i + 1, j + 1);
ShortestPath(h, i, j);//Ѱ�����·��
printf("\n");
}
}
}
return 0;
}
17 changes: 17 additions & 0 deletions 2017-1/Jasmine/exp07/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef H_GRAPH
#define H_GRAPH

#define INFINITY 99999
#define MAX_VERTEX_NUM 20
int visited[MAX_VERTEX_NUM];
typedef struct ArcCell //������
{
int adj;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct //ͼ�Ķ���
{
AdjMatrix arcs;
int vexnum, arcnum;
}Graph;

#endif
18 changes: 18 additions & 0 deletions 2017-1/Jasmine/exp07/queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef H_QUEUE
#define H_QUEUE

#define MAXQSIZE 100
typedef struct QNode
{
int data;
struct QNode *prious;
struct QNode *next;
}QNode, LinkList, *QueuePtr;

typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;

#endif
Binary file added 2017-1/Jasmine/exp07/截图19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions 2017-1/Jasmine/exp08/BSTOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
8 3 1 6 4 5 7 10 14 19 22 30
8 3 1 6 4 5 7 10 14 13 19 22 30
10 3 1 6 4 5 7 14 13 19 22 30
10 3 1 6 4 7 14 13 19 22 30
10 3 1 6 4 7 14 13 19 22 20 30
10 3 1 7 4 14 13 19 22 20 30
135 changes: 135 additions & 0 deletions 2017-1/Jasmine/exp08/动态查找表.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include<stdio.h>
#include<stdlib.h>
#define ElemType int

typedef struct NODE
{
ElemType elem; /*����Ԫ���ֶ�*/
struct NODE *lc, *rc; /*����ָ���ֶ�*/
}NodeType; /*�������������*/

int SearchBST(NodeType *t, NodeType **p, NodeType **q, ElemType kx)
/*�ڶ���������t �ϲ�������Ԫ�ص��� kx ��Ԫ�أ����ҵ�������1����q ָ��ý�㣬p ָ���丸��㣻*/
/*���򣬷���0����p ָ�����ʧ�ܵ����һ�����*/
{
int flag = 0;
*q = t;
while (*q!=NULL) /*�Ӹ���㿪ʼ����*/
{
if (kx>(*q)->elem) /*kx ���ڵ�ǰ���*q */
{
*p = *q;
*q = (*q)->rc;
} /*����ǰ���*q ���Һ�����Ϊ�¸�*/
else
{
if (kx<(*q)->elem) /*kx С�ڵ�ǰ���*q */
{
*p = *q;
*q = (*q)->lc;
} /*����ǰ���*q ������Ů��Ϊ�¸�*/
else
{
flag = 1;
break;
} /*���ҳɹ�������*/
}
}/*while*/
return flag;
}

int InsertBST(NodeType **t, ElemType kx)/*�ڶ���������*t �ϲ�������Ϊkx �Ľ��*/
{
NodeType *p=*t, *q, *s;
int flag = 0;
if (!SearchBST(*t, &p, &q, kx)); /*��*t Ϊ���������ϲ���*/
{
s = (NodeType*)malloc(sizeof(NodeType)); /*�����㣬����ֵ*/
s->elem = kx;
s->lc = NULL;
s->rc = NULL;
flag = 1; /*���ò���ɹ���־*/
if (!p)
*t = s; /*������в���ʱ*/
else
{
if (kx>p->elem)
p->rc = s; /*������Ϊp ������Ů*/
else
p->lc = s; /*������Ϊp ������Ů*/
}
}
return flag;
}

int DeleteBST(NodeType **t, ElemType kx)
{
NodeType *p = *t, *q, *s, **f;
int flag = 0;
if (SearchBST(*t, &p, &q, kx))
{
flag = 1; /*���ҳɹ�����ɾ���ɹ���־*/
if (p == q)
f = &(*t); /*��ɾ���Ϊ�����ʱ*/
else /*��ɾ���Ǹ����ʱ*/
{
f = &(p->lc); if (kx>p->elem) f = &(p->rc);
} /*f ָ���ɾ���ĸ�������Ӧָ����*/
if (!q->rc)
*f = q->lc; /*����ɾ����������������������滻��ɾ���*/
else
{
if (!q->lc)
*f = q->rc; /*����ɾ����������������������滻��ɾ���*/
else /*��������������������*/
{
p = q->rc;s = p;
while (p->lc)
{
s = p;
p = p->lc;
}/*����������������ɾ����ǰ��p*/
*f = p;
p->lc = q->lc; /*�滻��ɾ���q���ؽ�������*/
if (s != p)
{
s->lc = p->rc; /*��ɾ��������Ů��������ʱ����Ҫ�ؽ�������*/
p->rc = q->rc;
}
}
}
free(q);
}
return flag;
}
void PreOrder(NodeType *t)//����������
{
if (t != NULL)
{
printf("%d ", t->elem);
PreOrder(t->lc);
PreOrder(t->rc);

}
}

int main()
{
int a[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 };
int b[5] = { 13, 8, 5, 20, 6 };
NodeType *t=NULL;
int i;
for (i = 0;i < 12;i++)//��������������
{
InsertBST(&t, a[i]);
}
PreOrder(t);//�������
printf("\n");
for (i = 0;i < 5;i++)//����ɾ��
{
if (!DeleteBST(&t, b[i]))
InsertBST(&t, b[i]);
PreOrder(t);
printf("\n");
}
}
Binary file added 2017-1/Jasmine/exp08/运行结果.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.