我的数据结构C语言版本(给自己用的)

目录

顺序表:

链表:

栈:

队列:

堆:


我想在之后的大学数据结构课上需要自己写来做题,但每次都自己写,那太麻烦了,所以我就将这个博客来把所有的C语言的数据结构弄上去,

问我为什么不用GitHub,虽说也托管上去了,哈哈机房访问的GitHub太慢了!

顺序表:

头文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>



//  队列先进先出


typedef int QUDataType;

 typedef struct QueueNode
{
	struct QueueNode* next;
	QUDataType x;
}QueueNode;

typedef struct QUEUE
{
	QueueNode* head;
	QueueNode* tail;
}Queue;

//初始化
void QueueInit(Queue* pq);


//入队列
void QueuePush(Queue* pq,QUDataType x);

//出队列
void QueuePop(Queue* pq);

//取头数据
QUDataType QueueFront(Queue* pq);

//取尾数据
QUDataType QueueBack(Queue* pq);


//有几个数据

int QueueSize(Queue* pq);

//是否为空

bool QueueEmpty(Queue* pq);


//打印
void Print(Queue* pq);

函数文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"


void Checkcapacity(SL* pc)
{
	if (pc->size == pc->capacity)
	{
		int newcapacity = pc->capacity == 0 ? 4 : pc->capacity * 2;
		SLDataType* str = (SLDataType*)realloc(pc->a, newcapacity * sizeof(SLDataType));
		if (str == NULL)
		{
			perror("realloc");
			exit(-1);
		}
	}
}

void print(SL* pc)
{
	int i = 0;
	for (i = 0; i < pc->size; i++)
	{
		printf("%d ", pc->a[i]);
	}
}


//回收空间
void SeqlistDestory(SL* pc)
{
	free(pc->a);
	pc->a = NULL;
	pc->capacity = pc->size = 0;
}



void SeqListInit(SL* pc)
{
	pc->a = NULL;
	pc->size = 0;
	pc->capacity = 0;
}

