189 8069 5689

c语言创建循环队列主函数 c语言队列的实现

C语言循环队列

很简单啊,用一个数组,和2个指针就可以做到这一点,注意,一定要控制好指针,防止写的东西覆盖了没有发送的东西。

成都创新互联主营贡山网站建设的网络公司,主营网站建设方案,成都APP应用开发,贡山h5微信小程序定制开发搭建,贡山网站营销推广欢迎贡山等地区企业咨询

一个指针控制写,一个控制输出。如果走到尾巴 ,就把它移动到数组的0号元素。如果写的郭快,赶上了输出指针就不可以写。或则进行互斥处理,方法太多。不过写起来浪费时间。

c语言 队列问题

//改正如下,但是我觉得你那个出队有点问题,不应当是自己输入要出队的元素吧,是让他自己出队吧,然后把这个出队的元素值带回来,再在主函数中输出吧,没给你改,嘿嘿嘿嘿

#include "stdio.h"

#include "stdlib.h"

#define MAXSIZE 5

typedef struct k

{

int data[MAXSIZE];

int front,rear;

int num;

}c_k; //声明循环顺序队列的数据类型

//创建循环队列

c_k *create_k()

{

c_k * cq=(c_k *)malloc(sizeof(c_k)); //开辟循环队列的空间,并将地址存到变量cq中

cq-front=-1;

cq-rear=-1;

cq-num=0;//初始化为空队列

return cq;//返回队列的地址

}

int In_ck(c_k * cq,int *x)//-----在传的时候是个地址,所以这要用int *x-------///如果要用int x的话,在调用该函数时就要写成In_ck(cq,rear_x)--//

{

if(cq-num==MAXSIZE)//队列已满

{

printf("The c_k is full!\n");return -1;

}

else//队列不满

{

cq-rear=(cq-rear+1)%MAXSIZE;//形成循环

cq-data[cq-rear]=*x;/////------传的是地址,所以这要用*x----/////

cq-num++;//队列中元素个数增1

return 1;

}

}

//出队

int Out_ck(c_k * cq, int *x)//对cq所指向的队列出队,将队首元素存到x所指向的变量,并返回是否出队成功

{

if(cq-num==0)//队列为空

{

printf("The k is null!\n");return -1;

}

else//队列非空

{

cq-front=(cq-front+1)%MAXSIZE;//形成循环

*x=cq-data[cq-front];//将队首元素存到x所指向的变量

cq-num--;//队列中元素个数减1

return 1;

}

}

void main()

{

//调用创建队列函数

c_k * cq=create_k();

int rear_x,front_x;

int op,result;

printf("\n请输入 1入队,2出队,3退出:\n");

scanf("%d",op);

while(op!=3)

{

switch(op)

{

case 1:printf("请入入队元素:\n");

scanf("%d",rear_x);////----这要加号,!!!!!

if((result=In_ck(cq,rear_x))==1)////----加了一个if语句,在队空时就不输出元素了-----////

printf("入对元素是 %d\n",rear_x);

break;

case 2:printf("请输入出队元素:\n");

scanf("%d",front_x);////----这要加号,!!!!!

if ((result= Out_ck(cq,front_x))==1)////----加了一个if语句,在队空时就不输出元素了-----////

printf("出对元素是 %d\n",front_x);

break;

}

printf("\n请输入 1入队 2出队 3退出:\n");scanf("%d",op);

}

free(cq); /////---动态开辟的,用后要用free()释放;---////

}

C语言编程题,实现一个顺序存储的循环队列。

#includestdio.h

#includestdbool.h

#includemalloc.h

typedef

int

typedata;

struct

node

{

struct

node

*prev,

*next;

typedata

data;

};

typedef

struct

node

node;

typedef

struct

node*

link;

//

============init_head===============

//

//头节点的初始化

link

init_head(void)

{

link

head

=

(link)malloc(sizeof(node));

if(head

!=

NULL)

{

head-prev

=

head-next

=

head;

}

return

head;

}

//

============newnode

================

//

//创建新节点

link

newnode(typedata

data)

{

link

new

=

(link)malloc(sizeof(node));

if(new

!=

NULL)

{

//前趋指针和后趋指针都指向自己

new-prev

=

new-next

=

new;

new-data

=

data;

}

return

new;

}

//

=================is_empty================

//

bool

is_empty(link

head)

{

//为空时,头节点的前趋指针和后趋指针都指向head(头节点)

if((head-next==head)

(head-prev==head))

return

true;

return

false;

}

//

================insert_tail

==================

//

void

insert_tail(link

head,

link

new)

{

if(is_empty(head))

{

//第一个节点插入

head-next

=

head-prev

=

new;

new-next

=

new-prev

=

head;

return

;

}

//除了第一个节点插入

new-prev

=

head-prev;

new-next

=

head;

new-prev-next

=

new;

new-next-prev

=

new;

}

