当前位置:首页 » 编程语言 » c语言实现链队列的基本操作
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实现链队列的基本操作

发布时间: 2022-08-09 22:30:23

Ⅰ 求c语言实现链队列的建立和出入队列


#include<stdio.h>
#include<stdlib.h>

structNode{
intdata;//数据类型自己根据需要定义
Node*next;
};

structQueue{
Node*head,*rear;
};

Queue*creatQueue(){
Queue*Q=newQueue;
Node*node=(Node*)malloc(sizeof(Node));//创建头节点
node->next=NULL;
Q->head=Q->rear=node;
returnQ;
}

voidenQueue(Queue*Q,intd){
Node*node=(Node*)malloc(sizeof(Node));
node->data=d;
node->next=NULL;
Q->rear->next=node;
Q->rear=node;
}

intdeQueue(Queue*Q){
intdata;
if(Q->head==Q->rear){
printf("队列下溢!");
return99999999;//表示错误
}
Node*node=Q->head->next;
data=node->data;
Q->head->next=node->next;
if(node->next==NULL){
Q->rear=Q->head;
}
free(node);
returndata;
}

voidprintQueue(Queue*Q){
Node*node=Q->head->next;
puts("Queue:");
while(node!=NULL){
printf("%d",node->data);
node=node->next;
}
putchar(' ');
}

voiddestroyQueue(Queue*Q){//回收内存
Node*node;
while(Q->head){
node=Q->head->next;
free(Q->head);
Q->head=node;
}
free(Q);
}

voidmain(){
intd,run=1;
Queue*Q=creatQueue();
while(run){
system("cls");
printQueue(Q);
puts("1、入队 2、出队 3、退出 请输入操作号:");
scanf("%d",&d);
switch(d){
case1:
puts("请输入需要入队的数字:");
scanf("%d",&d);
enQueue(Q,d);
break;
case2:
printf("%d出队 ",deQueue(Q));
break;
case3:
run=0;
break;
default:
puts("输入错误! ");
}
system("pause");
}
destroyQueue(Q);
}

Ⅱ 数据结构(c语言版)队列基本操作的实现

/***************/
/* 链式队列 */
/***************/
#include "stdlib.h"
#include "stdio.h"

