您的位置:首页 > 其它

windows 命令记录

2012-02-25 22:21 155 查看
以下是我用c语言实现数据结构中的队列、
#pragma once

#ifndef _ELEMTYPE_H
#define _ELEMTYPE_H

typedef int Elemtype;

#endif

#ifndef _FUNCTIONPOINT_H
#define _FUNCTIONPOINT_H

typedef void(* Function)(Elemtype e);

#endif

#ifndef _STDLIB_H
#define _STDLIB_H

#include <stdlib.h>

#endif

#ifndef _QUEUE_H
#define _QUEUE_H

typedef struct Node
{
Elemtype Data;
struct Node * p_next;
}Node,* QueuePtr;

typedef struct
{
QueuePtr p_front;
QueuePtr p_rear;
}Queue;

#endif
#include "stdafx.h"

bool InitQueue(Queue &Q);

bool DestoryQueue(Queue &Q);

bool ClearQueue(Queue &Q);

bool QueueEmpty(const Queue &Q);

int QueueLength(const Queue &Q);

bool GetHead(const Queue &Q,Elemtype &e);

bool EnterQueue(Queue &Q,Elemtype e);

bool QuitQueue(Queue &Q,Elemtype &e);

bool QueueTraverse(Queue &Q,Function CallBack);
#include "Queue.h"

bool InitQueue(Queue &Q)
{
Q.p_front=(QueuePtr)malloc(sizeof(Node));
if(Q.p_front==NULL)
return false;
Q.p_rear=Q.p_front;
Q.p_front->p_next=NULL;
return true;
}

bool DestoryQueue(Queue &Q)
{
while (Q.p_front->p_next!=NULL)
{
Q.p_rear=Q.p_front->p_next;
free(Q.p_front);
Q.p_front=Q.p_rear;
}
free(Q.p_front);
return true;
}

bool ClearQueue(Queue &Q)
{
if(Q.p_front==Q.p_rear)
return false;
else
{
QueuePtr temp=Q.p_front->p_next;
while(temp->p_next!=NULL)
{
Q.p_rear=temp->p_next;
free(temp);
temp=Q.p_rear;
}
free(temp);
Q.p_rear=Q.p_front;
return true;
}
}

bool QueueEmpty(const Queue &Q)
{
if(Q.p_front==Q.p_rear)
return true;
else
return false;
}

int QueueLength(const Queue &Q)
{
if(Q.p_front==Q.p_rear)
return 0;
else
{
int Length=1;
QueuePtr temp=Q.p_front->p_next;
while(temp->p_next!=NULL)
{
Length++;
temp=temp->p_next;
}
return Length;
}
}

bool GetHead(const Queue &Q,Elemtype &e)
{
if(Q.p_front==Q.p_rear)
{
e=NULL;
return false;
}
else
{
e=Q.p_front->p_next->Data;
return true;
}
}

bool EnterQueue(Queue &Q,Elemtype e)
{
QueuePtr temp=(QueuePtr)malloc(sizeof(Node));
temp->Data=e;
Q.p_rear->p_next=temp;
temp->p_next=NULL;
Q.p_rear=temp;
return true;
}

bool QuitQueue(Queue &Q,Elemtype &e)
{
if(Q.p_front==Q.p_rear)
{
e=NULL;
return false;
}
else
{
QueuePtr temp=Q.p_front->p_next;
e=temp->Data;
Q.p_front->p_next=temp->p_next;
if(temp==Q.p_rear)
Q.p_rear=Q.p_front;
free(temp);
return true;
}
}

bool QueueTraverse(Queue &Q,Function CallBack)
{
if(Q.p_front==Q.p_rear)
return false;
else
{
QueuePtr temp=Q.p_front->p_next;
if(temp==Q.p_rear)
{
CallBack(temp->Data);
return true;
}
else
{
while(temp->p_next!=NULL)
{
CallBack(temp->Data);
temp=temp->p_next;
}
CallBack(temp->Data);
return true;
}
}
}


本文出自 “胡思旺” 博客,请务必保留此出处http://siwanghu.blog.51cto.com/10630140/1685570
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: