SortedLinkedList

本文介绍了一种实现动态队列的方法,每次插入元素时,自动更新队列并实时计算中位数。通过使用有序链接列表,确保了在保持队列动态特性的同时,能够快速获取中位数,适用于实时数据处理和分析场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

建立一个固定长度的队列,每次插入一个数据的时候,踢掉最早进入数组的数据,每次插入一个数据的时候,对这个队列求中位数。


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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值