赞
踩
#include<iostream>
#include<cstdlib>
#define MAXSIZE 100 //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType; // 自己定义的类型
typedef int status; // 上同
using namespace std;
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*Queueptr;
//嵌套定义
typedef struct
{
Queueptr front; //Queueptr类型的头指针
Queueptr rear; //Queueptr类型的尾指针
}LinkQueue;//也就是说Queueptr类型的头指针里面包括数据域和next域
status ENQueue(LinkQueue *Q,QElemType e)
{
QNode*p;
p=new QNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}
status DeQueue(LinkQueue *Q,QElemType *e)
{
QNode*p;
if(Q->front==Q->rear)
return ERROR;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
return OK;
}
status GetHead(LinkQueue *Q)
{
if(Q->front==Q->rear)
{
return ERROR;
}
else
{
return Q->front->next->data;
}
}
status printQueue(LinkQueue *Q)
{
cout<<"当前队列:";
Queueptr p=Q->front->next;
while(p)
{
cout<<'\40'<<p->data ;
p=p->next;
}
cout<<endl;
return OK;
}
status QueueLength(LinkQueue *Q)
{
int Length=0;
if(Q->front==Q->rear)
{
return 0;
}
Queueptr p=Q->front->next;
while(p)
{
p=p->next;
Length++;
}
return Length;
}
int main()
{
int n,m,t;
QElemType e;
LinkQueue Q;
InitQueue(&Q);
printf("请输入需要入队的次数:");
scanf("%d",&n);
for(int i=1 ;i<=n;i++)
{
printf("请输入第%d个元素的值:",i);
scanf("%d",&m);
ENQueue(&Q,m);
}
printQueue(&Q);
printf("当前对头元素为:%d\n",GetHead(&Q));
printf("当前链队长度为:%d\n",QueueLength(&Q));
printf("\n");
printf("\n");
printf("请输入需要出队的次数:");
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
DeQueue(&Q,&e);
}
printQueue(&Q);
printf("当前对头元素为:%d\n",GetHead(&Q));
printf("当前链队长度为:%d\n",QueueLength(&Q));
return 0;
}
int main()
{
int n,m,t;
QElemType e;
LinkQueue Q;
InitQueue(&Q);
cout<<"请输入需要入队的次数:";
cin>>n;
for(int i=1 ;i<=n;i++)
{
cout<<"请输入第"<<i<<"个元素的值:";
cin>>m;
ENQueue(&Q,m);
}
printQueue(&Q);
cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
cout<<"当前链队长度为:"<<QueueLength(&Q);
cout<<endl<<endl;
cout<<"请输入需要出队的次数:";
cin>>t;
for(int i=1;i<=t;i++)
{
DeQueue(&Q,&e);
}
printQueue(&Q);
cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
cout<<"当前链队长度为:"<<QueueLength(&Q);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100 //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType; // 自己定义的类型
typedef int status; // 上同
//链队列定义
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*Queueptr;
//嵌套定义
typedef struct
{
Queueptr front; //Queueptr类型的头指针
Queueptr rear; //Queueptr类型的尾指针
}LinkQueue;//也就是说Queueptr类型的头指针里面包括数据域和next域
//链队列的初始化
status InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=(QNode*)malloc(sizeof(QNode)); //为头指针和尾指针开辟动态空间
Q->front->next=NULL; //初始头指针的next为空
return OK;
}
//链队列的入队操作
status ENQueue(LinkQueue *Q,QElemType e)
{
QNode*p;
p=new QNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}
//链队列的出队操作
status DeQueue(LinkQueue *Q,QElemType *e)
{
QNode*p;
if(Q->front==Q->rear)
return ERROR;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
return OK;
}
//取链队的队头元素
status GetHead(LinkQueue *Q)
{
if(Q->front==Q->rear)
{
return ERROR;
}
else
{
return Q->front->next->data;
}
}
//输出队列的元素
status printQueue(LinkQueue *Q)
{
printf("当前队列:");
Queueptr p=Q->front->next;
while(p)
{
printf("%4d",p->data);
p=p->next;
}
printf("\n");
return OK;
}
//求链队的长度
status QueueLength(LinkQueue *Q)
{
int Length=0;
if(Q->front==Q->rear)
{
return 0;
}
Queueptr p=Q->front->next;
while(p)
{
p=p->next;
Length++;
}
return Length;
}
int main()
{
int n,m,t;
QElemType e;
LinkQueue Q;
InitQueue(&Q);
printf("请输入需要入队的次数:");
scanf("%d",&n);
for(int i=1 ;i<=n;i++)
{
printf("请输入第%d个元素的值:",i);
scanf("%d",&m);
ENQueue(&Q,m);
}
printQueue(&Q);
printf("当前对头元素为:%d\n",GetHead(&Q));
printf("当前链队长度为:%d\n",QueueLength(&Q));
printf("\n");
printf("\n");
printf("请输入需要出队的次数:");
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
DeQueue(&Q,&e);
}
printQueue(&Q);
printf("当前对头元素为:%d\n",GetHead(&Q));
printf("当前链队长度为:%d\n",QueueLength(&Q));
return 0;
}
#include<iostream>
#include<cstdlib>
#define MAXSIZE 100 //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType; // 自己定义的类型
typedef int status; // 上同
using namespace std;
//链队列定义
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*Queueptr;
//嵌套定义
typedef struct
{
Queueptr front; //Queueptr类型的头指针
Queueptr rear; //Queueptr类型的尾指针
}LinkQueue;//也就是说Queueptr类型的头指针里面包括数据域和next域
//链队列的初始化
status InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=new QNode; //为头指针和尾指针开辟动态空间
Q->front->next=NULL; //初始头指针的next为空
return OK;
}
//链队列的入队操作
status ENQueue(LinkQueue *Q,QElemType e)
{
QNode*p;
p=new QNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return OK;
}
//链队列的出队操作
status DeQueue(LinkQueue *Q,QElemType *e)
{
QNode*p;
if(Q->front==Q->rear)
return ERROR;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
return OK;
}
//取链队的队头元素
status GetHead(LinkQueue *Q)
{
if(Q->front==Q->rear)
{
return ERROR;
}
else
{
return Q->front->next->data;
}
}
//输出队列的元素
status printQueue(LinkQueue *Q)
{
cout<<"当前队列:";
Queueptr p=Q->front->next;
while(p)
{
cout<<'\40'<<p->data ;
p=p->next;
}
cout<<endl;
return OK;
}
//求链队的长度
status QueueLength(LinkQueue *Q)
{
int Length=0;
if(Q->front==Q->rear)
{
return 0;
}
Queueptr p=Q->front->next;
while(p)
{
p=p->next;
Length++;
}
return Length;
}
int main()
{
int n,m,t;
QElemType e;
LinkQueue Q;
InitQueue(&Q);
cout<<"请输入需要入队的次数:";
cin>>n;
for(int i=1 ;i<=n;i++)
{
cout<<"请输入第"<<i<<"个元素的值:";
cin>>m;
ENQueue(&Q,m);
}
printQueue(&Q);
cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
cout<<"当前链队长度为:"<<QueueLength(&Q);
cout<<endl<<endl;
cout<<"请输入需要出队的次数:";
cin>>t;
for(int i=1;i<=t;i++)
{
DeQueue(&Q,&e);
}
printQueue(&Q);
cout<<"当前对头元素为:"<<GetHead(&Q)<<endl;
cout<<"当前链队长度为:"<<QueueLength(&Q);
return 0;
}
《数据结构》(C语言版)(第2版)—严蔚敏 李冬梅 吴伟民 编著
多多关注!
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。