find out the neighbouring max D_value by counting sort in stack

时间:2022-01-02 01:12:48
 #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("*Error\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.