void SeqListPushback(SL* pc, SLDataType x)
{

	//如果没有空间或根本没有就增容:
	if (pc->size == pc->capacity)
	{
		int newcapacity = pc->capacity == 0 ? 4 : pc->capacity * 2;
		SLDataType* str = (SLDataType*)realloc(pc->a,newcapacity*sizeof(SLDataType));
		if (str == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		pc->a = str;
		str = NULL;
		pc->capacity = newcapacity;
	}
	pc->a[pc->size] = x;
	pc->size++;
}



void SeqlistPopback(SL* pc)
{
	if (pc->size == 0)
	{
		printf("没有了喵~\n");
		return;
	}
	pc->a[pc->size - 1] = 0;
	pc->size--;
}

void SeqlistPushfront(SL* pc, SLDataType x)
{
	if (pc->size == pc->capacity)
	{
		int newcapacity = pc->capacity == 0 ? 4 : pc->capacity * 2;
		SLDataType* str = (SLDataType*)realloc(pc->a, newcapacity * sizeof(SLDataType));
		if (str == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		pc->a = str;
		str = NULL;
		pc->capacity = newcapacity;
	}
	pc->size += 1;
	for (int i = pc->size; i>=0; i--)
	{
		pc->a[i+1] = pc->a[i];
	}
	pc->a[0]=x;
}

void SeqlistPopfront(SL* pc)
{
	if (pc->size == 0)
	{
		printf("没u删除的元素喵~\n");
	}
	int i = 1;
	for (i = i; i <= pc->size+1; i++)
	{
		pc->a[i - 1] = pc->a[i];
	}
	pc->size -= 1;
}

//插入数字
void Seqlistinsert(SL* pc, int pos, SLDataType x)
{
	Checkcapacity(pc);
	pc->size += 1;
	int i=pos;
	for (i = pos; i < pc->size; i++)
	{
		pc->a[i] = pc->a[i - 1];
	}
	pc->a[pos - 1] = x;
}

//查找数字()
int  Seqlistfind_bydata(SL* pc, SLDataType x)
{
	int i = 0;
	for (i; i < pc->size; i++)
	{
		if (pc->a[i] == x)
		{
			printf("返回第一个下标\n");
			return  i;
		}
	}
	printf("找不到\n");
	return -1;
}


//删除指定数字 
void Seqlistdelet(SL* pc, SLDataType x)
{
	int i = 0;
	for (i = 0; i < pc->size; i++)
	{
		if (x == pc->a[i])
		{
			for (i; i < pc->size; i++)
			{
				pc->a[i] = pc->a[i + 1];
       	    }
			pc->size--;
			break;
		}
	
	}
			printf("没这个数字哦\n");
			return;
}



链表:

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

//单链表的英文  Single linked list

typedef int SLLdatatype;

typedef struct SListNode
{
	SLLdatatype  data;
	struct SListNode*next;
}SLL;


//打印
void SLLprint(SLL* phead);

//尾部存储 
void SLLpushback(SLL**phead,SLLdatatype x);

//头部存储
void SLLpushfront(SLL** phead, SLLdatatype x);

void SLLpushfront2(SLL** phead, SLLdatatype x);


//尾部删除
void SLLpopback(SLL** phead);


//头部删除
void SLLpopfront(SLL** phead);


//查找
void SLL_find_print(SLL* phead, SLLdatatype x);

//查下标
SLL* SLL_findpos(SLL* phead, SLLdatatype x);

//  指定位置插入
void SLL_inset(SLL** phead, SLL* pos, SLLdatatype x);

//指定位置删除
void SLL_pos_del(SLL** phead, SLL* pos);

//销毁链表
void SLL_destory(SLL* *phead);

函数文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"



SLL* creatnode( SLLdatatype x)
{
	SLL* newnode = (SLL*)malloc(sizeof(SLL));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}


//打印
void SLLprint(SLL* phead)
{
	/*SLL* cur = phead;*/
	while (phead != NULL)
	{
		printf("%d ", phead->data);
		phead = phead->next;
	}
	printf("->NULL\n");
}


void SLLpushback(SLL**phead,SLLdatatype x)
{
	SLL* newnode = (SLL*)malloc(sizeof(SLL));
	newnode->data = x;
	newnode->next = NULL;
	if (*phead == NULL)
	{
		*phead = newnode;
	}
	else
	{
		SLL* tail = *phead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}


void SLLpushfront(SLL** phead, SLLdatatype x)
{
	SLL* newnode = (SLL*)malloc(sizeof(SLL));
	newnode->data = x;
	newnode->next = NULL;
	if (phead == NULL)
	{
		*phead = newnode;
	}
	else
	{
		newnode->next = *phead;
		*phead = newnode;
	}

}

void SLLpushfront2(SLL** phead, SLLdatatype x)
{
	SLL* newnode = creatnode(x);
	newnode->next = *phead;
	*phead = newnode;
}



//尾部删除
void SLLpopback(SLL** phead){
	SLL*tail = *phead;
	SLL* str = NULL;
	if (*phead==NULL)
	{
		return ;
	}
	if (tail->next ==NULL)
	{
		free(tail);
		*phead = NULL;

	}
	else
	{
		while (tail->next!=NULL)
		{
			str = tail;
			tail = tail->next;
		}
		free(tail);
		tail = NULL;
		str->next = NULL;
		
	}
}


//头部删除
void SLLpopfront(SLL** phead)
{
	if (*phead == NULL)
	{
		return;
	}
	if ((*phead)->next == NULL)
	{
		*phead = NULL;
	}
	else
	{
		SLL* front = *phead;
		(*phead)=(*phead)->next;
		free(front);
		front = NULL;
	}
}

//找加打印
void SLL_find_print(SLL* phead, SLLdatatype x)
{
	if (phead == NULL)
	{
		return;
	}
	while (phead->data!=x)
	{
		if (phead->next== NULL)
		{
			break;
		}
		phead = phead->next;
	}
	if (phead->next == NULL)
	{
		printf("找不到喵~\n");
	}
	else
	{
		printf("%d\n", phead->data);
	}
}

//查下标
SLL* SLL_findpos(SLL* phead, SLLdatatype x)
{
	if (phead == NULL)
	{
		return NULL;
	}
	else
	{
		while (phead)
		{
			if (phead->data == x)
			{
				return phead;
			}
			phead = phead->next;

		}
	}
	printf("找不到\n");
	return NULL;
}

//  指定位置插入
void SLL_inset(SLL** phead, SLL* pos, SLLdatatype x)
{
	if (*phead == NULL)
	{
		return;
	}
	else
	{
		SLL* find = *phead;
		while (find)
		{
			if (find->next == pos)
			{
				SLL* newnode = creatnode(x);
				newnode->next = find->next;
				find->next = newnode;
				return;
			}
			find = find->next;
		}
	}
}

//指定位置删除
void SLL_pos_del(SLL** phead, SLL* pos)
{
	if (*phead == NULL)
	{
       		return;
	}
	else
	{
		SLL* find = *phead;
		SLL* findpos = NULL;
		while (find)
		{
			if (find->next == pos)
			{
				findpos = find->next;
				find->next = findpos->next;
				free(findpos);
				findpos = NULL;
				return;
			}
			find = find->next;
		}
	}

}

//销毁链表
void SLL_destory(SLL** phead)
{
	if (*phead == NULL)
	{
		return;
	}
	else
	{   
		while ((*phead)->next!=NULL)
		{
			SLL* tailpos = *phead;
			SLL* tail = NULL;
			while (tailpos->next != NULL)
			{
				tail = tailpos;
				tailpos = tailpos->next;
			}
			free(tailpos);
			tail->next = NULL;
			tailpos = NULL;
		}
		free(*phead);
		*phead = NULL;
	}
}



栈:

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

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

typedef int  STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;


//初始化栈
void StackIint(ST* ps);

//销毁栈
void Stackdestory(ST* pc);

//push
void StackPush(ST* ps, STDataType x);

//pop
void StackPop(ST* ps);

//取栈顶数据
STDataType Stacktop(ST* ps);

//有多少数据
int StackSize(ST* ps);

//判断是否为空
bool StackEmpty(ST* ps);
 
int minStackGetMin(ST* obj);


函数:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"


//初始化单链表
void StackIint(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

//销毁栈
void Stackdestory(ST* pc)
{
	assert(pc);
	free(pc->a);
	pc->a = NULL;
	pc->capacity = pc->top = 0;
}


//push
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		ps->capacity =ps->capacity== 0 ? 4 : 2 * ps->capacity;
		STDataType* temp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity);
		if (temp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = temp;
	}
		ps->a[ps->top] = x;
		ps->top += 1;

}

int minStackGetMin(ST* obj) {
	int i = 0;
	int min = obj->a[i];
	for (i = 1; i<obj->top; i++)
	{
		if (obj->a[i] < min)
		{
			min = obj->a[i];
		}
	}
	return min;
}

//pop
void StackPop(ST* ps)
{
	assert(ps->top > 0);
	ps->top--;
}

//取栈顶数据
STDataType Stacktop(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

//有多少数据
int StackSize(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->top;
}
//判断是否为空
bool StackEmpty(ST* ps)
{
	return ps->top == 0;
}


队列:

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>



//  队列先进先出


typedef int QUDataType;

 typedef struct QueueNode
{
	struct QueueNode* next;
	QUDataType x;
}QueueNode;

typedef struct QUEUE
{
	QueueNode* head;
	QueueNode* tail;
}Queue;

//初始化
void QueueInit(Queue* pq);


//入队列
void QueuePush(Queue* pq,QUDataType x);

//出队列
void QueuePop(Queue* pq);

//取头数据
QUDataType QueueFront(Queue* pq);

//取尾数据
QUDataType QueueBack(Queue* pq);


//有几个数据

int QueueSize(Queue* pq);

//是否为空

bool QueueEmpty(Queue* pq);


//打印
void Print(Queue* pq);

函数:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"

//初始化
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;
	while (cur!=NULL)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = NULL;
	pq->tail = NULL;

}

//入队列
void QueuePush(Queue* pq, QUDataType x)
{
	assert(pq);
	if (pq->head == NULL && pq->tail == NULL)
	{
		QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
		newnode->x = x;
		newnode->next = NULL;
		pq->head=newnode;
		pq->tail=newnode;
	}
	else
	{
		QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
		newnode->x = x;
		newnode->next = NULL;
		QueueNode* tail1 = NULL;
		tail1 = pq->tail;
		tail1->next = newnode;
		pq->tail = newnode;
	}
}

//出队列
void QueuePop(Queue* pq)
{
	assert(pq);
	if (pq->head ==NULL)
	{
		exit(-1);
	}
	QueueNode* head1 = pq->head->next;
	free(pq->head);
	pq->head = head1;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = NULL;
	}
}

//取头数据
QUDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->x;
}

