数据结构与算法

数组归并

#include <stdio.h>
//a:目标数组
//lena: a数组有效元素个数
//b:操作数组
//lenb: b数组有效元素个数
//注意:a数组空间应>= a+b的长度
int ArrAddArr(int *a, int lena, int *b, int lenb)
{
	int i,j;
	for(i=0; i < lenb; i++)
	{	
		// 将大数向后移动
//		for(j=lena+i-1 ; j >= 0 ; j-- )
//		{
//			if(a[j] > b[i]) a[j+1] = a[j];
//			else break;
//		}
//		a[j+1] = b[i];
//		方式2
//		for(j=lena+i;  j-1 >= 0; j--)
//		{
//			if(a[j-1] > b[i] ) a[j] = a[j-1];
//			else break;
//		}
//		a[j] = b[i];
//	    方式3
		for(j=lena+i; j-1>=0 && a[j-1] > b[i]; a[j]=a[j-1],j--);
		a[j] = b[i];
	}
	return 0;
}

void show(int *a, int len)
{
	while(len --) printf("%d ",*a++);
}


int main()
{
	int a[20] = {1,3,5,7,8};
	int b[5]  = {2,3,5,8,9};
	ArrAddArr(a,5,b,5);
	show(a,10);
	return 0;
}




顺序表

#include <stdio.h>
#include <stdlib.h>
//顺序表
//#define  MAXLEN  20
//1 定义节点类型  假设为int 
typedef int  Data_t;

//2 定义顺序表
struct list_t
{
	int max_len; //存储顺序表的最大长度
	int cnt;     //存储当前表中节点个数
	Data_t *data;//存储节点的数组动态分配长度
	//	data_t data[MAXLEN]; //静态分配数组长度
};

//定义对 顺序表的操作
//1 创建表
int create_list(struct list_t *head, int max_len)
{
	//入参检查
	if(head == NULL || max_len <= 0)
	  return -1; // 返回错误码
	//填充 参数
	head->max_len = max_len;
	head->cnt = 0;
	//申请空间
	head->data = (Data_t *)malloc(max_len * sizeof(Data_t));
	if(head->data == NULL) 
	{
		printf("malloc err!\n");
		return -2;
	}
	return 0;
}

//2.删除表
int del_list(struct list_t *head)
{
	if(head == NULL) return -1;
	if(head->data)//释放堆区内存
	  free(head->data);
	head->data = NULL;
	head->cnt = 0;
	head->max_len = 0;
	return 0;
}


//插入数据
int insert_list(struct list_t *head, int index, Data_t data)
{
	if(head == NULL || head->data == NULL) return -1;
	//判断位置能否插入
	if( index > head->cnt ) 
	{
		printf("插入位置%d,不存在!\n", index);
		return -2;
	}
	//判断满否
	if(head->cnt == head->max_len) 
	{
		printf("顺序表已满!!\n");
		return -3;
	}
	//移开位置
	//找到最后一个元素下标
	//int i = head->cnt - 1;
	for( int i  = head->cnt -1  ; i >= index ;i--)
	{
		head->data[i+1] = head->data[i];
	}
	//写入数据
	head->data[index] = data;
	head->cnt ++;//更新元素个数
	return 0;
}

int show_list(struct list_t *head)
{
	if(head ==NULL || head->data == NULL) return -1;
	for(int i=0; i< head->cnt; i++) printf("%d ",head->data[i]);
	printf("\n");
	return 0;
}

//获取某个下标的元素值
int get_list_data(struct list_t *head, int index, Data_t *data)
{
	if(head == NULL || head->data == NULL) return -1;
	//下标不存在
	if(index >= head->cnt) 
	{
		printf("%d号元素不存在!\n",index);
		return -2;
	}
	*data = head->data[index];
	return 0;
}

