| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1907 人关注过本帖
标题:队列的简单实现
只看楼主 加入收藏
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
结帖率:95.65%
收藏
 问题点数:0 回复次数:4 
队列的简单实现
程序代码:
//静态数组实现:
#define QueueType int
void
Insert( QueueType value );//将一个值插入队列
void
Delete( void );//将一个值从队列中删除,但不弹出
QueueType
First( void );//将一个值弹出队列,但不删除
int
is_empty( void );//空队列检查
int
is_full( void );//满队列检查



程序代码:
#include "Quen.h"
#include <stdio.h>
#include <assert.h>
#define QUEUE_MAX_SIZE 100
#define ARRAY_MAX_SIZE ( QUEUE_MAX_SIZE + 1 )

static QueueType QueueArray[ ARRAY_MAX_SIZE ];
static int Frnot = 1;
static int Rear = 0;

void
Insert( QueueType value )
{
    if( is_full() )
    {
        printf( "队列已满" );
        return;
    }
    else
    {
        Rear = ( Rear + 1 ) % ARRAY_MAX_SIZE;
        QueueArray[ Rear ] = value;
    }
}

void
Delete( void )
{
    assert( !is_empty() );    
    Frnot = ( Frnot + 1 ) % ARRAY_MAX_SIZE;
}

QueueType
First( void )
{
    assert( !is_empty() );
    return  QueueArray[ Frnot ];

}

int
is_empty( void )
{
    return ( Rear + 1 ) % ARRAY_MAX_SIZE == Frnot;//空队列返回1,否则返回0
}
int
is_full( void )
{
    return ( Rear + 2 ) % ARRAY_MAX_SIZE == Frnot; 
}


程序代码:
//动态数组实现:
#define QueueType int
void
CreateQueue( void );
void
DestroyQueue( void );
void
Insert( QueueType value );
void
Delete( void );
QueueType
First( void );
int
IsEmpty( void );
int
IsFull( void );


程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "MallocQueue.h"
#define QUEUESIZE 124

static QueueType *Queue;
static int CurrentSize = QUEUESIZE + 1;
static int Read = 0;
static int Frnot = 1;

void
Insert( QueueType value )
{
    if( IsFull() || NULL == Queue )
    {
        printf( "队列已满,插入队列失败\n" );
        return;
    }

    Read = ( Read + 1 ) % CurrentSize;
    Queue[ Read ] = value;
}


void
Delete( void )
{
    if( !IsEmpty() && NULL != Queue )
        Frnot = ( Frnot + 1 ) % CurrentSize;
}


QueueType
First( void )
{
    assert( !IsEmpty() );
     assert( NULL != Queue );
     return Queue[ Frnot ];
}


int
IsFull( void )
{
    int i;
    QueueType *Temp;

    i = ( Read + 2 ) % CurrentSize;

    if( NULL == Queue )
    {
        printf( "队列不存在\n" );
        return 1;
    }

    if( i == Frnot )
    {
        CurrentSize += QUEUESIZE;
        Temp = ( QueueType * )realloc( Queue, CurrentSize * sizeof( QueueType ) );
        if( NULL == Temp )
        {
            CurrentSize -= QUEUESIZE;
            return 1;
        }
        Queue = Temp;
        return 0;
    }

    return 0;
}

int
IsEmpty( void )
{
    return ( Read + 1) % CurrentSize == Frnot;
}


void
CreateQueue( void )
{
    if( NULL == Queue )
    {
        Queue = ( QueueType * )malloc( CurrentSize * sizeof( QueueType ) );
        if( NULL == Queue )
            printf( "创建队列失败\n" );
    }
    else
    {
        printf( "队列已存在,创建失败\n" );
        return;
    }
}


void
DestroyQueue( void )
{
    if( NULL != Queue )
    {
        free( Queue );
        Queue = NULL;
        CurrentSize = QUEUESIZE + 1;
        Read = 0;
        Frnot = 1;
    }
    else
    {
        printf( "队列不存在,销毁失败\n" );
        return;
    }
}


程序代码:
//链表实现
#define QueueType int
void
CreateQueue( void );
void
DestroyQueue( void );
void
Insert( QueueType value );
void
Delete( void );
QueueType
First( void );
int
IsFull( void );
int
IsEmpty( void );


程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "ListQueue.h"

struct Node{
    QueueType Value;
    struct Node *Next;
};

static struct Node *Queue;

void
CreateQueue( void )
{
}


void
DestroyQueue( void )
{
    struct Node *Temp;
    struct Node *Next;

    if( NULL == Queue )
        return;

    Next = Queue;
    Queue = NULL;

    while( NULL != Next )
    {
        Temp = Next;
        Next = Next->Next;
        free( Temp );
    }
}

int
IsFull( void )
{
    return Queue == NULL;
}

int
IsEmpty( void )
{
    return Queue == NULL;
}

void
Insert( QueueType value )
{
    struct Node *Next;
    struct Node **Temp;
    struct Node *NewCell;

    Temp = &Queue;
    
    for( Next = *Temp; NULL != Next; Next = *Temp )
        Temp = &Next->Next;

    NewCell = ( struct Node * )malloc( sizeof( struct Node ) );
    if( NULL == NewCell )
    {
        printf( "插入队列失败\n" );
        return;
    }

    NewCell->Value = value;
     *Temp = NewCell;
    NewCell->Next = NULL;

}

QueueType
First( void )
{
    assert( !IsEmpty() );
    return Queue->Value;
}


void
Delete( void )
{
    struct Node *Temp;

    assert( !IsEmpty() );

    Temp = Queue;
    Queue = Queue->Next;
    free( Temp );

}
2017-03-28 15:11
sharplong
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:121
注 册:2017-3-27
收藏
得分:0 
支持一个

跟据科学研究呢,拥有一个良好的头像呢,有助于提高帖子关注度,和被友好对待的可能性:)准确来说呢,其实,我是一个演员....和兼职汽车维修员
2017-03-30 14:35
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
估摸着  windows的核心的核心  链表得占很大一部分

DO IT YOURSELF !
2017-03-30 14:36
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 3楼 wp231957
要不……找份linux的源代码来研究研究。
看看是什么管理内存的。

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-03-30 14:40
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 2楼 sharplong
谢谢支持。

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-03-30 14:45
快速回复:队列的简单实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026235 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved