数据结构-链式队列的基本操作-创新互联


//队列的基本操作

公司主营业务:网站建设、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出霍州免费做网站回馈大家。

#include

using namespace std;

#define datatype int

#define Status int

#define OK     1

#define ERROR  0

typedef struct linkqueuenode{           //定义队列节点

      datatype data;

      struct linkqueuenode *next;              

}LinkQueueNode;

typedef struct {                        //定义链式队列  

      LinkQueueNode *front;

      LinkQueueNode *rear;      

}LinkQueue;

//初始化

void InitQueue(LinkQueue *q)

{

q->front=NULL;

q->rear=NULL;

}

//入队

Status InQueue(LinkQueue *q,datatype x)

{

//创建节点

LinkQueueNode *s=new LinkQueueNode;

s->data=x;

s->next=NULL;

//是否队空

if(q->front==NULL)

{

s->next=q->rear;

q->front=s;

q->rear=s;

}

else

{

q->rear->next=s;

q->rear=s;

}

return 0;

}

//出队

Status OutQueue(LinkQueue *q,datatype &x)

{//若队空,返回0;出队完成,返回1

if(NULL==q->front) return 0;

LinkQueueNode *p=q->front;

x=p->data;

q->front=p->next;

delete p;

return 1;

}

//显示队列元素

Status ShowQueue(LinkQueue *q)

{

if(q->front==NULL) {cout<<"栈空!"<

LinkQueueNode *p;

p=q->front;

cout<<"遍历队列: ";

while(p!=NULL)

{

printf("%d ",p->data);

p=p->next;

}

cout<

return 1;

}

//读队首元素

Status ReadQueue(LinkQueue *q)

{

if(NULL==q->front) return 0;

cout<<"队首元素为:"<front->data<

return 1;

}

//双队列,队首出栈

Status OutQueueFront(LinkQueue *q,datatype &x)

{//若队空,返回0;出队完成,返回1

if(NULL==q->front) return 0;

LinkQueueNode *p=q->front;

x=p->data;

q->front=p->next;

delete p;

return 1;

}

//双队列,队尾出栈

Status OutQueueRear(LinkQueue *q,datatype &x)

{

//对空,返回0

if(NULL==q->front) return 0;

//找q.rear的前驱

LinkQueueNode *ptr;

ptr=q->rear;//ptr指向对尾

LinkQueueNode *p=q->front;

while(p->next!=q->rear)//p指向q->rear的前驱

{

p=p->next;

}

x=q->rear->data;

p->next=q->rear->next;

q->rear->next=p->next;

q->rear=p;

delete ptr;

return 1;

}

int main()

{

  LinkQueue que;//创建队列

  InitQueue(&que);//初始化队列

InQueue(&que,1);//入队

InQueue(&que,2);

InQueue(&que,3);

InQueue(&que,4);

ShowQueue(&que);//1,2,3,4

datatype temp;//用于保存出队的data

OutQueue(&que,temp);//2,3,4

ShowQueue(&que);//显示队列元素

ReadQueue(&que);

OutQueueRear(&que,temp);//2,3

ShowQueue(&que);

  system("pause");

  return 0;  

}

-----------------------------------------------------------

运行结果:

遍历队列: 1 2 3 4

出队!:

遍历队列: 2 3 4

队首元素为:2

队尾出队!

遍历队列: 2 3

Press any key to continue . . .

鲜少伟

2016-4-18

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


新闻名称:数据结构-链式队列的基本操作-创新互联
URL地址:http://myzitong.com/article/digjjh.html