/* 定义链式队列类型 */
typedef int ElemType;
typedef struct QNode
{ ElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct
{ QueuePtr front;
QueuePtr rear;
} LinkQueue;

/* 1、初始化链式队列 */
void InitQueue(LinkQueue *Q)
{ Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if (!(Q->front)) exit(0);
Q->front->next=NULL; }

/* 2、销毁链式队列 */
void DestroyQueue(LinkQueue *Q)
{ while (Q->front)
{ Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear; }
}

/* 3、清空链式队列 */
void ClearQueue(LinkQueue *Q)
{ QueuePtr p;
p=Q->front->next;
while (p)
{ Q->front->next=p->next;
free(p); }
Q->rear=Q->front;
}

/* 4、判断空队列 */
int QueueEmpty(LinkQueue Q)
{ if (Q.front==Q.rear)
return 1;
else
return 0; }

/* 5、求链式队列长度 */
int QueueLength(LinkQueue Q)
{ QueuePtr p; int n=0;
p=Q.front;
while (p!=Q.rear)
{ n++; p=p->next; }
return n;
}

/* 6、取队头元素 */
ElemType GetHead(LinkQueue Q)
{ if (Q.front!=Q.rear)
return Q.front->next->data;
}

/* 7、入队列 */
void EnQueue(LinkQueue *Q, ElemType e)
{ QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if (!p) exit(0);
p->data=e; p->next=NULL;
Q->rear->next=p;
Q->rear=p; }

/* 8、出队列 */
void DeQueue(LinkQueue *Q, ElemType *e)
{ QueuePtr p;
if (Q->front!=Q->rear)
{ p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if (Q->rear==p) Q->rear=Q->front;
free(p); }
}

/* 9、遍历链式队列并输出元素 */
void QueueTraverse(LinkQueue Q)
{ QueuePtr p;
printf("\nQueue: ");
p=Q.front->next;
while (p)
{ printf("%d\t",p->data);
p=p->next;}
}

/* 约瑟夫问题 */
void Joseffer(int n)
{ LinkQueue Q; int i; ElemType x;
InitQueue(&Q);
for(i=1; i<=n; i++)
EnQueue(&Q,i);
while (!QueueEmpty(Q))
{ for(i=1; i<=3; i++)
{ DeQueue(&Q,&x);
if (i!=3)
EnQueue(&Q,x);
else
printf("%5d",x);
}
}
}

/* 主函数 */
main()
{ LinkQueue Q; int i; ElemType x;
InitQueue(&Q);
for(i=2; i<=5; i++)
EnQueue(&Q,i);
printf("len:%d\n",QueueLength(Q));
while (!QueueEmpty(Q))
{ DeQueue(&Q,&x);
printf("%d\t",x); }
//QueueTraverse(Q);
//Joseffer(6);
}

自己去调试吧,这个是C语言版的链式队列,如果看不懂或者调不出来就去看书吧。否则你这门是白学了。
注:这里是链式队列

/***************/
/* 循环队列 */
/***************/
#include "stdlib.h"
#include "stdio.h"
#define N 100

/* 定义循环队列类型 */
typedef int ElemType;
typedef struct
{ ElemType *base;
int front;
int rear;
} SqQueue;

/* 1、初始化循环队列 */
void InitQueue(SqQueue *Q)
{ Q->base=(ElemType*)malloc(N*sizeof(ElemType));
Q->front=Q->rear=0; }

/* 2、销毁循环队列 */
void DestroyQueue(SqQueue *Q)
{ free(Q->base); }

/* 3、清空循环队列 */
void ClearQueue(SqQueue *Q)
{ Q->front=Q->rear=0; }

/* 4、判断空队列 */
int QueueEmpty(SqQueue Q)
{ if (Q.front==Q.rear)
return 1;
else
return 0; }

/* 5、求循环队列长度 */
int QueueLength(SqQueue Q)
{ return (Q.rear+N-Q.front)%N; }

/* 6、取队头元素 */
void GetHead(SqQueue Q, ElemType *e)
{ if (Q.front!=Q.rear)
*e=Q.base[Q.front];
}

/* 7、入队列 */
int EnQueue(SqQueue *Q, ElemType e)
{ if ((Q->rear+1)%N==Q->front)
return 0;
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%N;
return 1; }

/* 8、出队列 */
int DeQueue(SqQueue *Q, ElemType *e)
{ if (Q->front==Q->rear)
return 0;
*e=Q->base[Q->front];
Q->front=(Q->front+1)%N;
return 1; }

/* 9、遍历循环队列并输出元素 */
void QueueTraverse(SqQueue Q)
{ int i;
printf("\nQueue: ");
if (Q.rear<Q.front) Q.rear=Q.rear+N;
for(i=Q.front; i<Q.rear; i++)
printf("%d\t",Q.base[i%N]); }

/* 主函数 */
main()
{ SqQueue Q; int i; ElemType x;
InitQueue(&Q);
for(i=2; i<=5; i++)
EnQueue(&Q,i);
printf("len:%d\n",QueueLength(Q));
while (!QueueEmpty(Q))
{ DeQueue(&Q,&x);
printf("%d\t",x); }
QueueTraverse(Q);
}

在给你个循环队列吧

Ⅲ 链式存储队列的数据结构(逻辑结构+存储结构)分析、链式存储队列的基本C语言结构体分析与定义

链式队列

链式存储结构的队列称作链式队列。

链式队列的队头指针指在队列的当前队头结点位置,队尾指针指在队列的当前队尾结点位置。不带头结点的链式队列时可直接删除队头指针所指的结点。

链式队列中结点的结构体可定义如下:

typedef struct qnode

{

DataType datal;

Struct qnode *next;

}LQNode;

为了方便参数调用,通常把链式队列的队头指针front和队尾指针rear也定义为如下的结构体类型LQueue:

typedef struct

{

LQNode *front;

LQNode *rear;

}LQueue;

链式队列操作的实现

(1) 初始化QueueInitiate(LQueue *Q)

void QueueInitiate(LQueue *Q)

{

Q->rear=NULL;

Q->front=NULL;

}

(2)非空否QueueNotEmpty(LQueue Q)

int QueueNotEmpty(LQueue Q)

/*判断链式队列Q非空否,非空返回1,否则返回0*/

{

if(Q.front==NULL)return 0;

else return 1;

}

(3)入队列QueueAppend(LQueue *Q,DataType x)

int QueueAppend(LQueue *Q,DataType x)

/*把数据元素x插入链式队列Q队列的队尾,入队列成功返回1,否则返回0*/

{

LQNode *p;

if((p=(LQNode*)malloc(sizeof(LQNode)))==NULL)

{

printf(“内存不足无法插入!\n);

return 0;

}

p->data=x;

p->next=NULL;

if(Q->rear!=NULL)Q->rear->next=p;

Q->rear=p;

if(Q->front==NULL)Q->front=p;

return 1;

}

(4)出队列QueueDelete(LQueue *Q,DataType *d)

int QueueDelete(LQueue *Q,DataType *d)

/*删除链式队列Q的队头数据元素值到d,出队列成功返回1,否则返回0*/

{

LQNode *p;

if(Q->front==NULL)

{

printf(“队列已空无数据元素出队列!\n”);

return 0;

}

else

{

*d=Q->front->data;

p=Q->front;

Q->front=Q->front->next;

if(Q->front==NULL)Q->rear=NULL;

free(p);

return 1;

}

}

(5)取队头数据元素QueueGet(LQueue *Q,DataType *d)

int QueueGet(LQueue *Q,DataType *d)

/*取链式队列Q的当前队头数据元素值到d,成功返回1,否则返回0*/

{

if(Q.front==NULL)

{

printf(“队列已空无数据元素出队列!\n);

return 0;

}

else

{

*d=Q.front->data;

return 1;

}

}

(6)撤销动态申请空间Destory(LQueue *head)

int Destory(LQueue *head)

{

LQNode *p,*p1;

p=Q.front;

while(p!=NULL)

{

p1=p;

p=p->next;

free(p1);

}

}
帮你转的,我觉得他描述的很清楚。希望对你有帮助。

Ⅳ 谁做过c语言的 队列及其操作啊

我自己写了一个,练练手
由于没有指定数据类型,在第三行处我用float类型了
如果改成其它类型,代码中有//Type处,涉及输入输出,也要相应的改一下
这个代码只做了简单的测试,不保证正确性

#include<stdio.h>
#include<malloc.h>
#defineTypefloat
structnode{
Typedata;
structnode*next;
};
structqueue{
structnode*front,*rear;
};
structnode*push(Typet,structqueue*q)
{
structnode*last;
last=(structnode*)malloc(sizeof(structnode));
last->data=t;
last->next=NULL;
if(q->rear!=NULL){
q->rear->next=last;
q->rear=last;
}else{
q->front=last;
q->rear=last;
}
returnlast;
}
Typepop(structqueue*q)
{
structnodend;
if(q->front==NULL){
printf("<<<Error:queueisblank ");
return0;
}
nd=*(q->front);
free(q->front);
if(q->front!=q->rear)
q->front=nd.next;
else{
q->front=NULL;
q->rear=NULL;
}
returnnd.data;
}
voidclear(structqueue*q)
{
structnode*np,*np1;
if(q->front!=NULL){
np=q->front;
while(1){
np1=np;
free(np);
if(np==q->rear)
break;
np=np1->next;
}
q->front=NULL;
q->rear=NULL;
}
printf("<<<Thequeuehasbeencleared ");
}
structqueue*init()
{
structqueue*q=(structqueue*)malloc(sizeof(structqueue));
q->front=NULL;
q->rear=NULL;
returnq;
}
voiddisplay(structqueue*q)
{
structnode*np;
np=q->front;
if(np==NULL){
printf("<<<Thequeueisblank ");
return;
}
while(1){
printf(">>>%f ",np->data);//Type
if(np==q->rear)
break;
np=np->next;
}
}
intmain()
{
structqueue*q;
unsignedintc;
Typet;
q=init();
printf("<<<Oneblankqueuehasbeencreated ");
while(1){
printf("Menu:
*******************************************************
1:pushanelementtoqueue
2:popanelementfromqueue
3:clearqueue
4:displayqueue
5:exit
*******************************************************
Pleaseselectoperator ");
scanf("%d",&c);
switch(c){
case1:
printf(": ");
scanf("%f",&t);//Type
push(t,q);
break;
case2:
printf(">>>Theelementpopedfromqueueis%f ",pop(q));//Type
break;
case3:
clear(q);
break;
case4:
display(q);
break;
case5:
clear(q);
free(q);
break;
default:
printf("Invalidinput ");
break;
}
if(c==5)
break;
}
return0;
}

Ⅳ 建立循环链队列,并在循环链队列上实现入队、出队基本操作

/* 实现循环队列的基本操作(初始化、判断队空、判断队满、入队、出队) */ //在javascript中,可以使用数组来实现一个队列 function stack(){ this.datastore = new Array(); //初始化 this.isEmpty = isEmpty; //判断队空 this.isFull = isFull; //判断队满 this.add = add; //入队 this.remove = remove; //出队 this.count = 0; function isEmpty(){ if(this.count == 0) return true; return false; } function isFull(){ if(!isEmpty()) return true; return false; } function add(value){ alert(this.count); this.datastore[this.count ++] = value; } function remove(){ if(this.count <= 0) { this.count = 0; alert('当前队列为空,无法删除!'); return; } delete this.datastore[-- this.count ]; } }

Ⅵ 数据结构中的链式队列的基本操作的实现C或C++。。帮我找一下下面程序问题的所在。谢谢

#include<iostream>
using namespace std;
#define true 1
#define false 0

typedef struct Node //定义一个队列
{
int data;
struct Node *next;
}LinkQueueNode;

typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;

void InitQueue(LinkQueue *Q) //初始化一个队列,把声明的队列的对象作为初始化的参数,不能用局部声明的变量
{
Q->front=Q->rear=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
Q->front->next=NULL;
}

void CreatQueue(LinkQueue *Q) //建立一个队列
{
int i,n;
LinkQueueNode *s;
cout<<"输入队列中的元素的个数n"<<endl;
cin>>n;
cout<<"输入队列中的元素"<<endl;
for(i=0;i<n;i++)
{
s=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));//要分配空间
cin>>s->data;
Q->rear->next=s;
Q->rear=s;
//s=s->next; 无用
}
Q->rear->next=NULL;
}

