网站建设资讯

NEWS

网站建设资讯

c语言取队头元素的函数 队列获取队头元素

C语言,用数组实现队列的入队,出队函数编程

这样的话应该符合你的要求:

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟空间、营销软件、网站建设、武夷山网站维护、网站推广。

#includestdio.h

void add(int queue[],int x);

int Top(int queue[]);

void del(int queue[]);

int end=0;

int main()

{

int n;

scanf("%d",n);//将要入队列n个元素

int queue[1000];

for(int i=1;i=n;i++)//输入n个元素

{

add(queue,i);//将i加入队列

}

//验证加入队列的元素,将队列中的元素按照输入的顺序输出:

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

{

printf("%d ",Top(queue));//Top函数返回队头元素

del(queue);//删除队头元素

}

//验证输出已经出队列后的队列(数组)元素:

printf("\n");

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

printf("%d ",queue[i]);

printf("\n");

return 0;

}

void add(int queue[],int x)

{

queue[++end]=x;

}

int Top(int queue[])

{

return queue[1];//注意,这里的函数始终return queue[1];这里是和将普通数组中的元素输出最大的不同之处。!!!!!!

}

void del(int queue[])

{

for(int i=2;i=end;i++)

{

queue[i-1]=queue[i];

}

queue=0;//将删除后的地方置0

end--;

}

c语言编写,用数组表示栈,不用指针,编写gettop函数(取栈顶元素)

int top;//要求栈顶为全局变量

int gettop(int a[])

{

int e;

if(top == -1)//top=-1视为栈空.视具体情况分析,也可以top==0

{

printf("栈空\n");

}

e=a[top];//取值

top--; //栈顶自减

return e;

}

-是指针变量对结构成员的调用等同 点符号.

数据结构(使用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语言版)队列基本操作的实现

/***************/

/* 链式队列 */

/***************/

#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.rearQ.front) Q.rear=Q.rear+N;

for(i=Q.front; iQ.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语言取队头元素的函数 队列获取队头元素
分享路径:http://cdweb.net/article/dohgdsh.html