#include <stdio.h>
#include <malloc.h>
#define MAX_STACK 10 int COUNT = ;
// define the node of stack
typedef struct node {
int data;
node *next;
}*Snode; // define the stack
typedef struct Stack{
Snode top;
Snode bottom;
}*NewStack; void creatStack(NewStack s) {
// allocate stack
// allocate the elements of stack s->top = (Snode) malloc(sizeof(node));
s->bottom = s->top;
s->top->next = NULL;
} bool push(NewStack s,int val) {
//Snode top = s->top;
// Snode bottom = s->bottom;
if(COUNT == MAX_STACK) {
printf("StackOverFlowError\n");
return false;
}
Snode newNode = (Snode) malloc(sizeof(node));
newNode->data = val; // push the new node into the stack
newNode->next = s->top;
s->top = newNode;
COUNT ++;
//printf("%d \n",s->top->data);
return true;
} int pop(NewStack s) {
if(s->top == s->bottom) {
printf("this is bottom of stack!\n");
return NULL;
}
int val = s->top->data;
Snode tempNode = s->top;
s->top = s->top->next;
free(tempNode);
return val;
} void printStack(NewStack s){
Snode priNode = (Snode) malloc (sizeof(node));
priNode = s->top;
printf("now it is the show time of ur stack:\n");
while(priNode != s->bottom) {
printf("%d \t",priNode->data);
priNode = priNode->next;
}
} void scanfStack(NewStack s) {
printf("now u can write down the elements u cant push:\n");
int i = ;
int val;
for(i = ; i<MAX_STACK; ++i) {
scanf("%d",&val);
push(s,val);
}
} bool deleteStack(NewStack s) {
Snode clear = (Snode) malloc (sizeof(node));
while(s->top != s->bottom) {
clear = s->top;
s->top = s->top->next;
free(clear);
} return true;
} int getMax(NewStack s) {
Snode top = s->top;
Snode bottom = s->bottom;
int max = top->data;
while(top != bottom) {
if(top->data > max) {
max = top->data;
}
top = top->next;
} return max;
} int getMin(NewStack s) {
Snode top = s->top;
Snode bottom = s->bottom;
int Min = top->data;
while(top != bottom) {
if(top->data < Min) {
Min = top->data;
}
top = top->next;
} return Min;
} // using the counting sort -- is not appropriate for the neibouring numbers where there are big difference.
int getMaxNeighD_value(NewStack s){
Snode top = s->top;
Snode bottom = s->bottom;
int max = getMax(s);
int min = getMin(s);
int num = max-min+; // get the length of the new counting sort array
int arr[num];
int i = ;
int j = ; // to put the elements into the new counting sort array
for(; j<num; ++j) {
arr[j] = -;
}
while(top != bottom) {
arr[top->data - min] = top->data;
top = top->next;
} // to find out the max zone where there are max number of -1
int Max_count = ;
int count = ;
for(;i < num; ++i) {
//printf("%d:%d\n",i,arr[i]);
if(arr[i] == -) {
count ++;
}else {
if(count > Max_count) {
Max_count = count;
}
count = ;
}
//printf("%d\n",Max_count+1);
} return Max_count+;
} int main() {
NewStack s = (NewStack) malloc(sizeof(Stack));
creatStack(s);
// push(s,5);
// push(s,6);
// push(s,4);
// push(s,7);
// push(s,10);
// push(s,9);
// push(s,3);
// push(s,56);
// push(s,88);
// push(s,44);
// push(s,66);
scanfStack(s);
printStack(s);
//printf("%d \t",pop(s));
int max = getMax(s);
int min = getMin(s);
printf("%d %d\n",max,min);
int c = getMaxNeighD_value(s);
printf("the max neighbouring D_value is : %d \n",c);
//deleteStack(s);
//pop(s);
}

illustration : counting sort is not appropriate for the array where there are too big difference such as {1, 2, 100000} , then i will report the

      new method of sort called bucket sort to solve the problem.

find out the neighbouring max D_value by counting sort in stack的更多相关文章

  1. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  2. 《算法导论》——计数排序Counting Sort

    今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...

  3. HDU 1718 Rank counting sort解法

    本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的.奇怪的推断系统. 由于分数值的范围是0到100,很小,而student 号码又很大, ...

  4. 41. First Missing Positive(困难, 用到 counting sort 方法)

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  5. [Algorithms] Counting Sort

    Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...

  6. 【HackerRank】 The Full Counting Sort

    In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...

  7. 排序算法六:计数排序(Counting sort)

    前面介绍的几种排序算法,都是基于不同位置的元素比较,算法平均时间复杂度理论最好值是θ(nlgn). 今天介绍一种新的排序算法,计数排序(Counting sort),计数排序是一个非基于比较的线性时间 ...

  8. [MIT6.006] 7. Counting Sort, Radix Sort, Lower Bounds for Sorting 基数排序,基数排序,排序下界

    在前6节课讲的排序方法(冒泡排序,归并排序,选择排序,插入排序,快速排序,堆排序,二分搜索树排序和AVL排序)都是属于对比模型(Comparison Model).对比模型的特点如下: 所有输入ite ...

  9. 【算法】计数排序(Counting Sort)(八)

    计数排序(Counting Sort) 计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中. 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范 ...

随机推荐

  1. HTML5存储之 indexedDB

    IndexeDB是HTML5 重要的一部分,它是一种轻量级的NOSQL数据库.对创建具有丰富本地存储数据的数据密集型的离线HTML5 Web 应用程序很有用. IndexedDB是为了能够在客户端存储 ...

  2. Python自动化之模板继承和cookie

    request请求头信息 type(request) //查看类 from django.core.handlers.wsgi import WSGIRequest 结果会以字典的形式存在 reque ...

  3. 点击空白处 div隐藏掉了

    $(document).on('click',function (e) { var target = $(e.target); if(target.closest(".login-box&q ...

  4. 进程管理supervisor的简单说明

    背景: 项目中遇到有些脚本需要通过后台进程运行,保证不被异常中断,之前都是通过nohup.&.screen来实现,带着能否做一个start/stop/restart/reload的服务启动的想 ...

  5. [iOS] 为文本加上横线方法

    _oldPriceLabel.text = "; _oldPriceLabel.textColor = [UIColor lightGrayColor]; NSMutableAttribut ...

  6. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  7. cacti 安装

    cacti:是常用的一个监控软件(开源,免费) 特点:重图形,有数据历史,需要用到数据库的支持,支持web配置,默认不支持告警,可以加插件 cacti安装 1.安装扩展源epel (nagios 和z ...

  8. LocalDB 静默安装

    cmd命令:msiexec /i SqlLocalDB.msi /qn IACCEPTSQLLOCALDBLICENSETERMS=YES 注意:需要以管理员身份运行

  9. angularjs $broadcast $emit $on 事件触发controller间的值传递

    如何在作用域之间通信呢? 1.创建一个单例服务,然后通过这个服务处理所有子作用域的通信. 2.通过作用域中的事件处理通信.但是这种方法有一些限制:例如,你并不能广泛的将事件传播到所有监控的作用域中.你 ...

  10. Servlet引擎Jetty之入门1

    Jetty与tomcat一样,HttpWeb容器,支持实现Servlet规范. 详细介绍参考:https://www.ibm.com/developerworks/cn/java/j-lo-jetty ...