首页 > 代码库 > find out the neighbouring max D_value by counting sort in stack

find out the neighbouring max D_value by counting sort in stack

  1 #include <stdio.h>
  2 #include <malloc.h>
  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 =http://www.mamicode.com/ 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<MAX_STACK; ++i) {
 72         scanf("%d",&val);
 73         push(s,val); 
 74     } 
 75 }
 76 
 77 bool deleteStack(NewStack s) {
 78     Snode clear = (Snode) malloc (sizeof(node));
 79     while(s->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 array
123     int arr[num];
124     int i = 0; 
125     int j = 0;
126     
127     // to put the elements into the new counting sort array
128     for(; j<num; ++j) {
129         arr[j] = -1;
130     }
131     while(top != bottom) {
132         arr[top->data - min] = top->data;
133         top = top->next; 
134     }
135 
136     // to find out the max zone where there are max number of -1
137     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.

 

find out the neighbouring max D_value by counting sort in stack