//根据元素的值 得到下标  找第一个值
int get_list_index(struct list_t *head, int *index, Data_t data)
{
	if(head == NULL || head->data == NULL) return -1;
	for(int i=0; i<head->cnt; i++)
	{//找到值相同的下标 
		if(head->data[i] == data) 
		{
			*index = i;
			return 0;
		}
	}
}

//添加删除节点的函数del_node实现

int del_node(struct list_t *head, int index)
{
	if (head == NULL || head->data == NULL) return -1;
	if (index < 0 || index >= head->cnt) 
	{
		printf("删除位置%d不存在!\n", index);
		return -2;
	}
	for (int i = index; i < head->cnt-1; i++)
	{
		head->data[i] = head->data[i+1];
	}
	head->cnt--;
	return 0;
}
//添加判空判满函数is_empty和is_full
int is_empty(struct list_t *head)
{
	if (head == NULL) return -1;
	return head->cnt == 0;
}

int is_full(struct list_t *head)
{
	if (head == NULL) return -1;
	return head->cnt == head->max_len;
}
//添加获取表长度函数get_length
int get_length(struct list_t *head)
{
	if (head == NULL) return -1;
	return head->cnt;
}
//添加拼接两个表的函数concat_list
struct list_t *concat_list(struct list_t *list1, struct list_t *list2)
{
	if (list1 == NULL || list2 == NULL) return NULL;

	int total_len = list1->cnt + list2->cnt;
	struct list_t *new_list = (struct list_t *)malloc(sizeof(struct list_t));

	if (new_list == NULL)
	{
		printf("malloc err!\n");
		return NULL;
	}

	new_list->max_len = total_len;
	new_list->cnt = total_len;
	new_list->data = (Data_t *)malloc(total_len * sizeof(Data_t));

	if (new_list->data == NULL)
	{
		printf("malloc err!\n");
		free(new_list);
		return NULL;
	}

	int i, j;
	for (i = 0; i < list1->cnt; i++)
	{
		new_list->data[i] = list1->data[i];
	}
	for (j = 0; j < list2->cnt; j++)
	{
		new_list->data[i+j] = list2->data[j];
	}

	return new_list;
}

int main()
{
	struct list_t L;
	int ret = create_list(&L, 25);
	if (ret == 0)
	{
		printf("表L创建成功!!\n");
		printf("data=%p\n", L.data);
	}

	insert_list(&L, 0, 10);
	insert_list(&L, 0, 20);
	insert_list(&L, 0, 30);
	insert_list(&L, 0, 40);

	show_list(&L);

	del_node(&L, 2); // 删除位置为2的节点
	show_list(&L);

	printf("表L是否为空:%d\n", is_empty(&L));
	printf("表L是否已满:%d\n", is_full(&L));

	Data_t dt;
	if (get_list_data(&L, 3, &dt) == 0)
	{
		printf("3号元素的值为%d\n", dt);
	}

	int index;
	if (get_list_index(&L, &index, 20) == 0)
	{
		printf("元素值为20的下标是%d\n", index);
	}

	struct list_t L2;
	ret = create_list(&L2, 10);
	if (ret == 0)
	{
		printf("表L2创建成功!!\n");
		printf("data=%p\n", L2.data);
	}

	insert_list(&L2, 0, 50);
	insert_list(&L2, 1, 60);

	struct list_t *new_list = concat_list(&L, &L2);
	if (new_list != NULL)
	{
		printf("拼接后的顺序表:");
		show_list(new_list);
	}

	del_list(&L);
	del_list(&L2);
	if (new_list != NULL)
	{
		free(new_list->data);
		free(new_list);
	}

	return 0;
}

循环队列

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

typedef struct Node {
	int data; // 存储元素的数据
	struct Node* next; // 指向下一个节点的指针
} Node;

typedef struct {
	Node* front; // 队首指针
	Node* rear; // 队尾指针
} CircularQueue;

