C语言实现图的十字链表(完整代码)

今天我们用c语言将下面的图用代码表示出来。

像下图那样先定义一些结构体
在这里插入图片描述

typedef struct  Bow			//定义弧
{
	char head,tail;

	struct Bow *hlink,*tlink;
}Bow;

typedef struct FirstNode		//定义头结点
{
	char data;

	Bow *firIn;
	Bow *firOut;
}FirstNode;

typedef struct			//记录顶点的数量和弧的数量,先定义100顶点
{
	FirstNode list[100];

	int peak,edge;
}total;

完整代码如下:

#include<stdio.h>
#include<stdlib.h>

#define ERROR 0

typedef struct  Bow
{
	char head,tail;

	struct Bow *hlink,*tlink;
}Bow;

typedef struct FirstNode
{
	char data;

	Bow *firIn;
	Bow *firOut;
}FirstNode;

typedef struct
{
	FirstNode list[100];

	int peak,edge;
}total;

int LocalBow(char data,total *G)				//查询顶点的位置
{
	int i;
	for(i=0;i<G->peak;i++)
	{
		if(G->list[i].data==data)
		{
			return i;
		}
	}
	return ERROR;
}

void create(total *G)							//创建十字链表
{
	char x,y;int k,j,i;
	printf("请输入图的顶点和弧的数量:");
	scanf("%d %d",&G->peak,&G->edge);
	for(i=0;i<G->peak;i++)
	{
		fflush(stdin);
		printf("请输入第%d个顶点的值:",i+1);
		scanf("%c",&G->list[i].data);
		G->list[i].firIn=G->list[i].firOut=NULL;
	}
	for(i=0;i<G->edge;i++)
	{
	    fflush(stdin);
		printf("请输入<vi,vj>,中间用英文逗号隔开:");
		scanf("%c,%c",&x,&y);

		Bow *R=(Bow *)malloc(sizeof(Bow));
		k=LocalBow(x,G);
		j=LocalBow(y,G);
		R->head=x;
		R->tail=y;
		R->hlink=G->list[j].firIn;
		R->tlink=G->list[k].firOut;
		G->list[j].firIn=R;
		G->list[k].firOut=R;
	}
}
int main()
{
	total G;
	Bow *t;
	create(&G);
	int i;
	for(i=0;i<G.peak;i++)
	{
		printf("%c顶点出度情况为:\n",G.list[i].data);
		t=G.list[i].firOut;
		if(!t)	printf("无");
		while(t)
		{
			printf("%c->%c ",t->head,t->tail);
			t=t->tlink;
		}
		printf("\n");
		t=G.list[i].firIn;
		printf("%c顶点入度情况为:\n",G.list[i].data);
		if(!t)		printf("无");
		while(t)
		{
			printf("%c->%c ",t->head,t->tail);
			t=t->hlink;
		 }
		 printf("\n");
	}
	return 0;
}

运行结果如下:
在这里插入图片描述

  • 10
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,以下是用 C 语言实现十字链表做矩阵加法的示例代码: ``` #include <stdio.h> #include <stdlib.h> struct MatrixNode { int row; int col; int value; struct MatrixNode* right; struct MatrixNode* down; }; struct CrossMatrix { int rows; int cols; struct MatrixNode** row_heads; struct MatrixNode** col_heads; struct MatrixNode* head; }; struct MatrixNode* create_node(int row, int col, int value) { struct MatrixNode* node = (struct MatrixNode*)malloc(sizeof(struct MatrixNode)); node->row = row; node->col = col; node->value = value; node->right = NULL; node->down = NULL; return node; } struct CrossMatrix* create_cross_matrix(int rows, int cols) { struct CrossMatrix* matrix = (struct CrossMatrix*)malloc(sizeof(struct CrossMatrix)); matrix->rows = rows; matrix->cols = cols; matrix->row_heads = (struct MatrixNode**)malloc(rows * sizeof(struct MatrixNode*)); matrix->col_heads = (struct MatrixNode**)malloc(cols * sizeof(struct MatrixNode*)); matrix->head = create_node(-1, -1, 0); matrix->head->right = matrix->col_heads[0] = create_node(-1, 0, 0); matrix->head->down = matrix->row_heads[0] = create_node(0, -1, 0); for (int i = 1; i < rows; i++) { matrix->row_heads[i] = create_node(i, -1, 0); matrix->row_heads[i - 1]->down = matrix->row_heads[i]; } for (int i = 1; i < cols; i++) { matrix->col_heads[i] = create_node(-1, i, 0); matrix->col_heads[i - 1]->right = matrix->col_heads[i]; } return matrix; } void insert(struct CrossMatrix* matrix, int row, int col, int value) { struct MatrixNode* node = create_node(row, col, value); struct MatrixNode* row_head = matrix->row_heads[row]; struct MatrixNode* col_head = matrix->col_heads[col]; struct MatrixNode* current_node = row_head; while (current_node->right && current_node->right->col < col) { current_node = current_node->right; } node->right = current_node->right; current_node->right = node; current_node = col_head; while (current_node->down && current_node->down->row < row) { current_node = current_node->down; } node->down = current_node->down; current_node->down = node; } struct CrossMatrix* add(struct CrossMatrix* matrix1, struct CrossMatrix* matrix2) { if (matrix1->rows != matrix2->rows || matrix1->cols != matrix2->cols) { printf("Matrices must have the same size\n"); return NULL; } struct CrossMatrix* result = create_cross_matrix(matrix1->rows, matrix1->cols); for (int i = 0; i < matrix1->rows; i++) { struct MatrixNode* current_node1 = matrix1->row_heads[i]->right; struct MatrixNode* current_node2 = matrix2->row_heads[i]->right; while (current_node1 || current_node2) { if (current_node1 && (!current_node2 || current_node1->col < current_node2->col)) { insert(result, i, current_node1->col, current_node1->value); current_node1 = current_node1->right; } else if (current_node2 && (!current_node1 || current_node2->col < current_node1->col)) { insert(result, i, current_node2->col, current_node2->value); current_node2 = current_node2->right; } else { insert(result, i, current_node1->col, current_node1->value + current_node2->value); current_node1 = current_node1->right; current_node2 = current_node2->right; } } } return result; } void print_matrix(struct CrossMatrix* matrix) { for (int i = 0; i < matrix->rows; i++) { struct MatrixNode* current_node = matrix->row_heads[i]->right; for (int j = 0; j < matrix->cols; j++) { if (current_node && current_node->col == j) { printf("%d ", current_node->value); current_node = current_node->right; } else { printf("0 "); } } printf("\n"); } } int main() { struct CrossMatrix* matrix1 = create_cross_matrix(3, 3); insert(matrix1, 0, 0, 1); insert(matrix1, 0, 1, 2); insert(matrix1, 1, 1, 3); insert(matrix1, 1, 2, 4); insert(matrix1, 2, 0, 5); insert(matrix1, 2, 2, 6); struct CrossMatrix* matrix2 = create_cross_matrix(3, 3); insert(matrix2, 0, 0, 7); insert(matrix2, 0, 1, 8); insert(matrix2, 1, 1, 9); insert(matrix2, 1, 2, 10); insert(matrix2, 2, 0, 11); insert(matrix2, 2, 2, 12); struct CrossMatrix* result = add(matrix1, matrix2); print_matrix(result); return 0; } ``` 这里使用了结构体实现节点和矩阵,定义了`create_node`和`create_cross_matrix`函数用于创建节点和十字链表形式的矩阵。还定义了`insert`和`add`函数用于插入节点和进行矩阵加法。最后,使用`print_matrix`函数打印出矩阵。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值