//取尾数据
QUDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->tail->x;
}

//有几个数据

int QueueSize(Queue* pq)
{
	assert(pq);
	int count = 0;
	QueueNode*pos = pq->head;
	while (pos != NULL)
	{
		count += 1;
		pos = pos->next;
	}
	return count;
}

//是否为空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	if (pq->head)
	{
		return false;
	}
	return true;
}

//打印
void Print(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	QueueNode* pos = pq->head;
	while (pos != NULL)
	{
		printf("%d ", pos->x);
		pos = pos->next;
	}
 	printf("\n");
}                                        

typedef struct {
	Queue* q1;
	Queue* q2;
} MyStack;


MyStack* myStackCreate() {
	MyStack* st = (MyStack*)malloc(sizeof(MyStack));
	st->q1 = (QUEUE*)malloc(sizeof(QUEUE));
	st->q2 = (QUEUE*)malloc(sizeof(QUEUE));
	return st;
}

void myStackPush(MyStack* obj, int x) {
	if (!QueueEmpty(obj->q1))
	{
		QueuePush(obj->q1, x);
	}
	else
	{
		QueuePush(obj->q2, x);
	}
}

int myStackPop(MyStack* obj) {
	Queue* empty = obj->q1;
	Queue* noempty = obj->q2;
	if (!QueueEmpty(obj->q1))
	{
		empty = obj->q2;
		noempty = obj->q1;
	}
	int top = QueueBack(noempty);
	while (QueueSize(noempty) > 0)
	{
		QueuePush(empty, QueueFront(noempty));
		QueuePop(noempty);
	}
	return top;

}

