问题描述:
建立一个固定长度的队列,每次插入一个数据的时候,踢掉最早进入数组的数据,每次插入一个数据的时候,对这个队列求中位数。
main.cpp
#include <stdio.h>
#include "SortedLinkedList.h"
int main(int argc, char *argv[])
{
init();
float input;
printf("-----INPUT STREAM IN VALUE-----\n");
printf("input=");
while(scanf("%f", &input) != EOF){
insertIntoList(input);
timeListToString();
sortedListToString();
printf("midValue=%1.0f\n",getMidValue());
printf("-------------------------------\n");
printf("input=");
}
printf("---------END OF ENPUT ---------\n");
/*
printf("%1.0f", input);
init();
insertIntoList(10.0);
insertIntoList(9.0);
insertIntoList(8.0);
insertIntoList(7.0);
insertIntoList(6.0);
insertIntoList(5.0);
insertIntoList(4.0);
insertIntoList(3.0);
insertIntoList(2.0);
insertIntoList(1.0);
insertIntoList(111.0);
insertIntoList(222.0);
insertIntoList(123.0);
insertIntoList(999.0);
insertIntoList(7.0);
insertIntoList(6.0);
insertIntoList(5.0);
insertIntoList(4.0);
insertIntoList(3.0);
insertIntoList(2.0);
insertIntoList(1.0);
timeListToString();
sortedListToString();
printf("midValue=%1.0f\n",getMidValue());*/
getchar();
return 0;
}
SortedLinkedList.h
#include <cstdlib>
//#include <iostream>
#define CAPACITY 10
#define BOOL int
#define TRUE 1
#define FALSE 0
typedef struct Element
{
Element *next;
float value;
}Element;
void init();
void insertIntoList(float insertValue);
void timeListToString();
void sortedListToString();
float getMidValue();
SortedLinkedList.cpp
#include "SortedLinkedList.h"
#include <cstdio>
int first, last, length;
Element list[CAPACITY];
Element header = {NULL, -10000};//to insure that header->value is set to the minimize value for actural requirement
Element *preToDeleteElement, *preToInsertElement;
float midValue;
void init(){
first = 0;
last = -1;
length = 0;
preToDeleteElement = preToInsertElement = NULL;
midValue = -1;
}
Element* insertIntoTimeList(float insertValue)
{
if(length >= CAPACITY){
printf("The list is full for inserting!\n");
return NULL;
}
if((++last) >= CAPACITY)
{
last = last - CAPACITY;
}
list[last].value = insertValue;
length++;
return &list[last];
}
void deleteFromTimeList(){
if(length == 0){
printf("The list is empty for deleting!\n");
return;
}
if((++first) >= CAPACITY){
first = first - CAPACITY;
}
length--;
}
void deleteFromSortedList(){
Element *next = preToDeleteElement -> next;
preToDeleteElement -> next = next -> next;
}
void insertIntoSortedList(Element* insertValue){
Element *next = preToInsertElement -> next;
preToInsertElement -> next = insertValue;
insertValue -> next = next;
}
void getToInsertPosInSortedList(float insertValue){
Element *pre = &header;
Element *e = pre->next;
preToInsertElement = NULL;
preToDeleteElement = NULL;
int mid;
if(length >= CAPACITY){
mid = CAPACITY/2 + 1;
}
else{
mid = (length+1)/2 + 1;
}
int index = 1;
midValue = -1;
if(e == NULL){
preToInsertElement = &header;
midValue = insertValue;
return;
}
while(e!=NULL){
printf("index = %d\n", index);
if(index == mid){
midValue = e->value;
}
if(length >= CAPACITY && e == &list[first]){
preToDeleteElement = pre;
index--;
}
if(preToInsertElement == NULL &&insertValue <= e->value && insertValue >= pre->value){
preToInsertElement = pre;
if(index == mid){
midValue = insertValue;
}
index++;
if(index == mid){
midValue = e->value;
}
}
pre = e;
e = e -> next;
index++;
}
if(pre->value <= insertValue){
preToInsertElement = pre;
if(length == 1){
midValue = insertValue;
}
}
}
void insertIntoList(float insertValue){
getToInsertPosInSortedList(insertValue);
if(preToInsertElement == NULL || preToDeleteElement == NULL){
int atBreak = 1;
}
if(length >= CAPACITY){
if( preToInsertElement == preToDeleteElement->next){
Element *toDeleteElement = preToDeleteElement->next;
toDeleteElement->value = insertValue;
if((++first)>=CAPACITY) first = first - CAPACITY;
if((++last)>=CAPACITY) last = last- CAPACITY;
return;
}
deleteFromSortedList();
deleteFromTimeList();
}
Element* insertElement = insertIntoTimeList(insertValue);
insertIntoSortedList(insertElement);
}
void timeListToString()
{
if( length == 0){
printf("Time list is empty\n");
return;
}
int i,j;
for( i = first, j = length; j > 0 ; j--, i++){
if(i >= CAPACITY){
i -= CAPACITY;
}
printf("time[%d] = %1.1f\n", i, list[i].value);
}
}
void sortedListToString()
{
Element *e = header.next;
if( e == NULL){
printf("Sorted list is empty\n");
return;
}
while(e){
printf("sorted = %1.1f\n", e -> value);
e = e -> next;
}
}
float getMidValue(){
return midValue;
}