代码实现:用C语言实现Stack,Queue,Graph的邻接矩阵,BFS和DFS还有拓扑排序

前言

这个代码其实是我一年半前学习数据结构时敲下的,我检查了几遍应该是没有问题的,如果有更好的方法或哪里有bug,大家可以在评论区留言哦!

结构

我实现了这些功能,大家可以根据需要自行ctrl+f搜索hhhhhhhhh

Stack* initStack();
SNode* createSNode(VexType x);
void push(Stack* s, VexType x);
VexType pop(Stack* s);
int isStackEmpty(Stack* s);

Queue* initQueue();
QNode* createQNode(VexType x);
void enqueue(Queue* q, VexType x);
VexType dequeue(Queue* q);
int isQueueEmpty(Queue* q);

Graph* initGraph();
void fillUndirectedGraph(Graph* G);
void fillDirectedGraph(Graph* G);
int locateVex(Graph* G, VexType x);
void showGraph(Graph* G);
void DFS(Graph* G); //the beginning point is G->Vex[0]
void BFS(Graph* G); //the beginning point is G->Vex[0]
void TopologicalSorting(Graph* G);

代码实现

#include <stdio.h>
#include <stdlib.h>
#define MaxVnum 100

// 声明
typedef char VexType;
typedef int EdgeType;
typedef struct graph Graph;

typedef struct stack Stack;
typedef struct stackNode SNode;

typedef struct queue Queue;
typedef struct queueNode QNode;

struct graph
{
    VexType Vex[MaxVnum+1];
    EdgeType Edge[MaxVnum][MaxVnum];
    int vexnum;
    int edgenum;
};

struct stackNode
{
    VexType Sdata;
    SNode* next;
};

struct stack
{
    SNode* head;
    //SNode* tail;
};

struct queueNode
{
    VexType Qdata;
    QNode* next;
};

struct queue
{
    QNode* front;
    QNode* back;
};

// 函数声明
Stack* initStack();
SNode* createSNode(VexType x);
void push(Stack* s, VexType x);
VexType pop(Stack* s);
int isStackEmpty(Stack* s);

Queue* initQueue();
QNode* createQNode(VexType x);
void enqueue(Queue* q, VexType x);
VexType dequeue(Queue* q);
int isQueueEmpty(Queue* q);

Graph* initGraph();
void fillUndirectedGraph(Graph* G);
void fillDirectedGraph(Graph* G);
int locateVex(Graph* G, VexType x);
void showGraph(Graph* G);
void DFS(Graph* G);  //the beginning point is G->Vex[0]
void BFS(Graph* G);  //the beginning point is G->Vex[0]
void TopologicalSorting(Graph* G);

Stack* initStack()
{
    Stack* s = (Stack*)malloc(sizeof(Stack));
    s->head = NULL;
    //s->tail = NULL;
    return s;
}

SNode* createSNode(VexType x)
{
    SNode* newNode = (SNode*)malloc(sizeof(SNode));
    newNode->next = NULL;
    newNode->Sdata = x;
    return newNode;
}

void push(Stack* s, VexType x)
{
    if(!s->head)
    {
        s->head = createSNode(x);
    }
    else
    {
        SNode* newNode = createSNode(x);
        newNode->next = s->head;
        s->head = newNode;
    }
}

VexType pop(Stack* s)
{
    if(isStackEmpty(s))
    {
        return NULL;
    }
    SNode* temp;
    VexType t;
    temp = s->head;
    s->head = s->head->next;
    t = temp->Sdata;
    free(temp);
    temp = NULL;
    return t;
}

int isStackEmpty(Stack* s)
{
    if(s->head)
    {
        return 0;
    }
    return 1;
}

Queue* initQueue()
{
    Queue* q = (Queue*)malloc(sizeof(Queue));
    q->back = NULL;
    q->front = NULL;
    return q;
}

QNode* createQNode(VexType x)
{
    QNode* newNode = (QNode*)malloc(sizeof(QNode));
    newNode->next = NULL;
    newNode->Qdata = x;
    return newNode;
}

void enqueue(Queue* q, VexType x)
{
    if(!q->front)
    {
        q->front = createQNode(x);
        q->back = q->front;
    }
    else
    {
        QNode* newNode = createQNode(x);
        q->back->next = newNode;
        q->back = newNode;
    }
}

VexType dequeue(Queue* q)
{
    if(isQueueEmpty(q))
    {
        return NULL;
    }
    QNode* temp = q->front;
    VexType t = temp->Qdata;
    q->front = q->front->next;
    if(q->front == NULL)
        q->back = NULL;
    free(temp);
    temp = NULL;
    return t;
}
int isQueueEmpty(Queue* q)
{
    if(q->front)
    {
        return 0;
    }
    return 1;
}

