您的位置:首页 > 其它

队列的链式存储---链表实现(有头结点)

2015-07-04 13:37 295 查看
/* 队列的链式存储 */
/* with header */
/* with no typedef */
struct Node{
ElementType Ele;
struct Node *Next;
};
struct LinQ{
struct Node *rear;
struct Node *front;
};
struct LinQ *
CreateQ( void )
{
struct LinQ *Ptr;
struct Node *header;

Ptr = malloc(sizeof(struct LinQ));
if(Ptr == NULL )
Error("out of space ");

header = malloc( sizeof( struct Node ) );
if(header == NULL )
error("out of space ");

header->Next = NULL;
Ptr->front = Ptr->rear = header;
}
void
AddQ( struct LinQ *PtrQ, ElementType X )
{
struct Node *Ptr;
Ptr = malloc(sizeof(struct Node ));
if(Ptr == NULL)
Error("out of space ");
Ptr->Ele = X;
Ptr->Next = NULL;
PtrQ->rear->Next = Ptr;
PtrQ->rear = Ptr;
}
int
IsEmpty( struct LinQ * PtrQ )
{
return PtrQ->front->Next == NULL;
}

ElementType
DeleteQ( struct LinQ * PtrQ )
{
struct Node * TmpCell;
if( IsEmpty( PtrQ ) )
Error("empty Queue");
Tmpcell = PtrQ->front->Next;
PtrQ->front->Next = TmpCell->Next;
free( TmpCell );
}


View Code
CreateQ旨在创造一个front和rear都指向头结点header的LinQ结构

头结点处Next设为NULL,在判断是否空队时可用

front一直指到头结点

新增结点的Next一定改为NULL

结论:可以用typedef的地方就用,可以用头结点的地方就用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: