#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. float-position的一些细节

      一 综述: float position 对于div布局的作用明显, 注意使用的细节也变得有必要了.  float position 有相同的地方,都会脱离"文档流"(posi ...

  2. 一些LINQ的使用

    var list = from staff in staffList from extraRecord in extraList where staff.staffID == extraRecord. ...

  3. html5 第一天

    html4与html5的琐碎比较,不全,第一次写,望多多包涵. 一 兼容性:html5在老版本的浏览器上也可以运行 二 实用性:HYML5都是封装的简单使用功能 三非革命性的发展 Html5向前兼容, ...

  4. wxPython 自动提示文本框

    1.原版和例子都在这里 在浏览器的地址栏,或者在百度.google 输入文字的时候,输入框的下面会把有关的项目都提示出来. wxPython 没有提供类似的控件,google 了一下,发现了一个,很好 ...

  5. Dom元素的操作

    getElementById(): 获取有指定惟一ID属性值文档中的元素 getElementsByName(name): 返回的是数组 getElementsByTagName(): 返回具有指定标 ...

  6. JS判断字符串长度(中文长度为2,英文长度为1)

    目的:计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: String.prototype.gblen = function() { var len = 0; for (var i=0; ...

  7. ComboBox数据的绑定

    //带有ComboBox控件点Load事件 private void Library_Load(object sender, EventArgs e) { //创建连接字符串 string strCo ...

  8. javascript 性能惰性加载2016.12.13

    利用函数的惰性载入提高 javascript 代码性能 原文:利用函数的惰性载入提高javascript代码性能 作者:阿安 在 javascript 代码中,因为各浏览器之间的行为的差异,我们经常会 ...

  9. kali python pip3 的安装和卸载

    今天很高兴安装完成调整了kali 然后看见kali已经帮助我安装了python2.7和python3.5可把我开心坏了,可是2.7有pip,而且包很全,但2.7与3.0切换使用我的就尴尬了 最后在su ...

  10. java动态调用webservice

    cxf方式 public static Object[] invokeRemoteMethod(String url, String method, Object[] parameters) { Ja ...