常用数据结构库

就跟标题写的一样。

堆栈

Stack.h

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

typedef int Elemtype ;
typedef struct
{
	int maxSize;
	int loaded;
	Elemtype *key;
}*Stack;

Stack StackBuild(int max);
void StackDestory(Stack s);
int isSFull(Stack s);
int isSEmpty(Stack s);
void Stackin(Stack s, Elemtype key);
Elemtype Stackout(Stack s);
Elemtype Peek(Stack s);
void StackClear(Stack s);
int StackSize(Stack s);


Stack.c

#include"Stack.h"


Stack StackBuild(int max)
{
	Stack s = calloc(1,sizeof(Stack));
	s->loaded = 0;
	s->maxSize = max;
	s->key = calloc(1, sizeof(int) * (max+1));
	s->key[0] = INT_MAX;
	return s;
}
void StackDestory(Stack s)
{
	free(s->key);
	free(s);
}
int isSFull(Stack s)
{
	return s->maxSize == s->loaded ? 1 : 0;
}
int isSEmpty(Stack s)
{
	return !s->loaded;
}
void Stackin(Stack s, Elemtype key)
{
	if (isFull(s))
		return;
	s->key[++s->loaded] = key;
}
Elemtype Stackout(Stack s)
{
	if (isEmpty(s))
		return NULL;
	return s->key[s->loaded--];
}
Elemtype Peek(Stack s)
{
	if (isEmpty(s))
		return;
	return s->key[s->loaded];
}
void StackClear(Stack s)
{
	s->loaded = 0;
}
int StackSize(Stack s)
{
	return s->loaded;
}

队列

Queue.h

#pragma once
#include
#include

typedef int Elemtype ;
typedef struct
{
	int maxSize;
	int loaded;
	int begin;
	int end;
	Elemtype *key;
}*Queue;

Queue QueueBuild(int max);
void QueueDestory(Queue q);
int isQFull(Queue q);
int isQEmpty(Queue q);
void Queuein(Queue q, Elemtype key);
Elemtype Queueout(Queue q);
void QueueClear(Queue q);
int QueueSize(Queue q);

Queue.c

#include"Queue.h"

Queue QueueBuild(int max)
{
	Queue q = calloc(1,sizeof(Queue));
	q->loaded = q->begin = q->end = 0;
	q->maxSize = max;
	q->key = calloc(1, sizeof(int) * (max+1));
	return q;
}
void QueueDestory(Queue q)
{
	free(q->key);
	free(q);
}
int isQFull(Queue q)
{
	return q->loaded == q->maxSize? 1:0;
}
int isQEmpty(Queue q)
{
	return !q->loaded;
}
void Queuein(Queue q, Elemtype key)
{
	if (isQFull(q))
		return;
	q->key[(q->begin++)%q->maxSize]=key;
	q->loaded++;
}
Elemtype Queueout(Queue q)
{
	if (isQEmpty(q))
		return -INT_MAX;
	q->loaded--;
	return q->key[(q->end++)%q->maxSize];
}
void QueueClear(Queue s)
{
	s->loaded = 0;
}
int QueueSize(Queue q)
{
	return q->loaded;
}

 

二叉树(遍历和创建)

BinaryTree.h

#pragma once
#include
#include

typedef int ElemType;

typedef struct BinaryTreeNode// P.75
 {
 ElemType Data;
 struct BinaryTreeNode *lChild, *rChild;
 }BinaryTreeNode;
 typedef BinaryTreeNode* BiTree, *TNode;

typedef struct BinaryTree// I made it, also shown in course slides
 {
 BinaryTreeNode *root;
 }BinaryTree;


 void PreOrderTransverse(BiTree t);
 void InOrderTransverse(BiTree t);
 void PostOrderTransverse(BiTree t);
 BinaryTreeNode* PreCreateBt(BinaryTreeNode *t);

BinaryTree.c

/*
I don't have the copyright of these code.
*/
////////////////////////////////////////////////////////////////////////////////
 // BinaryTree PreOrder/InOrder/PostOrder Traverse Demo Program
 // Please note: This is a minimal functional traverse program
 // that lacks most of the functions in ADT 5.2
 // I will made another full version of BinaryTree program
#include "BinaryTree.h"
////////////////////////////////////////////////////////////////////////////////
 // I don’t know why the author used “BiTree” here,
 // so I made a typedef in Line 17, as below:
 // typedef BinaryTreeNode* BiTree;
 // Program 5.1-5.3
 void PreOrderTransverse(BiTree t)
 {
 if(t == NULL)
 return;

printf(“%c”, t->Data);
 PreOrderTransverse(t->lChild);
 PreOrderTransverse(t->rChild);
 }

void InOrderTransverse(BiTree t)
 {
 if(t == NULL)
 return;

InOrderTransverse(t->lChild);
 printf(“%c”, t->Data);
 InOrderTransverse(t->rChild);
 }

void PostOrderTransverse(BiTree t)
 {
 if(t == NULL)
 return;

PostOrderTransverse(t->lChild);
 PostOrderTransverse(t->rChild);
 printf(“%c”, t->Data);
 }

////////////////////////////////////////////////////////////////////////////////
 // Before traverse, you need to create a BinaryTree first
 // Here we use PreOrder manner to create one
 // In this function, you can input a string like ABEH###C#D##MN###
 // Program 5.4
 BinaryTreeNode* PreCreateBt(BinaryTreeNode *t)
 {
 char ch;

ch = getchar();
 if(ch == ‘#’)
t = NULL;
 else
 {
 t = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
 t->Data = ch;// A mistake in textbook
 t->lChild = PreCreateBt(t->lChild);
 t->rChild = PreCreateBt(t->rChild);
 }

return t;
 }

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注