数据结构之链队实现
代码及思想
#include < iostream>
#include < cstdio>
#include < cstdlib>
using namespace std;
typedef int ElemType;
#define Status int
#define ERROR 0
#define OK 1
typedef struct LNode
{
int data;
LNode * next;
} LNode;
typedef struct
{
LNode * front, * rear;
int length;
} LinkQueue;
Status LinkQueueInit ( LinkQueue & Q ) ;
bool IsEmpty ( LinkQueue & Q ) ;
Status enQueue ( LinkQueue & Q , ElemType x) ;
Status deQueue ( LinkQueue & Q , ElemType & x) ;
int LinkQueueLength ( LinkQueue & Q ) ;
void LinkQueueTraverse ( LinkQueue & Q ) ;
void LinkQueueDestroy ( LinkQueue & Q ) ;
Status LinkQueueInit ( LinkQueue & Q )
{
Q . front = Q . rear = ( LNode * ) malloc ( sizeof ( LNode) ) ;
Q . rear- > next = NULL ;
Q . length = 0 ;
}
bool IsEmpty ( LinkQueue & Q )
{
if ( Q . front == Q . rear)
return true ;
return false ;
}
Status enQueue ( LinkQueue & Q , ElemType x)
{
LNode * node = ( LNode * ) malloc ( sizeof ( LNode) ) ;
node- > data = x;
cout<< "#入队成功,入队结点数据为:" << x<< endl;
node- > next = NULL ;
Q . rear- > next = node;
Q . rear = node;
Q . length++ ;
return OK ;
}
Status deQueue ( LinkQueue & Q , ElemType & x)
{
if ( IsEmpty ( Q ) )
{
cout<< "#队列为空,出队失败" << endl;
return ERROR ;
}
LNode * node = Q . front- > next;
x = node- > data;
cout<< "#出队成功,出队结点数据为:" << x<< endl;
Q . front- > next = node- > next;
free ( node) ;
Q . length-- ;
return OK ;
}
int LinkQueueLength ( LinkQueue & Q )
{
return Q . length;
}
void LinkQueueTraverse ( LinkQueue & Q )
{
LNode * node = Q . front- > next;
while ( node!= NULL )
{
cout<< node- > data<< " " ;
node = node- > next;
}
cout<< endl;
}
void LinkQueueDestroy ( LinkQueue & Q )
{
if ( Q . front == Q . rear)
{
cout<< "#队列为空" << endl;
}
LNode * p = Q . front;
LNode * temp;
while ( p!= NULL )
{
temp = p;
p = p- > next;
free ( temp) ;
}
}
int main ( )
{
ElemType denum;
LinkQueue Q ;
LinkQueueInit ( Q ) ;
cout<< "*1、初始化队列数据为1-10" << endl;
for ( int i= 1 ; i< 11 ; i++ )
{
enQueue ( Q , i) ;
}
cout<< "#此时队列中数有:" << endl;
LinkQueueTraverse ( Q ) ;
cout<< "#此时队列长度为:" << LinkQueueLength ( Q ) << endl;
cout<< endl;
cout<< "*2、向队列中插入数据2019" << endl;
enQueue ( Q , 2019 ) ;
cout<< "#此时队列中数有:" << endl;
LinkQueueTraverse ( Q ) ;
cout<< "#此时队列长度为:" << LinkQueueLength ( Q ) << endl;
cout<< endl;
cout<< "*3、队列出队两次" << endl;
deQueue ( Q , denum) ;
deQueue ( Q , denum) ;
cout<< "#出队的第二个元素为:" << denum<< endl;
cout<< "#此时队列中数有:" << endl;
LinkQueueTraverse ( Q ) ;
cout<< "#此时队列长度为:" << LinkQueueLength ( Q ) << endl;
cout<< endl;
cout<< "*4、去向队列中插入数据111、222、333、444、,不会越界(链表无界)" << endl;
deQueue ( Q , denum) ; deQueue ( Q , denum) ;
enQueue ( Q , 111 ) ; enQueue ( Q , 222 ) ; enQueue ( Q , 333 ) ; enQueue ( Q , 444 ) ;
cout<< "#此时队列中数有:" << endl;
LinkQueueTraverse ( Q ) ;
cout<< "#此时队列长度为:" << LinkQueueLength ( Q ) << endl;
cout<< endl;
LinkQueueDestroy ( Q ) ;
return 0 ;
}
测试截图