int EnterQueue(LinkQueue *Q,int x) //入队操作
{
LinkQueueNode *NewNode;
NewNode=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
if(NewNode!=NULL)
{
NewNode->data=x;
NewNode->next=NULL;
Q->rear->next=NewNode;
Q->rear=NewNode;
return(true);
}
else
return(false);
}

void DeleteQueue(LinkQueue *Q) //出对操作
{
int data;
LinkQueueNode *p;
if(Q->front==Q->rear)
{
cout<<"队列为空"<<endl;

}
else
{
p=Q->front->next;
Q->front->next=p->next;
data=p->data;
free(p);
cout<<data<<endl;
}
}

void Output(LinkQueue *Q) // 输出队列中的值
{
LinkQueue *s;
s=Q;
LinkQueueNode *p;
p=s->front->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

int main()
{
LinkQueue *Q=(LinkQueue*)malloc(sizeof(LinkQueue));
InitQueue(Q);
int aa=1,x;
while(aa)
{
cout<<"建队列请按1"<<endl;
cout<<"进队列请按2"<<endl;
cout<<"出队列请按3"<<endl;
cout<<"输出请按4"<<endl;
cout<<"退出请按0"<<endl;
cin>>aa;
switch(aa)
{
case 1:
CreatQueue(Q);
break;
case 2:
cout<<"输入进队列的值x"<<endl;
cin>>x;
EnterQueue(Q,x);
break;
case 3:
DeleteQueue(Q);
break;
case 4:
Output(Q);
break;
case 0:return(0);
break;
default:aa=0;
break;
}
}
return 0;
}

已经测试,没的问题

Ⅶ 数据结构C语言描述的链队列的基本操作(初始化,判空,入队,出队,取对头,输出队列所有值....)

void InitQueue(LiQueue *&q)
{q=(LiQueue *)malloc(sizeof(LiQueue));
q->front=q->rear-NULL;} //初始化

int QueueEmpty(LiQueue *q)
{if(q->rear==NULL)
return 1;
else
return 0;} //判空

void enQueue( LiQueue *&q,ElemType e)
{QNode *s;
s=(QNode *)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
if(q->rear==NULL)
q->front=q->rear=s;
else
{q->rear->next=s;
q->rear=s;
}} //入队

int deQueue( LiQueue *&q,ElemType &e)
{QNode *t;
if(q->rear==NULL)
return 0;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;
free(t);
return 1;} //出队

int deQueue( LiQueue *&q,ElemType &e)
{QNode *t;
if(q->rear==NULL)
return 0;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;break;
free(t);
return 1;} //取队头

输出队列所有数就是出队

Ⅷ c语言队列操作

pq->rear->next
=
pnew这个代码从队列的尾部增加新节点,
然后pq->rear
=
pnew更新队列尾部指针。队列的数据结构形式就是由一个头front指针,一个尾rear指针来表征,items的设计是用空间换时间,涉及队列大小的操作会非常方便。
队列的特征是先进先出,你给出的链式实现,其实就跟一个链表一样,链表的添加删除如果能理解了,队列只是链表的元素增加/删除
按先进先出特点的一种实现。
但对于队列来说,实现方式不是重点,先进先出的性质才是重点,这在实际应用中很多,比如排队叫号。

Ⅸ 用C语言编写队列的各种基本操作,我不是非常明白:注释里有些问题:请大家讲讲,如果可能,请详细讲讲各种

ont)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。 在队列这种数据结构中,最先插入在元素将是最先被删除;反之最后插入的元素将最后被删除,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。 队列空的条件:front=rear 队列满的条件: rear = MAXSIZE 队列可以用数组Q[1…m]来存储,数组的上界m即是队列所容许的最大容量。在队列的运算中需设两个指针:head,队头指针,指向实际队头元素的前一个位置;tail,队尾指针,指向实际队尾元素所在的位置。一般情况下,两个指针的初值设为0,这时队列为空,没有元素。图1 ( a)画出了一个由6个元素构成的队列,数组定义Q[1…10]。Q(i) i=3,4,5,6,7,8头指针head=2,尾指针tail=8。队列中拥有的元素个数为:L=tail-head现要让排头的元素出队,则需将头指针加1。即head=head+1这时头指针向上移动一个位置,指向Q(3),表示Q(3)已出队。见图1 (b)。如果想让一个新元素入队,则需尾指针向上移动一个位置。即tail=tail+1这时Q(9)入队,见图1 (c)。当队尾已经处理在最上面时,即tail=10,如果还要执行入队操作,则要发生"上溢",但实际上队列中还有三个空位置,所以这种溢出称为"假溢出"。 克服假溢出的方法有两种。一种是将队列中的所有元素均向低地址区移动,显然这种方法是很浪费时间的;另一种方法是将数组存储区看成是一个首尾相接的环形区域。当存放到n地址后,下一个地址就"翻转"为1。在结构上采用这种技巧来存储的队列称为循环队列。 队列和栈一样只允许在断点处插入和删除元素。 循环队的入队算法如下: 1、tail=tail+1; 2、若tail=n+1,则tail=1; 3、若head=tail尾指针与头指针重合了,表示元素已装满队列,则作上溢出错处理; 4、否则,Q(tail)=X,结束(X为新入出元素)。 队列和栈一样,有着非常广泛的应用。

具体网上查:

另外,虚机团上产品团购,超级便宜