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

做党和人民满意的好教师PPT网站站长推广网

做党和人民满意的好教师PPT网站,站长推广网,wordpress站群功能,视频高清线hdmiLei宝啊:个人主页(也许有你想看的) 愿所有美好不期而遇 前言 : 栈和队列的实现与链表的实现很相似,新瓶装旧酒,没什么新东西。 可以参考这篇文章: -------------------------无头单向不循环…

 Lei宝啊:个人主页(也许有你想看的)

愿所有美好不期而遇



前言 :

栈和队列的实现与链表的实现很相似,新瓶装旧酒,没什么新东西。

可以参考这篇文章:

-------------------------无头单向不循环链表和带头双向循环链表的创建---------------------------

 

逻辑图: 

 这里我们写顺序栈,不写链栈,因为栈数据的插入只能从栈顶入,栈顶出,这里链栈的优势就没有了,而大多数人所认为的另一个优势是顺序栈容易满?我们难道不能动态开一个,写一个checkcapacity吗,让他满了自动扩容,虽然有空间的损失,但这个并不是什么问题。再一个顺序栈的开辟与释放更为简单,直接释放掉动态开辟的数组空间就好。(当然,链栈也有其优势,但我更认可顺序栈)

头文件: 

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>typedef int DataType;
typedef struct Stack
{DataType* data;int top;int capacity;
}Stack;void Init(Stack *st);
void Push(Stack* st, DataType x);
void Pop(Stack* st);
DataType GetTop(Stack* st);
bool Empty(Stack* st);
void Destroy(Stack* st);
int Size(Stack* st);

Test文件(main):

#include "join_stack.h"void Test();int main()
{Test();return 0;
}void Test()
{Stack st;Init(&st);Push(&st, 1);Push(&st, 2);Push(&st, 3);Push(&st, 4);Push(&st, 5);Push(&st, 6);printf("size = %d\n", Size(&st));while (!Empty(&st)){printf("%d ", GetTop(&st));Pop(&st);}putchar('\n');Destroy(&st);
}

函数源文件:

#include "join_stack.h"void Init(Stack* st)
{assert(st);st->data = NULL;st->top = 0;st->capacity = 0;
}void Push(Stack* st, DataType x)
{assert(st);if (st->capacity == st->top){int newcapacity = (st->capacity == 0) ? 4 : st->capacity * 2;DataType* temp = (DataType*)realloc(st->data, sizeof(DataType) * newcapacity);if (temp == NULL){perror("realloc fail");exit(-1);}st->data = temp;st->capacity = newcapacity;}st->data[st->top++] = x;
}void Pop(Stack* st)
{assert(st);assert(st->top > 0);st->top--;
}DataType GetTop(Stack* st)
{assert(st);assert(st->top > 0);return st->data[st->top - 1];
}bool Empty(Stack* st)
{assert(st);return (st->top == 0);
}void Destroy(Stack* st)
{assert(st);free(st->data);st->data = NULL;st->top = st->capacity = 0;}int Size(Stack* st)
{assert(st);return st->top;
}


 

队列 

逻辑图:

队列我们就用链式结构,这和链表非常像,只是不能在中间插入,而且只能尾进头出。 

头文件:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>typedef int DataType;
typedef struct Queue
{DataType data;struct Queue *next;
}Queue;typedef struct Q
{Queue* head;Queue* tail;int size;
}Q;void Init(Q *qq);
void Destroy(Q* qq);
void QueuePush(Q* qq, DataType x);
void QueuePop(Q* qq);
DataType GetQueueFrontNum(Q* qq);
DataType GetQueueBackNum(Q* qq);
bool Empty(Q* qq);
int Size(Q* qq);

 Test文件(main):

#define _CRT_SECURE_NO_WARNINGS 1
#include "newQueue.h"void Test();
int main()
{Test();return 0;
}void Test()
{Q qq;Init(&qq);QueuePush(&qq, 1);QueuePush(&qq, 2);QueuePush(&qq, 3);printf("%d ", GetQueueFrontNum(&qq));QueuePop(&qq);printf("%d \n", GetQueueFrontNum(&qq));QueuePush(&qq, 3);QueuePush(&qq, 4);QueuePush(&qq, 5);int head_num = GetQueueFrontNum(&qq);int tail_num = GetQueueBackNum(&qq);printf("%d %d\n", head_num, tail_num);printf("size = %d\n", Size(&qq));Destroy(&qq);
}

函数源文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include "newQueue.h"void Init(Q* qq)
{assert(qq);qq->head = NULL;qq->tail = NULL;qq->size = 0;
}void QueuePush(Q* qq, DataType x)
{assert(qq);Queue* temp = (Queue*)malloc(sizeof(Queue));if (temp == NULL){perror("malloc fail");exit(-1);}temp->data = x;temp->next = NULL;if (qq->tail == NULL)qq->head = qq->tail = temp;else{qq->tail->next = temp;qq->tail = temp;}qq->size++;
}void QueuePop(Q* qq)
{assert(qq);assert(!Empty(qq));if (qq->head == qq->tail){free(qq->head);qq->head = qq->tail = NULL;}else{Queue* next = qq->head->next;free(qq->head);qq->head = next;}qq->size--;
}DataType GetQueueFrontNum(Q* qq)
{assert(qq);assert(!Empty(qq));return qq->head->data;
}DataType GetQueueBackNum(Q* qq)
{assert(qq);assert(!Empty(qq));return qq->tail->data;
}bool Empty(Q* qq)
{assert(qq);return qq->size == 0;
}void Destroy(Q* qq)
{assert(qq);free(qq->head);free(qq->tail);free(qq);
}int Size(Q* qq)
{assert(qq);return qq->size;
}

 

 

http://www.hkea.cn/news/119559/

相关文章:

  • 常州网站建设案例网站制作建设公司
  • 外国人做家具的网站一站传媒seo优化
  • 佛山h5建站模板怎样优化网站
  • 第三方做公司网站谷歌搜索广告优化
  • 网站风格模板快速排名精灵
  • 做网站横幅 的网站推荐几个公司推广
  • html5国内网站建设客户管理软件
  • 网站建设报价单站长工具 seo查询
  • 日本电商网站贵州快速整站优化
  • 物业服务网站建设建立网站要多少钱一年
  • 中铁建设门户加长版廊坊百度提升优化
  • 最便宜的外贸网站建设电商平台运营方案
  • 做网站应该会什么问题网络营销软文范例500字
  • 摄影网课百度关键词优化查询
  • 打广告型的营销网站西安百度推广外包
  • 乌鲁木齐招聘网站建设一站式网络营销
  • 中小型网站建设服务淘宝数据分析工具
  • 梧州网站设计企业网站模板建站
  • 行政事业单位网站建设建议营销策划公司
  • 网络推广网站怎么做百度联盟广告点击一次收益
  • wordpress居中样式宁波seo网络推广外包报价
  • java做网站用到哪些技术网络营销的重要性与意义
  • 网络营销推广的作用谷歌seo什么意思
  • 免费网站建设解决方案郑州网络营销公司哪个好
  • 转转怎么做钓鱼网站税收大数据
  • 株洲专业网站排名优化深圳产品网络推广
  • 深圳美食教学网站制作如何免费搭建自己的网站
  • 兰州移动端网站建设广东整治互联网霸王条款
  • 彩票网站该怎么建设天津seo实战培训
  • 原平的旅游网站怎么做的新冠疫情最新情况最新消息