码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构-线性表-队列

时间:2015-10-11 16:43:58      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

 

队列的特别实现

组合使用两个栈的后进先出可以实现队列的先进先出,简单高效,入队和出队的时间复杂度可以到 O(1)

SQueue.h

#ifndef _SQUEUE_H_
#define _SQUEUE_H_

typedef void SQueue;

SQueue* SQueue_Create();

void SQueue_Destroy(SQueue* queue);

void SQueue_Clear(SQueue* queue);

int SQueue_Append(SQueue* queue, void* item);

void* SQueue_Retrieve(SQueue* queue);

void* SQueue_Header(SQueue* queue);

int SQueue_Length(SQueue* queue);

#endif

SQueue.c

#include <stdio.h>
#include <malloc.h>
#include "LinkStack.h"
#include "SQueue.h"

typedef struct _tag_SQueue
{
    LinkStack* inStack;
    LinkStack* outStack;
} TSQueue;

SQueue* SQueue_Create() // O(1)
{
    TSQueue* ret = (TSQueue*)malloc(sizeof(TSQueue));
    
    if( ret != NULL )
    {
        ret->inStack = LinkStack_Create();
        ret->outStack = LinkStack_Create();
        
        if( (ret->inStack == NULL) || (ret->outStack == NULL) )
        {
            LinkStack_Destroy(ret->inStack);
            LinkStack_Destroy(ret->outStack);
            
            free(ret);
            
            ret = NULL;
        }
    }
    
    return ret;
}

void SQueue_Destroy(SQueue* queue) // O(n)
{
    SQueue_Clear(queue);
    free(queue);
}

void SQueue_Clear(SQueue* queue) // O(n)
{
    TSQueue* sQueue = (TSQueue*)queue;
    
    if( sQueue != NULL )
    {
        LinkStack_Clear(sQueue->inStack);
        LinkStack_Clear(sQueue->outStack);
    }
}

int SQueue_Append(SQueue* queue, void* item) // O(1)
{
    TSQueue* sQueue = (TSQueue*)queue;
    
    if( sQueue != NULL )
    {
        LinkStack_Push(sQueue->inStack, item);
    }
}

void* SQueue_Retrieve(SQueue* queue) // O(1)
{
    TSQueue* sQueue = (TSQueue*)queue;
    void* ret = NULL;
    
    if( sQueue != NULL )
    {
        if( LinkStack_Size(sQueue->outStack) == 0 )
        {
            while( LinkStack_Size(sQueue->inStack) > 0 )
            {
                LinkStack_Push(sQueue->outStack, LinkStack_Pop(sQueue->inStack));
            }
        }
        
        ret = LinkStack_Pop(sQueue->outStack);
    }
    
    return ret;
}

void* SQueue_Header(SQueue* queue) // O(1)
{
    TSQueue* sQueue = (TSQueue*)queue;
    void* ret = NULL;
    
    if( sQueue != NULL )
    {
        if( LinkStack_Size(sQueue->outStack) == 0 )
        {
            while( LinkStack_Size(sQueue->inStack) > 0 )
            {
                LinkStack_Push(sQueue->outStack, LinkStack_Pop(sQueue->inStack));
            }
        }
        
        ret = LinkStack_Top(sQueue->outStack);
    }
    
    return ret;
}

int SQueue_Length(SQueue* queue) // O(1)
{
    TSQueue* sQueue = (TSQueue*)queue;
    int ret = -1;
    
    if( sQueue != NULL )
    {
        ret = LinkStack_Size(sQueue->inStack) + LinkStack_Size(sQueue->outStack);
    }
    
    return ret;
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "SQueue.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) 
{
    SQueue* queue = SQueue_Create();
    int a[10] = {0};
    int i = 0;
    
    for(i=0; i<10; i++)
    {
        a[i] = i + 1;
        
        SQueue_Append(queue, a + i);
    }
    
    printf("Header: %d\n", *(int*)SQueue_Header(queue));
    printf("Length: %d\n", SQueue_Length(queue));
    
    for(i=0; i<5; i++)
    {
        printf("Retrieve: %d\n", *(int*)SQueue_Retrieve(queue));
    }
    
    printf("Header: %d\n", *(int*)SQueue_Header(queue));
    printf("Length: %d\n", SQueue_Length(queue));
    
    for(i=0; i<10; i++)
    {
        a[i] = i + 1;
        
        SQueue_Append(queue, a + i);
    }
    
    while( SQueue_Length(queue) > 0 )
    {
        printf("Retrieve: %d\n", *(int*)SQueue_Retrieve(queue));
    }
    
    SQueue_Destroy(queue);
    
    return 0;
}

 

数据结构-线性表-队列

标签:

原文地址:http://www.cnblogs.com/siqi/p/4869440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!