//

================show

====================

//

void

show(link

head)

{

//为空时,直接返回

if(is_empty(head))

return

;

//遍历整个链

link

tmp

=

head-next;

while(tmp

!=

head)

{

printf("%d\t",

tmp-data);

tmp

=

tmp-next;

}

printf("\n");

}

//

==============insert_opint

===============

//

void

insert_opint(link

end_node,

link

p)

{

p-prev

=

end_node;

p-next

=

end_node-next;

end_node-next-prev

=

p;

end_node-next

=

p;

}

//

================insertion_sort===========

//

//顺序排序

void

insertion_sort(link

head)

{

if(is_empty(head))

return;

//把队列分拆,头节点和第一个节点为一个已排序的队列,

//其他的节点逐个比较已排序队列插

link

p

=

head-next-next;

head-prev-next

=

NULL;

head-next-next

=

head;

head-next-prev

=

head;

head-prev

=

head-next;

while(p

!=

NULL)

{

link

end_node

=

head-prev;

if(p-data

end_node-data)

{

link

tmp

=

p;

p

=

p-next;

insert_tail(head,

tmp);

}

else

{

while(end_node!=head

p-dataend_node-data)

end_node

=

end_node-prev;

link

tmp

=

p;

p

=

p-next;

insert_opint(end_node,

tmp);

}

}

}

int

main(void)