int myStackTop(MyStack* obj) {
	if (!QueueEmpty(obj->q1))
	{
		return QueueBack(obj->q1);
	}
	return QueueBack(obj->q2);
}

bool myStackEmpty(MyStack* obj) {
	if (QueueEmpty(obj->q1) || QueueEmpty(obj->q2))
	{
		return false;
	}
	return true;

}

void myStackFree(MyStack* obj) {
	free(obj->q1);
	free(obj->q2);
	free(obj);
}


堆:

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

#define DataType int


typedef struct Heap
{
	DataType*a;
	int size;
	int capacity;	
}Heap;


//----------------------包含函数------------------------------ 

//初始化堆
void HeapInit(Heap*hp);


//打印数据
void HeapPrint(DataType*arr,int n);


//插入数据
void HeapPush(Heap*hp,DataType x); 

//向上调整
void AdjustUp(int*arr,int child);


//交换值
void swap(int*a,int *b); 


//删除数据
void HeapPop(Heap*hp);


//向下调整
void HeapAdjust(int*arr,int child,int size);


//取堆区首元素
DataType HeapTop(Heap*hp);




//------------------------实现函数------------------------------- 

//交换值 
void Swap(int*a,int *b)
{
	int temp=*a;
	*a=*b;
	*b=temp;
}