// 1. 创建空队列
CircularQueue* createQueue() {
	CircularQueue* queue = (CircularQueue*)malloc(sizeof(CircularQueue));
	if (queue == NULL) {
		printf("队列创建失败!\n");
		return NULL;
	}
	queue->front = NULL;
	queue->rear = NULL;
	return queue;
}

// 2. 销毁队列
void destroyQueue(CircularQueue* queue) {
	if (queue == NULL) return;

	Node* p = queue->front;
	while (p != NULL) {
		Node* temp = p;
		p = p->next;
		free(temp);
	}

	free(queue);
}

// 3. 入队
int enqueue(CircularQueue* queue, int value) {
	if (queue == NULL) {
		printf("队列为空!\n");
		return -1;
	}

	Node* newNode = (Node*)malloc(sizeof(Node));
	if (newNode == NULL) {
		printf("内存分配失败!\n");
		return -1;
	}
	newNode->data = value;
	newNode->next = NULL;

	if (queue->front == NULL) {
		queue->front = newNode;
	} else {
		queue->rear->next = newNode;
	}

	queue->rear = newNode;
	queue->rear->next = queue->front; // 将队尾指针指向队首,形成循环

	return 0;
}

// 4. 出队
int dequeue(CircularQueue* queue, int* value) {
	if (queue == NULL || value == NULL) {
		printf("队列为空!\n");
		return -1;
	}

	if (queue->front == NULL) {
		printf("队列为空!\n");
		return -1;
	}

	*value = queue->front->data;

	Node* frontNode = queue->front;
	if (queue->front == queue->rear) { // 队列只有一个节点的情况
		queue->front = NULL;
		queue->rear = NULL;
	} else {
		queue->front = queue->front->next;
		queue->rear->next = queue->front; // 将队尾指针指向队首,形成循环
	}

	free(frontNode);
	return 0;
}

// 5. 置空队列
void clearQueue(CircularQueue* queue) {
	if (queue == NULL) return;

	Node* p = queue->front;
	while (p != NULL) {
		Node* temp = p;
		p = p->next;
		free(temp);
	}

	queue->front = NULL;
	queue->rear = NULL;
}

// 6. 判空判满
int isEmpty(CircularQueue* queue) {
	if (queue == NULL) {
		printf("队列为空!\n");
		return -1;
	}
	return queue->front == NULL ? 1 : 0;
}

int isFull(CircularQueue* queue) {
	if (queue == NULL) {
		printf("队列为空!\n");
		return -1;
	}
	return queue->front == queue->rear ? 1 : 0;
}

// 7. 获得队列长度
int getQueueLength(CircularQueue* queue) {
	if (queue == NULL) {
		printf("队列为空!\n");
		return -1;
	}

	int length = 0;
	Node* p = queue->front;
	while (p != NULL) {
		length++;
		if (p == queue->rear) break; // 在循环队列中,遍历到队尾即可停止
		p = p->next;
	}
	return length;
}

int main() {
	CircularQueue* queue = createQueue();
	if (queue == NULL) return 0;

	enqueue(queue, 1);
	enqueue(queue, 2);
	enqueue(queue, 3);

	printf("队列长度:%d\n", getQueueLength(queue));

	int value;
	dequeue(queue, &value);
	printf("出队元素:%d\n", value);

	clearQueue(queue);
	printf("队列长度:%d\n", getQueueLength(queue));

	destroyQueue(queue);
	return 0;
}
链表
#include <stdio.h>
#include <stdlib.h>
//链表
//1. 定义数据类型
typedef int Data_t;

//2. 定义节点类型
typedef struct node_t 
{
	Data_t data;	    //数据域:存储数据本身的
	struct node_t *next;//指针域:存储逻辑关系
}node_t;

//创建空链表
//返回值 失败返回 NULL 
//成功 返回 链表头节点地址
node_t * create_link_list(void)
{
	node_t *p = (node_t *)malloc(sizeof(node_t));
	if(p) p->next = NULL; //空链表 头节点next=NULL
	return p;
}