Graph* initGraph()
{
    int i, j;
    Graph* G = (Graph*)malloc(sizeof(Graph));
    printf("Please enter the number of vertexes: ");
    scanf("%d", &G->vexnum);
    printf("Please enter the nuber of edges: ");
    scanf("%d", &G->edgenum);
    for(i = 0; i < G->edgenum; i++)
    {
        for(j = 0; j < G->edgenum; j++)
        {
            G->Edge[i][j] = 0;
        }
    }
    return G;
}

int locateVex(Graph* G, VexType x)
{
    int i;
    for(i = 0; i < G->vexnum; i++)
    {
        if(x == G->Vex[i])
            return i;
    }
    return -1;
}

void fillUndirectedGraph(Graph* G)
{
    int i, j;
    VexType u, v;
    int temp = G->edgenum;
    char x; //Gap mark
    printf("Please enter the information of vertexes: ");
    getchar();
    for(i = 0; i < G->vexnum; i++)
    {
        scanf("%c", &G->Vex[i]);
    }

//    printf("TEST:\n");
//    for(i = 0; i < G->vexnum; i++)
//    {
//        printf("%c", G->Vex[i]);
//    }
    printf("Please enter the two vertexes to form a edge: ");
    getchar();
    while(temp--)
    {
        scanf("%c%c%c", &u, &v, &x);
        //getchar();
        i = locateVex(G, u);
        j = locateVex(G, v);
        if(i != -1 && j != -1)
        {
            G->Edge[i][j] = 1;
            G->Edge[j][i] = 1;
        }
    }
}

void fillDirectedGraph(Graph* G)
{
    int i, j;
    VexType u, v;
    int temp = G->edgenum;
    char x; //Gap mark
    printf("Please enter the information of vertexes: ");
    getchar();
    for(i = 0; i < G->vexnum; i++)
    {
        scanf("%c", &G->Vex[i]);
    }
    printf("Please enter the two vertexes to form a edge: ");
    getchar();
    while(temp--)
    {
        scanf("%c%c%c", &u, &v, &x);
        //getchar();
        i = locateVex(G, u);
        j = locateVex(G, v);
        if(i != -1 && j != -1)
        {
            G->Edge[i][j] = 1;//    directed graph 
        }
    }
}

void showGraph(Graph* G)
{
    int i, j;
    printf("The graph is:\n");
    printf("  ");
    for(i = 0; i < G->vexnum; i++)
    {
        printf("%c ", G->Vex[i]);
    }
    printf("\n");
    for(i = 0; i < G->vexnum; i++)
    {
        printf("%c ", G->Vex[i]);
        for(j = 0; j < G->vexnum; j++)
        {
            printf("%d ", G->Edge[i][j]);
        }
        printf("\n");
    }
}

void DFS(Graph* G)
{
    int visited[G->vexnum];
    int i, j;
    int temp;
    Stack* s = initStack();
    for(i = 0; i < G->vexnum; i++)
    {
        visited[i] = 0;
    }
    for(i = 0; i < G->vexnum; i++)
    {
        if(!visited[i])
        {
            push(s, G->Vex[i]);
            visited[i] = 1;
            temp = i;
            j = i;
            while(j < G->vexnum)
            {
                if(!visited[j] && G->Edge[temp][j])
                {
                    push(s, G->Vex[j]);
                    temp = j;
                    visited[j] = 1;
                    continue;
                }
                j++;
            }
        }
    }
    while(!isStackEmpty(s))
    {
        printf("%c ", pop(s));
    }
    printf("\n");
}

void BFS(Graph* G)
{
    int visited[G->vexnum];
    int i, j;
    Queue* q = initQueue();
    for(i = 0; i < G->vexnum; i++)
    {
        visited[i] = 0;
    }
    for(i = 0; i < G->vexnum; i++)
    {
        if(!visited[i])
        {
            enqueue(q, G->Vex[i]);
            visited[i] = 1;
            //temp = i;
            for(j = i; j < G->vexnum; j++)
            {
                if(!visited[j] && G->Edge[i][j])
                {
                    enqueue(q, G->Vex[j]);
                    visited[j] = 1;
                }

            }
        }
    }
    while(!isQueueEmpty(q))
    {
        printf("%c ", dequeue(q));
    }
    printf("\n");
}

void TopologicalSorting(Graph* G)
{
    
}

int main()
{
    Graph* G = initGraph();
    fillUndirectedGraph(G);	// 只能输入一个字符,权重是用int
    showGraph(G);
    DFS(G);
    BFS(G);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值