博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
find out the neighbouring max D_value by counting sort in stack
阅读量:4591 次
发布时间:2019-06-09

本文共 4498 字,大约阅读时间需要 14 分钟。

1 #include 
2 #include
3 #define MAX_STACK 10 4 5 int COUNT = 0; 6 // define the node of stack 7 typedef struct node { 8 int data; 9 node *next; 10 }*Snode; 11 12 // define the stack 13 typedef struct Stack{ 14 Snode top; 15 Snode bottom; 16 }*NewStack; 17 18 void creatStack(NewStack s) { 19 // allocate stack 20 // allocate the elements of stack 21 22 s->top = (Snode) malloc(sizeof(node)); 23 s->bottom = s->top; 24 s->top->next = NULL; 25 } 26 27 bool push(NewStack s,int val) { 28 //Snode top = s->top; 29 // Snode bottom = s->bottom; 30 if(COUNT == MAX_STACK) { 31 printf("StackOverFlowError\n"); 32 return false; 33 } 34 Snode newNode = (Snode) malloc(sizeof(node)); 35 newNode->data = val; 36 37 // push the new node into the stack 38 newNode->next = s->top; 39 s->top = newNode; 40 COUNT ++; 41 //printf("%d \n",s->top->data); 42 return true; 43 } 44 45 int pop(NewStack s) { 46 if(s->top == s->bottom) { 47 printf("this is bottom of stack!\n"); 48 return NULL; 49 } 50 int val = s->top->data; 51 Snode tempNode = s->top; 52 s->top = s->top->next; 53 free(tempNode); 54 return val; 55 } 56 57 void printStack(NewStack s){ 58 Snode priNode = (Snode) malloc (sizeof(node)); 59 priNode = s->top; 60 printf("now it is the show time of ur stack:\n"); 61 while(priNode != s->bottom) { 62 printf("%d \t",priNode->data); 63 priNode = priNode->next; 64 } 65 } 66 67 void scanfStack(NewStack s) { 68 printf("now u can write down the elements u cant push:\n"); 69 int i = 0; 70 int val; 71 for(i = 0; i
top != s->bottom) { 80 clear = s->top; 81 s->top = s->top->next; 82 free(clear); 83 } 84 85 return true; 86 } 87 88 int getMax(NewStack s) { 89 Snode top = s->top; 90 Snode bottom = s->bottom; 91 int max = top->data; 92 while(top != bottom) { 93 if(top->data > max) { 94 max = top->data; 95 } 96 top = top->next; 97 } 98 99 return max;100 }101 102 int getMin(NewStack s) {103 Snode top = s->top;104 Snode bottom = s->bottom;105 int Min = top->data;106 while(top != bottom) {107 if(top->data < Min) {108 Min = top->data;109 }110 top = top->next;111 }112 113 return Min;114 }115 116 // using the counting sort -- is not appropriate for the neibouring numbers where there are big difference.117 int getMaxNeighD_value(NewStack s){118 Snode top = s->top;119 Snode bottom = s->bottom;120 int max = getMax(s);121 int min = getMin(s);122 int num = max-min+1; // get the length of the new counting sort array123 int arr[num];124 int i = 0; 125 int j = 0;126 127 // to put the elements into the new counting sort array128 for(; j
data - min] = top->data;133 top = top->next; 134 }135 136 // to find out the max zone where there are max number of -1137 int Max_count = 0;138 int count = 0;139 for(;i < num; ++i) {140 //printf("%d:%d\n",i,arr[i]);141 if(arr[i] == -1) {142 count ++;143 }else {144 if(count > Max_count) {145 Max_count = count;146 }147 count = 0;148 }149 //printf("%d\n",Max_count+1);150 }151 152 return Max_count+1;153 }154 155 int main() {156 NewStack s = (NewStack) malloc(sizeof(Stack));157 creatStack(s); 158 // push(s,5);159 // push(s,6);160 // push(s,4);161 // push(s,7);162 // push(s,10);163 // push(s,9);164 // push(s,3);165 // push(s,56);166 // push(s,88);167 // push(s,44);168 // push(s,66);169 scanfStack(s);170 printStack(s);171 //printf("%d \t",pop(s));172 int max = getMax(s);173 int min = getMin(s);174 printf("%d %d\n",max,min);175 int c = getMaxNeighD_value(s);176 printf("the max neighbouring D_value is : %d \n",c);177 //deleteStack(s);178 //pop(s);179 }

 

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.

 

转载于:https://www.cnblogs.com/AmoryWang-JavaSunny/p/6193846.html

你可能感兴趣的文章
一个开源的MY BLOG
查看>>
ko.js学习一
查看>>
java学习基础部分
查看>>
C++第二篇--访问控制
查看>>
关于傅里叶分析与香农采样定理
查看>>
mysql-5.6.17编译安装和常见问题
查看>>
二维数组中的查找
查看>>
css中可以和不可以继承的属性
查看>>
Fiddler抓包使用教程-断点调试
查看>>
Linux 进程后台运行的几种方式 screen
查看>>
发送邮件
查看>>
Eclipse Svn 取消某些文件或文件夹的版本控制
查看>>
【模板归纳】网络流及费用流
查看>>
iOS --------Crash 分析(一)
查看>>
冲刺二阶段-个人总结05
查看>>
开源的android客户端,ghost网站
查看>>
《ISCSI集中存储》RHEL6——CE
查看>>
V4L2测试时出现Segmentation fault
查看>>
java基础---->java输入输出流
查看>>
[Apple开发者帐户帮助]九、参考(3)支持的功能(iOS)
查看>>