//插入数据
void HeapPush(Heap*hp,DataType x)
{
	assert(hp);
	if(hp->size==hp->capacity)
	{
		hp->capacity=hp->capacity==0?2:hp->capacity*2;
		DataType*temp=(DataType*)realloc(hp->a,sizeof(int)*hp->capacity);
		if(temp==NULL)
		{
			perror("realloc");
			return ;	
		}	
		hp->a=temp;
	}	
	hp->a[hp->size++]=x;
	AdjustUp(hp->a,hp->size-1);
} 

//向上调整
void AdjustUp(int*arr,int child)
{
	int father=(child-1)/2;
	while(child>0)
	{
		if(arr[child]<arr[father])
		{
			Swap(&arr[child],&arr[father]);
			child=father;
			father=(child-1)/2;
		}
		else
		{
			break;
		}
	}
}

//打印数据
void HeapPrint(DataType*arr,int n)
{
	assert(arr);
	for(int i=0;i<n;i++)
	{
	printf("%d ",arr[i]);
	}
	printf("\n");
} 


//初始化堆
void HeapInit(Heap*hp)
{
	hp->a=NULL;
	hp->size=0;
	hp->capacity=0;
} 

//删除数据
void HeapPop(Heap*hp)
{
	assert(hp);
	assert(hp->a);
	hp->a[0]=hp->a[hp->size-1];
	hp->size--;
	HeapAdjust(hp->a,0,hp->size-1);
} 

//向下调整
void HeapAdjust(int*arr,int father,int size)
{
	int child=father*2+1;
	while(child<size)
	{
		if(child+1<size&&arr[child]>arr[child+1])
		{
			child++;
		}
		if(arr[child]<arr[father])
		{
			Swap(&arr[child],&arr[father]);
			father=child;
			child=(father+1)*2;
		}
		else
		{
			break;
		}
	}
} 

//取堆区首元素
DataType HeapTop(Heap*hp)
{
	assert(hp);
	assert(hp->size>0);
	return hp->a[0];	
} 




  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
数据结构C语言版)》是严蔚敏和吴伟民编写的一本经典教材,该教材是针对数据结构这门课程的C语言版本。作为一本数据结构的教材,该书系统地介绍了数据结构的各种概念、原理和算法,并通过C语言进行具体的实现和应用。 该书的内容主要包括线性表、栈和队列、串、树和二叉树、图、查找和排序、文件等内容。每章都有详细的讲解和示例代码,以及相应的习题和实验,可以帮助读者加深对数据结构的理解和掌握。 在《数据结构C语言版)》中,严蔚敏和吴伟民以清晰简洁的语言,深入浅出地介绍了数据结构的基本概念和常用算法。每个概念都有相应的应用示例和实现代码,读者可以通过阅读和实践来理解和掌握相关知识。此外,该书还给出了习题,通过解答这些习题可以帮助读者巩固所的知识。 《数据结构C语言版)》的编写结构严谨,知识内容完整,是一本非常好的数据结构教材。它适合作为高校计算机专业课程的教材,也适合作为自的参考书。无论是初者还是进阶者,都可以通过该书系统习和提升自己的数据结构能力。 总之,《数据结构C语言版)》是一本权威、经典的教材,适合对数据结构有一定了解的读者习。通过阅读和实践,读者可以全面了解数据结构的概念、原理和算法,并通过C语言实现来加深理解。同时,该书的作用不仅局限于课堂教,也可以作为读者进一步提升自己的参考书。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值