mirror of
https://github.com/CodePanda66/CSPostgraduate-408.git
synced 2023-05-21 21:49:33 +08:00
🎨 format file
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
//
|
||||
// Created by kim on 2020/6/17.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//下面四种函数的时间复杂度值得分析一二
|
||||
|
||||
//逐步递增型爱你
|
||||
@@ -58,6 +61,10 @@ void loveYou4(int n){
|
||||
printf("I love you %d\n",n);
|
||||
}//递归调用会带来多余的内存开销
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
//测试函数
|
||||
void Test(){
|
||||
LoveYou0(30);
|
||||
@@ -71,6 +78,8 @@ void Test(){
|
||||
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
//
|
||||
// Created by kim on 2020/6/17.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//顺序表——静态分配的实现方式
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
#include <stdio.h>
|
||||
#define MaxSize 10
|
||||
|
||||
@@ -14,8 +18,9 @@ typedef struct {
|
||||
//函数声明
|
||||
void PrintSqList(SqList L);
|
||||
|
||||
//
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化
|
||||
void InitList(SqList &L){
|
||||
@@ -137,6 +142,10 @@ void ChangeTest(SqList &L,int e,int em1,int i, int em2){
|
||||
//销毁
|
||||
//由于静态分配方式是通过声明数组的方式实现的,故不需要手动销毁SqList表,在使用完成之后,系统会自动删除数据并回收数据空间
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
//测试
|
||||
//打印整个顺序表
|
||||
void PrintSqList(SqList L){
|
||||
@@ -192,7 +201,7 @@ void Test(){
|
||||
//打印输出
|
||||
PrintSqList(L);
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
//主函数
|
||||
int main(){
|
||||
Test();
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
//
|
||||
// Created by kim on 2020/6/17.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//顺序表——动态分配的实现方式
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
#define InitSize 10
|
||||
typedef struct {
|
||||
int *data; //指示动态分配数组的指针
|
||||
@@ -12,6 +17,11 @@ typedef struct {
|
||||
int length;//顺序表当前的长度
|
||||
}SeqList;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化
|
||||
bool InitList(SeqList &L){
|
||||
//用 malloc 函数申请一片连续的存储空间
|
||||
@@ -113,6 +123,11 @@ void DestroySqList(SeqList &L){
|
||||
L.length=0;
|
||||
}
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
//测试
|
||||
//打印整个顺序表
|
||||
void PrintSqList(SeqList L){
|
||||
@@ -136,7 +151,7 @@ void TestPrint(bool test,char message[]){
|
||||
printf("%s失败啦!\n",message);
|
||||
}
|
||||
|
||||
int main(){
|
||||
void testModule(){
|
||||
SeqList L;
|
||||
|
||||
TestPrint(InitList(L),"初始化");
|
||||
@@ -177,6 +192,11 @@ int main(){
|
||||
DestroySqList(L);
|
||||
TestPrint(Empty(L),"删除");
|
||||
PrintSqList(L);
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
testModule();
|
||||
return 0;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim on 2020/6/19.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//带头节点的单链表
|
||||
@@ -7,11 +8,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
typedef struct LNode{
|
||||
int data;
|
||||
LNode *next;
|
||||
}LNode,*LinkList;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初试化(带有头节点)
|
||||
bool InitList(LinkList &L){
|
||||
L=(LNode *)malloc(sizeof(LNode));//分配一个头节点
|
||||
@@ -97,6 +105,9 @@ bool DeleteNode(LNode *p){
|
||||
|
||||
}
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
//Test
|
||||
void TestPrint(bool test,char about[]){
|
||||
if (test)
|
||||
@@ -117,6 +128,7 @@ void PrintList(LinkList L){
|
||||
}
|
||||
|
||||
}
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
LinkList L;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
//
|
||||
// Created by kim on 2020/6/19.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//不带头节点的单链表
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
typedef struct LNode{
|
||||
int data;
|
||||
struct LNode *next;
|
||||
@@ -19,6 +22,11 @@ typedef struct LNode{
|
||||
//typedef struct LNode LNode;
|
||||
//typedef struct LNode *LinkList;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化
|
||||
bool InitList(LinkList &L){
|
||||
L=NULL;//空表暂时没有任何数据
|
||||
@@ -64,6 +72,11 @@ bool ListInsert(LinkList &L,int i, int e){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
//Test
|
||||
void TestPrint(bool test,char about[]){
|
||||
if (test)
|
||||
@@ -84,6 +97,8 @@ void PrintList(LinkList L){
|
||||
}
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
LinkList L;
|
||||
TestPrint(InitList(L),"初始化");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim on 2020/6/20.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//带有头节点版本——双链表
|
||||
@@ -7,11 +8,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
typedef struct DNode{
|
||||
int data;//数据域
|
||||
struct DNode *prior,*next;//前指针和后指针
|
||||
}DNode,*DLinkList;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化
|
||||
bool InitDLinkList(DLinkList &L){
|
||||
L = (DNode *)malloc(sizeof(DNode));//分配一个头节点
|
||||
@@ -88,6 +96,11 @@ bool PrintPriorElemsOverHead(DNode *p){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
//测试函数
|
||||
void TestPrint(bool test,char message[]){
|
||||
if (test)
|
||||
@@ -96,6 +109,8 @@ void TestPrint(bool test,char message[]){
|
||||
printf("%s失败啦!\n",message);
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
DLinkList L;
|
||||
TestPrint(InitDLinkList(L),"初始化");
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
//
|
||||
// Created by kim on 2020/6/20.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//循环单链表
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
typedef struct LNode{
|
||||
int data;
|
||||
struct LNode *next;
|
||||
}LNode,*LinkList;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化一个循环单链表
|
||||
bool InitRLinkList(LinkList &L){
|
||||
L=(LNode *)malloc(sizeof(LNode));//分配一个头节点
|
||||
@@ -25,6 +32,13 @@ bool IsTail(LinkList L,LNode *p){
|
||||
return (p->next==L);
|
||||
}
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
//
|
||||
// Created by kim on 2020/6/20.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//循环双链表
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
typedef struct DNode{
|
||||
int data;
|
||||
struct DNode *prior,*next;
|
||||
}DNode,*DLinkList;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化
|
||||
bool InitRDLinkList(DLinkList &L){
|
||||
L=(DNode *)malloc(sizeof(DNode));//分配头节点
|
||||
@@ -42,6 +49,12 @@ bool DeleteNextDNode(DLinkList &L,DNode *p){
|
||||
free(q);
|
||||
return true;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim on 2020/6/20.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//静态链表
|
||||
@@ -7,29 +8,41 @@
|
||||
#include <stdlib.h>
|
||||
#define MaxSize 10
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
//第一种定义方法
|
||||
struct Node0{
|
||||
int data;
|
||||
int next;
|
||||
};
|
||||
|
||||
void testSLinkList(){
|
||||
//声明时
|
||||
struct Node0 a[MaxSize];
|
||||
}
|
||||
|
||||
//第二种定义方法
|
||||
typedef struct Node{
|
||||
int data;
|
||||
int next;
|
||||
}SLinkList[MaxSize];
|
||||
//相比之下,第二种方法可读性更强!,推荐
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
void testSLinkList(){
|
||||
//声明时
|
||||
struct Node0 a[MaxSize];
|
||||
}
|
||||
|
||||
void TestSLinkList(){
|
||||
//第二种方法声明时
|
||||
SLinkList a;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
//相比之下,第二种方法可读性更强!,推荐
|
||||
/**测试模块**/
|
||||
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
//后续来填坑吧!!
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
//
|
||||
// Created by kim on 2020/6/21.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//顺序栈的实现
|
||||
#include <stdio.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
int data[MaxSize];
|
||||
int top;
|
||||
} SqStack;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化
|
||||
void InitStack(SqStack &S) {
|
||||
@@ -94,7 +100,9 @@ int GetTopOther1(SqStack S) {
|
||||
if (S.top == 0)return -1;
|
||||
return S.data[S.top - 1];
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
//打印整个栈
|
||||
void PrintStack(SqStack S) {
|
||||
@@ -188,7 +196,7 @@ void testStack() {
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
printf("Hello, SqStack!");
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
//
|
||||
// Created by kim on 2020/7/28.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//共享顺序栈的实现
|
||||
//简单来说就是两个栈共享一片存储空间,提高顺序栈的对存储空间的使用率
|
||||
#include <stdio.h>
|
||||
|
||||
/**定义模块**/
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
int data[MaxSize];
|
||||
@@ -13,7 +15,9 @@ typedef struct {
|
||||
int top1;
|
||||
} ShStack;
|
||||
//从结构体的定义就可以看出来,两个共享栈的根源就在于定义两个指针
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
//初始化
|
||||
void InitStack(ShStack &S) {
|
||||
S.top0 = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
@@ -74,8 +78,9 @@ bool GetTop1(ShStack S, int &x) {
|
||||
x = S.data[S.top1];
|
||||
return true;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
//打印整个栈,栈0
|
||||
void PrintStack0(ShStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
@@ -155,6 +160,7 @@ void testShStack() {
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
printf("Hello, ShStack!");
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
//
|
||||
// Created by kim on 2020/7/28.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//链栈的实现
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
# define MaxSize 10
|
||||
typedef struct LinkNode {
|
||||
int data;
|
||||
struct LinkNode *next;
|
||||
} *LinkStack;
|
||||
//从结构体的定义就可以看出来,两个共享栈的根源就在于定义两个指针
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
//初始化
|
||||
bool InitStack(LinkStack &LS) {
|
||||
LS = (LinkNode *) malloc(sizeof(LinkNode));//分配一个头节点
|
||||
@@ -54,8 +58,9 @@ bool GetTop(LinkStack LS, int &x) {
|
||||
x = LS->next->data;
|
||||
return true;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
//打印整个栈,栈
|
||||
void PrintStack(LinkStack LS) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
@@ -114,7 +119,7 @@ void testLinkStack() {
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
printf("Hello, LinkNode!");
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
//
|
||||
// Created by kim yang on 2020/7/31.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//循环顺序队列的第一种实现方式
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**定义模块**/
|
||||
#define MaxSize 10
|
||||
|
||||
typedef struct {
|
||||
int data[MaxSize];//
|
||||
int front, rear;//对头指针和队尾指针
|
||||
} SqQueue;
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
//初始化
|
||||
void InitQueue(SqQueue &Q) {
|
||||
Q.rear = Q.front = 0;//初始化时,队头队尾都指向0
|
||||
@@ -49,7 +53,9 @@ bool GetHead(SqQueue Q, int &x) {
|
||||
x = Q.data[Q.front];
|
||||
return true;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
//打印整个队列
|
||||
void PrintQueue(SqQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
@@ -95,6 +101,7 @@ void TestQueue() {
|
||||
|
||||
printf("结束测试!\n");
|
||||
}
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
TestQueue();
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/1.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//循环顺序队列的第二种实现方式
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**定义模块**/
|
||||
#define MaxSize 10
|
||||
|
||||
typedef struct {
|
||||
@@ -13,7 +15,9 @@ typedef struct {
|
||||
int front, rear;//对头指针和队尾指针
|
||||
int size;//利用size变量记录队列长度,并用作判满的条件!有了size就不会浪费一个存储空间
|
||||
} SqQueue;
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
//初始化
|
||||
void InitQueue(SqQueue &Q) {
|
||||
Q.rear = Q.front = 0;//初始化时,队头队尾都指向0
|
||||
@@ -52,7 +56,9 @@ bool GetHead(SqQueue Q, int &x) {
|
||||
x = Q.data[Q.front];
|
||||
return true;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
//打印整个队列
|
||||
void PrintQueue(SqQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
@@ -104,6 +110,7 @@ void TestQueue() {
|
||||
|
||||
printf("结束测试!\n");
|
||||
}
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
TestQueue();
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/1.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//循环顺序队列的第二种实现方式
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**定义模块**/
|
||||
#define MaxSize 10
|
||||
|
||||
typedef struct {
|
||||
@@ -12,7 +15,9 @@ typedef struct {
|
||||
int front,rear;//对头指针和队尾指针
|
||||
int tag;//利用tag变量记录最后一次操作是什么,0为删除,1为插入,并用作判满的条件!有了tag就不会浪费一个存储空间
|
||||
}SqQueue;
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
//初始化
|
||||
void InitQueue(SqQueue &Q){
|
||||
Q.rear=Q.front=0;//初始化时,队头队尾都指向0
|
||||
@@ -50,7 +55,9 @@ bool GetHead(SqQueue Q,int &x){
|
||||
x=Q.data[Q.front];
|
||||
return true;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
//打印整个队列
|
||||
void PrintQueue(SqQueue Q){
|
||||
printf("开始打印队列\n");
|
||||
@@ -101,6 +108,7 @@ void TestQueue(){
|
||||
|
||||
printf("结束测试!\n");
|
||||
}
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
TestQueue();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/2.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//链式队列(带头节点版本)
|
||||
@@ -7,6 +8,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
typedef struct LinkNode {
|
||||
int data;
|
||||
struct LinkNode *next;
|
||||
@@ -15,7 +17,9 @@ typedef struct LinkNode {
|
||||
typedef struct {
|
||||
LinkNode *front, *rear;
|
||||
} LinkQueue;
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
void InitQueue(LinkQueue &Q) {
|
||||
Q.front = Q.rear = (LinkNode *) malloc(sizeof(LinkNode));
|
||||
//初始化时,front 、rear 都指向头节点
|
||||
@@ -54,7 +58,9 @@ bool GetHead(LinkQueue Q, int &x) {
|
||||
bool QueueEmpty(LinkQueue Q){
|
||||
return Q.front==Q.rear? true: false;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
void PrintQueue(LinkQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
int i = 0;
|
||||
@@ -105,6 +111,7 @@ void TestLinkQueue() {
|
||||
|
||||
printf("测试结束!\n");
|
||||
}
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
TestLinkQueue();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/3.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//链式队列(带头节点版本)
|
||||
@@ -7,6 +8,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
typedef struct LinkNode {
|
||||
int data;
|
||||
struct LinkNode *next;
|
||||
@@ -15,7 +18,9 @@ typedef struct LinkNode {
|
||||
typedef struct {
|
||||
LinkNode *front, *rear;
|
||||
} LinkQueue;
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
void InitQueue(LinkQueue &Q) {
|
||||
Q.front = Q.rear = NULL;
|
||||
//不带头点,初始化时,front 、rear 指向NULL
|
||||
@@ -68,7 +73,9 @@ bool GetHead(LinkQueue Q, int &x) {
|
||||
bool QueueEmpty(LinkQueue Q) {
|
||||
return Q.front == Q.rear ? true : false;
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
void PrintQueue(LinkQueue Q) {
|
||||
printf("开始打印队列\n");
|
||||
int i = 0;
|
||||
@@ -119,6 +126,7 @@ void TestLinkQueue() {
|
||||
|
||||
printf("测试结束!\n");
|
||||
}
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
TestLinkQueue();
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/3.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//栈的应用
|
||||
|
||||
/**以下都是基础操作定义以及实现的方式和前面一样**/
|
||||
#include <stdio.h>
|
||||
|
||||
/**定义模块**/
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
char data[MaxSize];
|
||||
int top;
|
||||
} SqStack;
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
/**以下都是基础操作定义以及实现的方式和前面一样**/
|
||||
//初始化
|
||||
void InitStack(SqStack &S) {
|
||||
S.top = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
@@ -45,14 +49,6 @@ bool StackEmpty (SqStack S) {
|
||||
return S.top==-1;
|
||||
}
|
||||
|
||||
//打印整个栈
|
||||
void PrintStack(SqStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top >= 0) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top, S.data[S.top--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
/**以上都是基础操作定义以及实现的方式和前面一样**/
|
||||
bool bracketCheck(char str[],int length){
|
||||
@@ -76,7 +72,17 @@ bool bracketCheck(char str[],int length){
|
||||
}
|
||||
return StackEmpty(S);//最后检查栈,若空匹配成功,非空匹配失败
|
||||
}
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
//打印整个栈
|
||||
void PrintStack(SqStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top >= 0) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top, S.data[S.top--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
void TestQueueApplication(){
|
||||
printf("开始测试\n");
|
||||
@@ -116,6 +122,8 @@ void TestQueueApplication(){
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
/**测试模块**/
|
||||
|
||||
int main(){
|
||||
TestQueueApplication();
|
||||
return 0;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/3.
|
||||
// Created by Kim Yang on 2020/8/3.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//顺序存储——静态数组实现方式(定长顺序存储),注意下面实现在数组中存放字符串时,都会舍弃,Str[0],第一个结点的空间,以保证字符下标和数组下标保证一致
|
||||
#include <stdio.h>
|
||||
#include <cstring>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
#define MAXLEN 15 //预定义最大串长为15
|
||||
|
||||
typedef struct {
|
||||
@@ -13,30 +16,37 @@ typedef struct {
|
||||
int length; //串的实际长度
|
||||
} SString;
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
//初始化
|
||||
void InitStr(SString &S) {
|
||||
S.ch[1] = ' ';
|
||||
S.ch[1] = '\0';//以字符串结束符号来作为初始状态
|
||||
S.length = 0;
|
||||
|
||||
}
|
||||
|
||||
//赋值操作
|
||||
bool StrAssign(SString &T, char *str[]) {
|
||||
if (str[0] == NULL)return false;//传入空数组就失败
|
||||
bool StrAssign(SString &T, char *str) {
|
||||
|
||||
if (str[0] == '\0')return false;//传入空数组就失败,条件依据初始化操作和清空操作而定
|
||||
int strLength = sizeof(str) / sizeof(str[0]);
|
||||
for (int i = 0; i < strLength; ++i) {
|
||||
T.ch[i + 1] = *str[i];//空置T的第一个元素位置
|
||||
T.ch[i + 1] = str[i];//空置T的第一个元素位置
|
||||
}
|
||||
T.length = strLength;
|
||||
return true;
|
||||
}
|
||||
|
||||
//复制操作
|
||||
bool StrCopy(SString &T, SString S) {
|
||||
void StrCopy(SString &T, SString S) {
|
||||
|
||||
for (int i = 1; i < S.length; ++i) {
|
||||
T.ch[i] = S.ch[i];
|
||||
}
|
||||
T.length = S.length;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
//判空
|
||||
@@ -44,16 +54,6 @@ bool StrEmpty(SString S) {
|
||||
return S.length == 0;
|
||||
}
|
||||
|
||||
//清空操作
|
||||
void ClearStr(SString &S) {
|
||||
S.length = 0;
|
||||
memset(S.ch, 0, MAXLEN);//用到了一个cstring库中的memset函数
|
||||
}
|
||||
|
||||
//销毁操作
|
||||
void DestoryString(SString &S) {
|
||||
|
||||
}
|
||||
|
||||
//串链操作
|
||||
void Concat(SString &T, SString S1, SString S2) {
|
||||
@@ -162,7 +162,7 @@ int Index_KMP(SString S, SString T, int next[]) {
|
||||
}
|
||||
|
||||
//求模式串T的next数组
|
||||
void Get_Next(SString T, int next[]) {
|
||||
void getNext(SString T, int *next) {
|
||||
int i = 1, j = 0;
|
||||
next[1] = 0;
|
||||
while (i < T.length) {
|
||||
@@ -182,7 +182,7 @@ void Get_Next(SString T, int next[]) {
|
||||
int Index_KMP1(SString S, SString T) {
|
||||
int i = 1, j = 1;
|
||||
int next[T.length + 1];
|
||||
Get_Next(T, next);
|
||||
getNext(T, next);
|
||||
while (i <= S.length && j <= T.length) {
|
||||
if (j == 0 || S.ch[i] == T.ch[j]) {
|
||||
++i;
|
||||
@@ -201,7 +201,7 @@ int Index_KMP1(SString S, SString T) {
|
||||
void Get_BetterNext(SString T, int betterNext[]) {
|
||||
int i = 1, j = 0;
|
||||
int next[T.length + 1];
|
||||
Get_Next(T, next);//先求出next数组
|
||||
getNext(T, next);//先求出next数组
|
||||
betterNext[1] = 0;//令betterNext[1]=0
|
||||
for (int j = 2; j <= T.length; ++j) {
|
||||
if (T.ch[next[j]] == T.ch[j])
|
||||
@@ -211,29 +211,77 @@ void Get_BetterNext(SString T, int betterNext[]) {
|
||||
}
|
||||
}
|
||||
|
||||
void PrintStr(SString S){
|
||||
for (int i = 1; i <=S.length ; ++i) {
|
||||
printf("%s",S.ch[i]);
|
||||
}
|
||||
//清空操作
|
||||
void ClearStr(SString &S) {
|
||||
S.length = 0;
|
||||
memset(S.ch, '\0', MAXLEN);//用到了一个cstring库中的memset函数
|
||||
}
|
||||
|
||||
void TestStr() {
|
||||
//销毁操作
|
||||
void DestoryString(SString &S) {
|
||||
|
||||
}
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
/**测试模块**/
|
||||
void printDs(SString S, char *StrName) {
|
||||
printf("当前%s字符串内容为:", StrName);
|
||||
for (int i = 1; i <= S.length; ++i) {
|
||||
if (S.ch[i] != '\0')
|
||||
printf("%c", S.ch[i]);//注意输出单个字符用的是%c,而%s是输出一个字符串
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void testModule() {
|
||||
printf("开始测试!\n");
|
||||
|
||||
SString S,T;
|
||||
SString S, T;
|
||||
InitStr(S);
|
||||
char *str1[10]={"k","i","m"};
|
||||
printf("%s\n",str1[0]);
|
||||
if (StrAssign(S,str1)){
|
||||
InitStr(T);
|
||||
char str1[] = "kim";//使用这种初始化列表进行初始化,最后会数组会多一个结束符'\0'
|
||||
// char str1[] = {'k','i','m'};
|
||||
// 而这种不会,所以在选择初始化方式的时候尽量做到统一,否则你很有可能因为'\0'而匹配不到子串
|
||||
|
||||
char str2[] = "kimyang";
|
||||
|
||||
if (StrAssign(S, str1)) {
|
||||
printf("赋值操作成功啦!\n");
|
||||
} else {
|
||||
printf("赋值操作失败了!\n");
|
||||
}
|
||||
// PrintStr(S);
|
||||
printDs(S, "S");
|
||||
if (StrAssign(T, str2)) {
|
||||
printf("赋值操作又成功啦!\n");
|
||||
} else {
|
||||
printf("赋值操作失败了!\n");
|
||||
}
|
||||
printDs(T, "T");
|
||||
|
||||
SString S1;
|
||||
InitStr(S1);
|
||||
if (StrEmpty(S1)) {
|
||||
printf("字符串为空!\n");
|
||||
} else {
|
||||
printf("字符串非空!\n");
|
||||
}
|
||||
StrCopy(S1, S);
|
||||
if (StrEmpty(S1)) {
|
||||
printf("字符串为空!\n");
|
||||
} else {
|
||||
printf("字符串非空!\n");
|
||||
}
|
||||
printDs(S1, "S1");
|
||||
|
||||
|
||||
printf("测试结束!\n");
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
|
||||
int main() {
|
||||
TestStr();
|
||||
testModule();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/7.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//顺序存储——动态数组实现方式(堆分配存储)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/8.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//链式存储——单结点单字符
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/8.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//链式存储——单结点多字符
|
||||
|
||||
31
DataStructure/DS_3_String/Test.cpp
Normal file
31
DataStructure/DS_3_String/Test.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by Kim Yang on 2020/8/14.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
#include <stdio.h>
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
/**定义模块**/
|
||||
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
/**实现模块**/
|
||||
|
||||
|
||||
/**测试模块**/
|
||||
void printDS() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
void testModule() {
|
||||
|
||||
}
|
||||
|
||||
/**测试模块**/
|
||||
int main() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/11.
|
||||
// Created by Kim Yang on 2020/8/14.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//顺序存储
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#define MaxSize 100
|
||||
|
||||
struct TreeNode{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/11.
|
||||
// Created by Kim Yang on 2020/8/14.
|
||||
// Copyright (c) Kim Yang All rights reserved.
|
||||
//
|
||||
|
||||
//链式存储
|
||||
|
||||
Reference in New Issue
Block a user