
#include<iostream>
typedef char QElemType;
QElemType data;
struct QNode next;
}QNode,QueuePtr;
typedef struct{
QueuePtr front,rear;
}LinkQueue;
void InitQueue(LinkQueue &Q){ //创建一个带头结点的空队列
Qfront=new QNode;
if (!Qfront) exit(0);
Qrear=Qfront;
Qfront->next=NULL;
}
void DestroyQueue(LinkQueue &Q){ //销毁队列
while(Qfront){
Qrear=Qfront->next;
delete Qfront;
Qfront=Qrear=NULL;
}
}
void EnQueue(LinkQueue &Q,QElemType e){ //入队
QueuePtr p;
p=new QNode;
if (!p) exit(0);
p->data=e;
p->next=NULL;
if(Qfront==Qrear) Qfront->next=p;
Qrear->next=p;
Qrear=p;
}
void DeQueue(LinkQueue &Q,QElemType &e){ //出队
QueuePtr p;
if (Qfront==Qrear) return; //如果是空队列则返回
p=Qfront->next;
e=p->data; //将队头的值保存在e中
Qfront->next=p->next;
if (Qrear==p) Qrear=Qfront; //如果要删除的是队列中最后一个元素
delete p;
}
void GetFront(LinkQueue Q,QElemType &e)
{
if(Qfront==Qrear)return;
e=Qfront->next->data;
}
#include "stdioh"
#include "stdlibh"
#include "zh"
//链队列初始化
void initlinkqueue(LINKQUEUE q)
{
q->front=(LINKQLIST )malloc(sizeof(LINKQLIST));
(q->front)->next=NULL;
q->rear=q->front;
}
//判链队列空
int emptylinkqueue(LINKQUEUE q)
{
int v;
if(q->front==q->rear)
v=1;
else
v=0;
return v;
}
//读链队列队首元素
DATATYPE1 getlinkfront(LINKQUEUE q)
{
DATATYPE1 v;
if(emptylinkqueue(q))
v=NULL;
else
v=(q->front)->next->data;
return v;
}
//元素插入链队列
void enlinkqueue(LINKQUEUE q,DATATYPE1 x)
{
(q->rear)->next=(LINKQLIST )malloc(sizeof(LINKQLIST));
q->rear=(q->rear)->next;
(q->rear)->data=x;
(q->rear)->next=NULL;
}
//从链队列中删除元素
DATATYPE1 dellinkqueue(LINKQUEUE q)
{
LINKQLIST p;
DATATYPE1 v;
if(emptylinkqueue(q))
{ printf("Queue is empty\n");
v=NULL;}
else
{p=(q->front)->next;
(q->front)->next=p->next;
if(p->next==NULL)
q->rear=q->front;
v=p->data;
free(p);}%0
循环单链中尾指针执行一个命令:rear=rear->next; 不就成头指针了~
插入:
InserterList_Dul(DuLNode l,Datatype p,Datatype e)/将E元素插入到循环单链表L中的P指针所指的元素前面/
{
s=(struct DuLNode )malloc(sizeof(sturct DuLNode));/申请一个节点,让指针S指向它/
s->data=e; /将S送入新节点/
s->next=p;/使新节点的后继指针指向P/
s->prior=p->prior;/使新节点的前驱指针指向P的前驱指针/
p->prior->next=s;/使P的前驱节点的后继指针指向新节点/
p->prior=s;/使P的前驱指针指向新节点/
}
删除:
DeleteList_Dul(DulNOde l,DuLnode p) /删除循环单链表L中P指针所指的元素/
{
p->prior->next=p->next;/使P的前驱节点的后继指针指向P的后继节点/
p->next->prior=p->prior;/使P的后继节点的前向指针指向P的前驱节点/
free(p);/释放P所指被删除的节点/
}
#include<stdioh>/湖南大学计科/
#include<stdlibh>
# define MAXSIZE 100
typedef char DataType;
typedef struct
{DataType elem [MAXSIZE];
int front,rear;
}SeQueue;
void addq (SeQueue Q ,DataType x)
{
if((Q->rear+1)%MAXSIZE==Q->front) printf("Queue is FULL!\n");
else{Q->rear=(Q->rear+1)%MAXSIZE;Q->elem[Q->rear]=x; }
}
DataType delq(SeQueue Q)
{if(Q->front==Q->rear)
{ printf("Queue is EMPTY!\n"); return (-1);}
else{Q->front=(Q->front+1)%MAXSIZE; return(Q->elem[Q->front]);}
}
DataType front(SeQueue Q)
{DataType x;
if(Q->front==Q->rear){printf("Queue is EMPTY!\n"); return (-1);}
else x=Q->elem[(Q->front+1)%MAXSIZE];
return(x);
}
main()
{
int choice ;DataType x,y;SeQueue Q;int i;
Qrear=-1; Qfront=-1;
printf("\n输入 i="); /输入非0数继续/
scanf("%d",&i);
while(i){printf("\n 队 列 子 系 统\n");
printf("\n");
printf(" 1-----进 队 \n");
printf(" 2-----出 队 \n");
printf(" 3-----读 队 头 元 素 \n");
printf(" 4-----显 示 \n");
printf(" 0-----退 出 \n");
printf("\n");
printf("请输入你的选择(0,1,2,3,4)");scanf("%d",&choice);
switch(choice)
{
case 1:{
printf("输入要进队的元素x=");
scanf(" %c",&x);
addq (&Q , x);
}break;
case 2:{ delq(&Q) ;printf( " %c ",Qelem[Qfront]);}break;
case 3:{y=front(&Q) ;printf("%c",y);}break;
case 4:{for(i=(Qfront+1)%MAXSIZE;i<=Qrear;i++)printf("%c ",Qelem[i]);}break;
case 0:exit(0);
}
}
end
网络答案,已验证:
#include<stdioh>
#include<stdlibh>
struct Node
{
int data; /值域/
struct Node next; /链接指针/
};
struct queue
{
struct Node front; /队首指针/
struct Node rear; /队尾指针/
};
/初始化链队/
void initQueue(struct queue hq)
{
hq->front=hq->rear=NULL; /把队首和队尾指针置空/
}
/向链队中插入一个元素x/
void inQueue(struct queue hq, int x)
{
struct Node newNode; /得到一个由newNode指针所指向的新结点/
newNode=malloc(sizeof(struct Node));
if(newNode==NULL)
{
printf("内存空间分配失败! ");
exit(1);
}
newNode->data=x; /把x的值赋给新结点的值域/
newNode->next=NULL; /把新结点的指针域置空/
/若链队为空,则新结点即是队首结点又是队尾结点/
if(hq->rear==NULL)
{
hq->front=hq->rear=newNode;
}else
{
/若链队非空,则依次修改队尾结点的指针域和队尾指针,使之指向新的队尾结点/
hq->rear=hq->rear->next=newNode;
}
//return;
}
/从队列中删除一个元素/
int delQueue(struct queuehq)
{
struct Nodep;
int temp;
/若链队为空则停止运行/
if(hq->front==NULL)
{
printf("队列为空,无法删除! ");
exit(1);
}
temp=hq->front->data;
/暂存队首元素以便返回/
p=hq->front;
/暂存队首指针以便回收队尾结点/
hq->front=p->next; /使队首指针指向下一个结点/
/若删除后链队为空,则需同时使队尾指针为空/
if(hq->front==NULL)
{
hq->rear=NULL;
}
free(p); /回收原队首结点/
return temp; /返回被删除的队首元素值/
}
/读取队首元素/
int peekQueue(struct queue hq)
{ /若链队为空则停止运行/
if(hq->front==NULL)
{
printf("队列为空,无法删除! ");
exit(1);
}
return hq->front->data; /返回队首元素/
}
/检查链队是否为空,若为空则返回1,否则返回0/
int emptyQueue(struct queue hq)
{
/判断队首或队尾任一个指针是否为空即可/
if(hq->front==NULL)
{
return 1;
}else
{
return 0;
}
}
/清除链队中的所有元素/
void clearQueue(struct queue hq)
{
struct Node p=hq->front; /队首指针赋给p/
/依次删除队列中的每一个结点,最后使队首指针为空/
while(p!=NULL)
{
hq->front=hq->front->next;
free(p);
p=hq->front;
}
/循环结束后队首指针已经为空/
hq->rear=NULL; /置队尾指针为空/
return;
}
int main(int argc,char argv[])
{
struct queue q;
int a[8]={3,8,5,17,9,30,15,22};
int i;
initQueue(&q);
for(i=0;i<8;i++)
{
inQueue(&q,a[i]);
}
printf("delnode is %d\n",delQueue(&q));
printf("delnode is %d\n",delQueue(&q));
inQueue(&q,68);
printf("peeknode is %d\n",peekQueue(&q));
while(!emptyQueue(&q))
{
printf("%d\n",delQueue(&q));
}
clearQueue(&q);
}
考虑不全面
int EnQueue(LinkQueue Q,int e)函数里没有考虑Q->rear== NULL的情况
实际上你初始化后Q->rear就等于NULL,之后Q->rear->next=p访问了非法的内存地址,导致异常退出
#include <stdioh>
#include <stdlibh>
#include <mathh>
#include <conioh>
typedef int ElemType;
typedef struct QNode
{
ElemType data;
struct QNode next;
}QNode;
typedef struct
{
QNode front, rear;
}L_Queue;
L_Queue Q1;
void init_Q(L_Queue Q);
void out_Q(L_Queue Q);
void EnQueue(L_Queue Q,ElemType e);
ElemType DeQueue(L_Queue Q);
void find(L_Queue Q, ElemType y);
int main()
{
int k,y;
ElemType e,x;
char ch;
init_Q( &Q1);
do
{
printf("\n\n\n");
printf("\n\n 1 shuju yuansu e jin duilei ");
printf("\n\n 2 shudui yige yuansu,fanhui qi zhi");
printf("\n\n 3 qing shuru yige yuansu jinxing chazhao ");
printf("\n\n 4 jieshu chengxu yunxing ");
printf("\n======================================");
printf("\n qing shuru ni de xuanze (1,2,3,4)");
scanf("%d",&k);
switch(k)
{
case 1:
{
printf("\n jindui yuansu e=");
scanf("%d",&e);
EnQueue(&Q1,e);
out_Q(&Q1);
} break;
case 2:
{
x=DeQueue(&Q1);
printf("\nchudui yuansu wei : %d", x);
out_Q(&Q1);
} break;
case 3:
{
printf("yao chazhao de shu :");
scanf("%d",&y);
find(&Q1,y);
}break;
case 4: printf("\n beybey !");
}
printf("\n ----------------");
}
while(k>=1 && k<3);
printf("\n da huichejian,tuichu");
ch=getch();
return 0;
}
void init_Q(L_Queue Q)
{
QNode p ;
p=(QNode )malloc(sizeof(QNode));
p->next=NULL;
Q->front=p;
Q->rear=p;
}
void out_Q(L_Queue Q)
{
QNode p;
char ch;
p=Q->front->next;
printf("\n dangqian duilei zhong de yuansu shi:");
while(p!=NULL)
{
printf("\n %d",p->data);
p=p->next;
}
printf("\n da huichejian,jixu ");
ch=getch();
}
void EnQueue(L_Queue Q,ElemType e)
{
QNode s;
s=(QNode)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
Q->rear->next=s;
Q->rear=s;
}
ElemType DeQueue(L_Queue Q)
{
ElemType x; QNode p;
if(Q->front==Q->rear)
{
printf("\n Queue is NULL!");
x=-1;
}
else
{
p=Q->front->next;
x=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
}
return(x);
}
void find(L_Queue Q, ElemType y)
{
int chazhao=0;
QNode p;
p=Q->front;
while (p!=NULL)
{
if(y==p->data)
{
chazhao=1;
break;
}
else p=p->next;
}
if (chazhao>=1) printf("cha dao gai zhi\n");
else printf("cha bu dao gai zhi\n");
}
我在别的地方看到的,希望能对你有些帮助。
虽然我也在学数据结构,可惜学的不好,而且用的是清华的C版。
以上就是关于C++.用头文件定义链队列的数据类型LinkQueue,包括入队,出队,取对头元素等基本 *** 作.全部的内容,包括:C++.用头文件定义链队列的数据类型LinkQueue,包括入队,出队,取对头元素等基本 *** 作.、队列在链式存储上的 *** 作包括:(6) 队列置空 *** 作CLEAR(Q)(7) 求队列中元素个数的 *** 作 CURRENT_SIZE(Q)、数据结构如果一个循环单链表示队列(循环队列),编写程序实现循环队列的插入和删除等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)