{

link

head

=

init_head();

if(head

==

NULL)

{

printf("falure\n");

return

0;

}

typedata

data;

while(1)

{

if(scanf("%d",

data)

!=

1

)

break;

link

new

=

newnode(data);

if(new

==

NULL)

{

printf("falure\n");

return

0;

}

insert_tail(head,

new);

show(head);

}

printf("the

figure

is:\n");

show(head);

insertion_sort(head);

show(head);

return

0;

}

C语言,请用数组作个循环队列

a#include

"Stdio.h"

#include

stdlib.h

#include

"Conio.h"

#include

"malloc.h"

#define

TRUE

1

#define

FALSE

#define

INFEASIBLE

1

#define

OVERFLOW

-2

#define

OK

1

#define

ERROR

#define

MAXQSEZE

100

/*最大队列长度*/

typedef

int

QElemType;

typedef

int

Status;

typedef

struct{

QElemType

*base;

/*初始化的动态分配存储空间*/

int

front;

/*头指针,若队列不空,指向队列头元素*/

int

rear;

/*尾指针,若队列不空,指向队列尾元素的下一位置*/

}SqQueue;

Status

Queuelength(SqQueue

*Q){

/*构造一个空的循环队列*/

Q-base=(QElemType

*)malloc(MAXQSEZE*sizeof(SqQueue));

if(!Q-base)

exit(OVERFLOW);

/*存储分配失败*/

Q-front=Q-rear=0;

return

OK;

}

Status

EnQueue(SqQueue

*Q,QElemType

e){

/*插入元素e为Q的新的队尾元素*/

if((Q-rear+1)%MAXQSEZE==Q-front)

return

ERROR;/*队列满*/

Q-base[Q-rear]=e;

Q-rear=(Q-rear+1)%MAXQSEZE;

return

OK;

}

Status

DeQueue(SqQueue

*Q,QElemType

*e){

/*若队列不空,则删除Q的队头元素,用e返回其值*/

/*否则返回ERROR*/

if(Q-front==Q-rear)

return

ERROR;

*e=Q-base[Q-front];

Q-front=(Q-front+1)%MAXQSEZE;

return

OK;

}

Status

GetHead(SqQueue

*Q,QElemType

*e){

/*队列不为空用e返回Q的头元素,并返回OK,否则返回ERROR*/

if(Q-front==Q-rear)

return

ERROR;

*e=Q-base[Q-front];

return

OK;

}

Status

QueueEmpty(SqQueue

*Q){

/*队列为空时返回OK否则返回FALSE*/

if(Q-front==Q-rear)

return

OK;

return

FALSE;

}

void

yanghuiTriangle(int

n){

/*打印输出杨辉三角的钱n(n0)行*/

SqQueue

Q;

char

ch='

';

int

i,k;

QElemType

s,e;

FILE

*fq;

if((fq=fopen("output.txt","w"))==NULL){

/*打开写入文件*/

printf("error

on

open\n");

exit

(1);

}

Queuelength(Q);

/*创建空的队列*/

for(i=1;in;i++)

{

printf("

");

fputc(ch,fq);}

/*输出n个空格以保持三角形的队形*/

printf("1\n");

fprintf(fq,"%d\n",1);

EnQueue(Q,0);

/*添加第一行末尾的行分界0并入队*/

EnQueue(Q,1);

/*第二行的两个1值入队列*/

EnQueue(Q,1);

k=2;

while(kn){

/*通过循环队列输出第2行到第n-1行的值*/

for(i=1;i=n-k;i++)

{printf("

");

fputc(ch,fq);}

/*输出n-k个空格以保持三角形*/

EnQueue(Q,0);

do{

/*输出第k行,计算第k+1行*/

DeQueue(Q,s);

GetHead(Q,e);

if(e)

/*若e为非行分界值0,则打印输出e的值,并加一空格*/

{printf("%d

",e);

fprintf(fq,"%d%c",e,ch);

}

else

{

printf("\n");

fputc('\n',fq);}

/*回车换行,为下一行输出做准备*/

EnQueue(Q,s+e);

/*计算所得抵k+1行的值入队列*/

}while(e!=0);

k++;

}

DeQueue(Q,e);

/*行界值“0“出队列*/

while(!QueueEmpty(Q)){

/*单独处理第n行的值的输出*/

DeQueue(Q,e);

{

printf("%d

",e);

fprintf(fq,"%d%c",e,ch);

}

}

}

int

main(void)

{

FILE

*

fp;

QElemType

n;

if((fp=fopen("input.txt","r"))==NULL){

/*打开写入文件*/

printf("error

on

open\n");

exit

(1);

}

fscanf(fp,"%d",n);

/*读入n*/

fclose(fp);

yanghuiTriangle(n);

getch();

return

0;

}

用一个文件输入一个N,这个数位杨辉三角的行数上面是用循环队列做的,你看看

数据结构(使用C语言)队列

对顺序循环队列,常规的设计方法是使用队尾指针和队头指针,队尾指针用于指出当前胡队尾位置下标,队头指针用于指示当前队头位置下标。现要求:

(1)设计一个使用队头指针和计数器胡顺序循环循环队列抽象数据类型,其中包括:初始化,入队列,出队列,取队头元素肯判断队列是否非空;

#include "stdio.h"

#include "malloc.h"

#include "stdlib.h"

#include "conio.h"

#define MAX 80

typedef struct

{

int data[MAX];

int front,rear;

int num;

}SeQue;

SeQue *Init_SeQue()

{

SeQue *s;

s=new SeQue;

s-front=s-rear=MAX-1;

s-num=0;

return s;

}

int Empty_SeQue(SeQue *s)

{

if(s-num==0)

return 1;

else

return 0;

}

int In_SeQue(SeQue *s,int x)

{

if(s-num==MAX)

return 0;

else

s-rear=(s-rear+1)%MAX;

s-data[s-rear]=x;

s-num++;

return 1;

int Out_SeQue(SeQue *s,int *x)

{

if(Empty_SeQue(s))

return 0;

else

{

s-front=(s-front+1)%MAX;

*x=s-data[s-front];

s-num--;

return 1;

}

}

void Print_SeQue(SeQue *s)

{

int i,n;

i=(s-front+1)%MAX;

n=s-num;

while(n0)

{ printf("%d ",s-data[i]);

i=(i+1)%MAX;

n--;

}

}

void main()

{

SeQue *s;

int k,flag,x;

s=Init_SeQue();

do{

printf("\\\");

printf("\\t\\t\\t循环顺序队列\");

printf("\\t\\t\\t***********************\");

printf("\\t\\t\\t**1-入队**\");

printf("\\t\\t\\t**2-出队**\");

printf("\\t\\t\\t**3-判 队 空**\");

printf("\\t\\t\\t**4-队列显示**\");

printf("\\t\\t\\t**0-返回**\");

printf("\\t\\t\\t***********************\");

printf("\\t\\t\\t 请输入菜单项(0-4):");

scanf("%d",k);

switch(k)

{

case 1:

printf("\请输入入队元素:");

scanf("%d",x);

flag=In_SeQue(s,x);

if(flag==0)

printf("\队满不能入队!按任意键返回..");

else

printf("\元素已入队!按任意键返回..");

getch();

system("cls");

break;

case 2:

flag=Out_SeQue(s,x);

if(flag==0)

printf("\队列空出队失败!按任意键返回..");

else

printf("\队列头元素已出队~!按任意键返回..");

getch();

system("cls");

break;

case 3:

flag=Empty_SeQue(s);

if(flag==1)

printf("\该队列为空!按任意键返回..");

else

printf("\该队列不为空!按任意键返回..");

getch();

system("cls");

break;

case 4:

printf("\该队列元素为:");

Print_SeQue(s);

printf("\按任意键返回..");

getch();

system("cls");

break;

}

}while(k!=0);

}


当前标题:c语言创建循环队列主函数 c语言队列的实现
本文地址:http://cdxtjz.cn/article/hiegoi.html

其他资讯