//链表插入节点
int insert_data(node_t *head, int index, Data_t data)
{
	if(head == NULL) return -1;
	//1. 创建新节点空间
	node_t *q = (node_t *)malloc(sizeof(node_t));
	if(q == NULL) return -2;
	q->data = data;//存储data到q节点中
	//2. p指针 找到index的前一个节点地址	
	node_t *p = head;
	//移动p 指向index前一个节点位置  p 到最末尾仍然没有到index前一个位置
	for(int i=0;i<index && p != NULL ; i++) p = p->next;
	if( p == NULL  ) 
	{//p 到最末尾仍然没有到index前一个位置
		printf("%d 位置 没有在链表上!\n",index);
		return -3;
	}
	else 
	{//p找到index位置的前一个节点的地址 
		//3.将新节点插入到链表中
		q->next = p->next;
		p->next = q;
	}
	return 0;
}

//遍历链表
int show_link_list(node_t *head)
{
	if(head == NULL ) return -1;
	if(head->next == NULL) 
	{
		printf("\n");
		return 0;
	}
	node_t *p = head->next;
	for(  ; p != NULL  ; p=p->next )
	  printf("%d ", p->data);
	printf("\n");
}

//给定下标index删除该节点

//拼接两个链表
int connect_link_list(node_t *head,node_t *head2)
{
	if(head == NULL || head2 == NULL) return 0;
	if(head2->next == NULL) return 0;
	node_t *p = head;
	//找最后节点p
	while(p->next != NULL) p = p->next;
	//拼接2两个节点
	p->next = head2->next;
	//head2 设置为空表 
	head2->next = NULL;
	return 0;
}

//链表翻转
int link_list_Reverse(node_t* head)
{
	if (head == NULL || head->next == NULL) return -1;

	node_t* curr = head->next;
	node_t* prev = NULL;

	while (curr != NULL)
	{
		node_t* next_temp = curr->next;
		curr->next = prev;
		prev = curr;
		curr = next_temp;
	}

	head->next = prev;

	return 0;
}





int main()
{
	//创建一个链表
	node_t *head = create_link_list();
	if(head == NULL) 
	{
		printf("创建失败!!\n");
		return -1;
	}
	else 
	  printf("创建成功 head=%p\n",head);

	node_t *head2 = create_link_list();
	if(head2 == NULL) 
	{
		printf("创建失败!!\n");
		return -1;
	}
	else 
	  printf("创建成功 head2=%p\n",head2);

	//插入数据到链表
	insert_data(head, 0, 10);
	insert_data(head, 1, 20);
	insert_data(head, 1, 30);
	insert_data(head, 1, 40);
	show_link_list(head);

	for(int i=1;i<10;i++) insert_data(head2,0,i);

	show_link_list(head2);
	connect_link_list(head,head2);
	return 0;
}

图的访问
#include <stdio.h>

typedef char map_data_t;

// 存储顶点
map_data_t top[] = "ABCDE";

// 临界矩阵存储边
signed char side[5][5] = {
	0, 1, 1, -1, -1,
	-1, 0, -1, -1, 1,
	-1, 1, 0, 1, -1,
	1, -1, -1, 0, -1,
	-1, -1, -1, 1, 0
};

// 深度遍历
// in:开始节点下标
// top: 节点数组
// side: 邻接矩阵
// flag: 访问标志数组
int show(int in, map_data_t* top, signed char (*side)[5], int* flag) {
	if (flag[in]) return 0; // 该节点被访问过了
	// 访问in节点
	printf("%c ", top[in]);
	flag[in] = 1; // 标记该节点被访问

	// 访问下一个节点
	for (int i = 0; i < 5; i++) {
		// 递归访问相邻节点
		if (side[in][i] == 1) show(i, top, side, flag);
	}
	return 0;
}

int main() {
	int flag[5] = {0};
	show(0, top, side, flag);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值