1、队列的常见操作
#define MATRIX_INIT_SIZE 100
#define MATRIXINCREMENT 10
#define OK true
#define ERROR false
#include <stdio.h>
#include <stdlib.h>
typedef int Status;
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct LinkQueue
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)
exit(0);
Q.front->next=NULL;
return OK;
}
Status DestroyQueue(LinkQueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
Status ClearQueue(LinkQueue &Q)
{
QueuePtr p;
p=Q.front->next;
while(Q.front!=Q.rear)
{
Q.front->next=p->next;
free(p);
p=Q.front->next;
}
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
return OK;
else
return ERROR;
}
int QueueLength(LinkQueue Q)
{
int i=0;
QueuePtr p;
p=Q.front;
while(p!=Q.rear)
{
i++;
p=p->next;
}
return OK;
}
Status GetHead(LinkQueue Q,int &e)
{
e=Q.front->next->data;
return OK;
}
Status EnQueue(LinkQueue &Q,int e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,int &e)
{
QueuePtr p;
p=Q.front->next;
if(Q.front==Q.rear)
return ERROR;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return OK;
}
2、堆栈的常见操作
#include <stdlib.h>
#include <stdio.h>
#define STACK_INIT_SIZE 100000
#define STACKINCREMENT 10
#define OK true
#define ERROR false
typedef int Status;
typedef struct Stack
{
int *base;
int *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Destroystack(SqStack &S)
{
free(S.base);
free(S.top);
S.stacksize=0;
return OK;
}
Status ClearStack(SqStack &S)
{
S.top=S.base;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)
return OK;
else
return ERROR;
}
int StackLength(SqStack S)
{
int n;
n=S.top-S.base;
return n;
}
Status GetTop(SqStack S,int &e)
{
if(S.base==S.top)
return ERROR;
else
{
e=*(S.top-1);
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));
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*(S.top)=e;
S.top++;
return OK;
}
Status Pop(SqStack &S)
{
if(S.top==S.base)
return ERROR;
S.top--;
//e=*(S.top);
return OK;
}
3、邻接表的存储
//邻接表
#define MAX_VERTEX_NUM 16
typedef char VertexType[5];
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
int info;
}ArcNode; //表结点类型
typedef struct VNode{
int count;
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM]; //头结点
typedef struct{
AdjList vertices; //邻接表
int vexnum,arcnum;
}ALGraph;
//邻接矩阵
4、图的操作
#define INF 65534
typedef int Graph[20][20];
int LocateVex(ALGraph G,char u[5])
{
int i;
for (i=0;i<G.vexnum;i++)
{ if(strcmp(u,G.vertices[i].data)==0) return i; }
if (i==G.vexnum) {printf("Error u!\n");exit(1);}
return 0;
}
int inputM(int cost[][MAXV])
{
int n,e,w,h,z;
printf("Please input 顶点数和边数\n");
scanf("%d%d",&n,&e);
n=3;
e=5;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(i==j) cost[i][j]=0;
else cost[i][j]=INF;
}
printf("please int the 邻接矩阵的值(起点(数字) 终点(数字) 权值(数字)):\n");
for(int i=0;i<e;i++)
{
scanf("%d%d%d",&h,&z,&w);
cost[h][z]=w;
}//for
return n;
}