公众号做视频网站,网站建设财务项目管理制度,商城小程序开发发,项目建设总结报告目录 1.栈
1.1栈的概念及结构 1.2栈的实现 2.队列
2.1队列的概念及结构
2.2队列的实现 3.顺序栈的具体实现
3.1建头文Stack.h”
3.2创建具体接口实现文件Stack.c
3.2.1初始化 3.2.2入栈出栈
3.2.4判空
3.2.5栈的大小
3.2.6销毁栈
3.3主函数的实现 4.链队的具体实现…目录 1.栈
1.1栈的概念及结构 1.2栈的实现 2.队列
2.1队列的概念及结构
2.2队列的实现 3.顺序栈的具体实现
3.1建头文Stack.h”
3.2创建具体接口实现文件Stack.c
3.2.1初始化 3.2.2入栈出栈
3.2.4判空
3.2.5栈的大小
3.2.6销毁栈
3.3主函数的实现 4.链队的具体实现
4.1建头文Queue.h”
4.2创建具体接口实现文件Queue.c
4.2.1打印
4.2.2初始化 4.2.3出队入队
4.2.4返回队头队尾元素
4.2.5判空 4.2.6返回队列长度
4.2.7销毁队列 4.3.主函数的实现 1.栈
1.1栈的概念及结构 栈一种特殊的线性表其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端 称为栈顶另一端称为栈底。栈中的数据元素遵守后进先出LIFOLast In First Out的原则。 压栈栈的插入操作叫做进栈/压栈/入栈入数据在栈顶。 出栈栈的删除操作叫做出栈。出数据也在栈顶。 1.2栈的实现 栈的实现一般可以使用数组或者链表实现相对而言数组的结构实现更优一些。因为数组在尾上插入数据的 代价比较小。 2.队列
2.1队列的概念及结构 队列只允许在一端进行插入数据操作在另一端进行删除数据操作的特殊线性表队列具有先进先出 FIFO(First In First Out) 入队列进行插入操作的一端称为队尾 出队列进行删除操作的一端称为队头 2.2队列的实现 队列也可以数组和链表的结构实现使用链表的结构实现更优一些因为如果使用数组的结构出队列在数 组头上出数据效率会比较低。 3.顺序栈的具体实现
3.1建头文Stack.h” 为什么要创立头文件
#pragma once
#include stdio.h
#include string.h
#include stdlib.h
#include stdbool.h
#include assert.htypedef int STDataType;
typedef struct Stack {STDataType*a ;int capacity;int top;
}ST;void StackInit(ST*ps);void StackDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);STDataType StackTop(ST* ps);
//返回栈顶元素
bool StackEmpty(ST* ps);
//判空
int StackSize(ST* ps);3.2创建具体接口实现文件Stack.c
先引用#include Stack.h 3.2.1初始化
void StackInit(ST* ps)
{assert(ps);//创建一个节点空间类似顺序表ps-a (STDataType*)malloc(sizeof(STDataType) * 4);if (ps-a NULL){perror(malloc fail);exit(-1);}ps-top 0;ps-capacity 4;
} 3.2.2入栈出栈 void StackPush(ST* ps, STDataType x){assert(ps);if (ps-top ps-capacity)//判断是否栈满 需要扩容{STDataType* tmp (STDataType*)realloc(ps-a,sizeof(STDataType) * ps-capacity*2);if (tmp NULL){perror(melloc fail);exit(-1);}ps-a tmp;ps-capacity * 2;}ps-a[ps-top] x;ps-top;
}
void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));ps-top--;
} 3.2.3返回栈顶元素 STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps-a[ps-top - 1];
} 3.2.4判空 bool StackEmpty(ST* ps)
{assert(ps);/*if (ps-top 0){return true;}else{return false;}*/return(ps-top 0);
} 3.2.5栈的大小 int StackSize(ST* ps)
{assert(ps);return ps-top;
}3.2.6销毁栈 void StackDestroy(ST* ps)
{assert(ps);free(ps-a);ps-a NULL;ps-top 0;ps-capacity 0;
} 3.3主函数的实现 #include Stack.hvoid Test1()
{ST st;StackInit(st);StackPush(st, 1);StackPush(st, 2);StackPush(st, 3);StackPush(st, 4);StackPush(st, 5);printf(size:%d\n, StackSize(st)); // 不关心底层实现printf(size:%d\n, st.top 1); // 关心StackPop(st);StackPop(st);StackPop(st);//StackPop(st);printf(%d\n, StackTop(st));StackDestroy(st);}int main()
{Test1();return 0;
} 4.链队的具体实现
4.1建头文Queue.h” 为什么要创立头文件
#pragma once
#include stdio.h
#include string.h
#include stdlib.h
#include stdbool.h
#include assert.htypedef int QDataType;
typedef struct QueueNode {//创建队列结点QDataType data;struct QueueNode* next;//别学了单链表 就不会类比其他特殊链表结点的创建}QNode;
typedef struct Queue {//创建队列 指针结构体QNode* head;QNode* tail;int size;
}Queue;void QueueInit(Queue* ps);
void QueueDestroy(Queue* ps);
void QueuePush(Queue* ps, QDataType x);
void QueuePop(Queue* ps);
void QueuePrint(Queue* ps);
//返回队头队尾元素
QDataType Queuefront(Queue* ps);
QDataType Queueback(Queue* ps);bool QueueEmpty(Queue* ps);
int QueueSize(Queue* ps);4.2创建具体接口实现文件Queue.c
先引用#include Queue.h 4.2.1打印 void QueuePrint(Queue* ps)
{if (ps NULL){printf(NULL\n);}QNode* cur ps-head;while (cur){QNode* next cur-next;printf(%d-, cur-data);cur next;}printf(NULL\n);
} 4.2.2初始化 void QueueInit(Queue* ps)
{assert(ps);ps-head NULL;ps-tail NULL;ps-size 0;
} 4.2.3出队入队 void QueuePush(Queue* ps, QDataType x)
{QNode* newnode (QNode*)malloc(sizeof(QNode));if (newnode NULL){perror(malloc fail);exit(-1);}newnode-data x;newnode-next NULL;if (ps-tail NULL){ps-tail ps-head newnode;}else{ps-tail-next newnode;ps-tail newnode;}ps-size;}void QueuePop(Queue* ps)
{assert(ps);assert(!QueueEmpty(ps));if (ps-head-next NULL){free(ps-head);ps-head NULL;ps-tail NULL;ps-size 0;}else {QNode* del ps-head;ps-head ps-head-next;free(del);ps-size--;}
} 3.2.5头插头删 void SLPushFront(SL* ps, SLDataType x)
{assert(ps);SLCheckCapacity(ps);for (int i ps-size; i 0; i--){ps-a[i] ps-a[i-1];}ps-a[0] x;ps-size;
}void SLPopFront(SL* ps)
{assert(ps);for(int i 0; ips-size; i){ps-a[i] ps-a[i 1];}ps-size--;
} 4.2.4返回队头队尾元素 QDataType Queuefront(Queue* ps)
{assert(ps);assert(!QueueEmpty(ps));return (ps-head-data);}
QDataType Queueback(Queue* ps)
{assert(ps);assert(!QueueEmpty(ps));return (ps-tail-data);
} 4.2.5判空 bool QueueEmpty(Queue* ps)
{assert(ps);return ps-head NULL ps-tail NULL;
}
} 4.2.6返回队列长度 int QueueSize(Queue* ps)
{assert(ps);return (ps-size);
} 4.2.7销毁队列 void QueueDestroy(Queue* ps)
{assert(ps);QNode* cur ps-head;while (cur){QNode* next cur-next;free(cur);cur next;}ps-head NULL;ps-tail NULL;ps-size 0;} 4.3.主函数的实现
#include Queue.hvoid Test1(){Queue q;//初始化测试QueueInit(q);QueuePrint(q);//入队测试QueuePush(q, 1);QueuePush(q, 2);QueuePush(q, 3);QueuePrint(q);//出队测试QueuePop(q);QueuePop(q);QueuePop(q);QueuePrint(q);//返回队头队尾元素QueuePush(q, 1);QueuePush(q, 2);QueuePush(q, 3);printf(%d----%d\n, Queueback(q), Queuefront(q));QueuePrint(q);printf(%d, QueueSize(q));
}
int main()
{Test1();return 0;
}