c语言队列主函数出队,队列C语言实现

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

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

十多年的善右网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整善右建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“善右网站设计”,“善右网站推广”以来,每个客户项目都认真落实执行。

#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语言编程实现出队与入队?急!!!

#include assert.h

#include iostream.h

#ifndef POINTQUEUE

#define POINTQUEUE

template class Type class Queue; //前视声明

template class Type

class QueueNode

{

friend class QueueType;

private:

Type data; //队列结点数据

QueueNodeType *link; //结点链指针

QueueNode(Type d=0,QueueNode *l=NULL):data(d),link(l){};

};

template class Type

class Queue

{

public:

Queue():rear(NULL),front(NULL),length(0){};

~Queue();

void EnQueue(const Type item);

Type DeQueue();

Type GetFront();

void MakeEmpty(){ Distroy();front=rear=NULL; }

int Length(){ return length;}

bool IsEmpty()const { return front==NULL; }

private:

QueueNodeType *front, *rear; //队列指针

int length;

void Distroy();

};

template class Type

QueueType::~Queue() //队列的析构函数

{

Distroy();

}

template class Type

void QueueType::EnQueue(const Type item) //将新元素item插入到队列的队尾

{

length++;

if(front==NULL)

front=rear=new QueueNodeType(item,NULL);

else

rear=rear-link=new QueueNodeType(item,NULL);

}

template class Type

Type QueueType::DeQueue() //删去队头结点,并返回队头元素的值

{

assert(!IsEmpty()); //判队空的断言

length--;

QueueNodeType *p = front;

Type retvalue=p-data; //保存队头的值

front=front-link; //新队头

delete p;

return retvalue;

}

template class Type

Type QueueType::GetFront() //若队不空,则函数返回队头元素的值

{

assert(!IsEmpty());

return front-data;

}

template class Type

void QueueType::Distroy()

{

QueueNodeType *p;

while(front!=NULL) //逐个结点释放

{

p=front;

front=front-link;

delete p;

}

}

#endif

#include "pointqueue.h"

int main()

{

// Queueint qu(10);

Queueint qu;

int i;

for(i=0;i10;i++)

qu.EnQueue(i*10-3);

coutqu.Length()endl;

for(i=0;i10;i++)

coutqu.DeQueue()' ';

return 1;

}

数据结构c语言版,出队入队及依次输出一个队列的操作。

黑色的提示框是程序运行结果窗口,不是错误的窗口

代码错误说明如下:

while(Q-front!=Q-rear)//在本循环体之中,Q-front Q-rear的值始终没有变化

//故而在这里肯定是一个死循环

{

printf("%d,  ", Q-front-next-data);

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

}

//改正后的代码如下:

QNode* s = Q-front;

while(s!=Q-rear)

{

printf("%d,  ", s-data);

s=s-next;

}

另外,所有的函数当中不应该有exit

exit是一个系统函数,表示结束程序,而不是退出函数

如果需要退出函数可以使用return来达到该目的

c语言 队列的操作

//定义队列结构体

typedef struct Qnode

{

int data;

struct Qnode *next;

} Queue , *QueuePtr;

typedef struct

{

QueuePtr front;

QueuePtr rear;

} linkQnode;

//创建一个队列

initQueue (linkQnode *q)

{

q - front = q - rear = (QueuePtr) malloc (sizeof (Queue));

if (!q - front) exit (0);

q - front - next = NULL;

}

//入队列

EnterQueue (linkQnode *q , int item)

{

QueuePtr p;

p = (QueuePtr) malloc (sizeof (Queue));

if (!p) exit (0);

p - data = item;

p - next = NULL;

q - rear - next = p;

q - rear = p;

}

//出队列

DelQueue (linkQnode *q , int *item)

{

QueuePtr p;

if (q - front = q - rear) return;

p = q - front - next;

*item = p - data;

q - front - next = p - next;

if (q - rear == p)

q - rear = q - front;

free (p);

}

C语言用数组实现循环队列的入队出队

//定义一个int型数组que,长度为N(常量切大于2).

int que[N];

int rear=0,front=0; //队尾 队头

判断队列已满:

if((front+1)%N==rear%N)  //成立则队列已满

判断队列为空

if((rear==front)) //成立则队列空

入队(一般在入队前判断队列是否已满)

//将val入队

que[front++]=val;

front%=N;

出队(一般在出队前判断队列是否为空)

rear=(rear+1)%N;

下一个要出队的元素(一般先判断是否为空)

que[rear];


网站栏目:c语言队列主函数出队,队列C语言实现
分享路径:http://lszwz.com/article/dsspsjo.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款