当前位置: 首页 > news >正文

还能做网站的分类做网站 套用模板之后用什么改

还能做网站的分类,做网站 套用模板之后用什么改,域名,微信小程序一年费用多少钱目录 栈 概念与结构 栈的结构 初始化与销毁 入栈 出栈 取栈顶元素 取元素个数 整体实现 Stack.h Stack.c 队列 概念与结构 队列的结构 初始化与销毁 入队 出队 取队头和队尾元素 取元素个数 整体实现 Queue.h Queue.c 栈 概念与结构 栈:一种…

目录

概念与结构

栈的结构

初始化与销毁

入栈

出栈

取栈顶元素

取元素个数

整体实现

Stack.h

Stack.c

队列

概念与结构

队列的结构

初始化与销毁

入队

出队

取队头和队尾元素

取元素个数

整体实现

Queue.h

Queue.c


概念与结构

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO的原则。

压栈:栈的插入操作叫做进栈 / 压栈 / 入栈,入数据在栈顶。

出栈:栈的删除操作叫做出栈。出数据也在栈顶。

这里使用数组来实现栈,入栈出栈的时间复杂度都是 O(1) 。

栈的结构

栈和顺序表很像,把顺序表的size改为top。

typedef int STDataType;
typedef struct Stack
{STDataType* arr;int top;             //栈顶int capacity;
}ST;

初始化与销毁

void STInit(ST* ps)
{ps->arr = NULL;ps->top = ps->capacity = 0;
}void STDestroy(ST* ps)
{if (ps->arr)free(ps->arr);ps->arr = NULL;ps->top = ps->capacity = 0;
}

入栈

就像顺序表的尾插,入栈也一样

void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->capacity == ps->top){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* newarr = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));if (newarr == NULL){perror("realloc fail!");exit(1);}ps->arr = newarr;}ps->arr[ps->top] = x;ps->top++;
}

出栈

bool StackEmpty(ST* ps)        //返回bool型判断
{assert(ps);return ps->top == 0;
}void StackPop(ST* ps)          //出栈
{assert(!StackEmpty(ps));--ps->top;
}

取栈顶元素

STDataType StackTop(ST* ps)
{assert(!StackEmpty(ps));return ps->arr[ps->top - 1];   // ps->top - 1
}

取元素个数

int STSize(ST* ps)
{assert(ps);return ps->top;
}

整体实现

Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef int STDataType;
typedef struct Stack
{STDataType* arr;int top;int capacity;
}ST;void STInit(ST* ps);
void STDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
int STSize(ST* ps);
bool StackEmpty(ST* ps);

Stack.c

#include"Stack.h"void STInit(ST* ps)
{ps->arr = NULL;ps->top = ps->capacity = 0;
}void STDestroy(ST* ps)
{if (ps->arr)free(ps->arr);ps->arr = NULL;ps->top = ps->capacity = 0;
}void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->capacity == ps->top){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* newarr = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));if (newarr == NULL){perror("realloc fail!");exit(1);}ps->arr = newarr;}ps->arr[ps->top] = x;ps->top++;
}bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}void StackPop(ST* ps)
{assert(!StackEmpty(ps));--ps->top;
}STDataType StackTop(ST* ps)
{assert(!StackEmpty(ps));return ps->arr[ps->top - 1];
}int STSize(ST* ps)
{assert(ps);return ps->top;
}

队列

概念与结构

概念:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO。

入队列:进行插入操作的一端称为队尾。

出队列:进行删除操作的一端称为队头。

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

队列的结构

为了追求更高的效率,这里创建两个结构体,一个为队列本体,一个为队列的结点。

typedef int QDataType;typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;typedef struct Queue
{QueueNode* phead;QueueNode* ptail;int size;             //在数据多时,为了更好的知道队列内有多少元素,多
}Queue;                   //创建一个size来记录队列内元素的数量。

初始化与销毁

void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QueueNode* pcur = pq->phead;while (pcur){QueueNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;
}

入队

void QueuePush(Queue* pq,QDataType x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;if (pq->phead == NULL){pq->phead = pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}

出队

bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == 0;
}void QueuePop(Queue* pq)
{assert(!QueueEmpty(pq));if (pq->phead == pq->ptail){free(pq->phead);pq->phead = pq->ptail = NULL;}else{QueueNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}

取队头和队尾元素

QDataType QueueFront(Queue* pq)
{assert(!QueueEmpty(pq));return pq->phead->data;
}QDataType QueueBack(Queue* pq)
{assert(!QueueEmpty(pq));return pq->ptail->data;
}

取元素个数

int QueueSize(Queue* pq)
{//int size = 0;//QueueNode* pcur = pq->phead;//while (pcur)//{//	++size;//	pcur = pcur->next;//}//return size;return pq->size;
}

整体实现

Queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef int QDataType;typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;typedef struct Queue
{QueueNode* phead;QueueNode* ptail;int size;
}Queue;void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);

Queue.c

#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QueueNode* pcur = pq->phead;while (pcur){QueueNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;
}void QueuePush(Queue* pq,QDataType x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;if (pq->phead == NULL){pq->phead = pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == 0;
}void QueuePop(Queue* pq)
{assert(!QueueEmpty(pq));if (pq->phead == pq->ptail){free(pq->phead);pq->phead = pq->ptail = NULL;}else{QueueNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(!QueueEmpty(pq));return pq->phead->data;
}QDataType QueueBack(Queue* pq)
{assert(!QueueEmpty(pq));return pq->ptail->data;
}int QueueSize(Queue* pq)
{//int size = 0;//QueueNode* pcur = pq->phead;//while (pcur)//{//	++size;//	pcur = pcur->next;//}//return size;return pq->size;
}

http://www.dtcms.com/a/531106.html

相关文章:

  • 网站设计 psd用php做图书管理网站
  • 做网站什么是三网合一网站源码怎么写
  • 大型电子商务网站建设公司哪家公司建5g基站
  • 网站建设策划书ppt免费的设计软件有哪些
  • 重庆永川网站建设价格wordpress 访问控制
  • 安徽专业网站建设检修温州网站建设托管
  • 免费做那个的视频网站凡客家居怎么样
  • 如何高效率的建设网站白云区住房和建设水务局网站
  • 网站建设在哪里找客户淮南企业网站建设
  • 网站设计培训成都哪家好互动性的网站
  • 模板网站开发注意事项做公司
  • 天商阳光网站邮箱wordpress标签页样式
  • 高水平的郑州网站建设网上购物商城有哪些
  • 成都seo整站做公司网站要钱吗
  • 网站备案查询什么是短视频营销
  • 如何建购物网站免费开源代码网站
  • 衡水网站建设网络公司网站建设原则应考虑哪些方面
  • 湛江网站建设服务微网站开发平台wizi
  • 江苏建筑网站建设手机可以开发网站
  • 电子商务网站建设的难点贵州建设职业技术学院报名网站
  • 网站外链如何建设最有用怎么做类似清风dj网站
  • c2c商城网站建设二次开发品牌建设岗位职责
  • 合肥专业做淘宝网站偃师网站
  • 铜川做网站淘宝网站设计价格
  • 广西钦州有做网站的公司吗网站建设购买什么境外主机
  • 网页游戏网站网址微信微商城怎么开通
  • 深圳技术支持 骏域网站建设贵州省建设项目备案查询网站
  • 网络认证网站多语言社交网站开发
  • 免费爱做网站建站网站破解版
  • wordpress上弹广告山东网络推广优化排名