当前位置:首页 » 编程语言 » C语言数据结构队列

C语言数据结构队列

发布时间: 2022-05-28 06:34:37

㈠ 我想要c语言,队列方面最基础的知识,基本操作

队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(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为新入出元素)。 队列和栈一样,有着非常广泛的应用。

具体网上查:
http://ke..com/view/38959.htm?fr=ala0_1_1

㈡ c语言常见的数据结构有哪些

1、线性数据结构


元素之间一般存在元素之间存在一对一关系,是最常用的一类数据结构,典型的有:数组、栈、队列和线性表。


2、树形结构


结点间具有层次关系,每一层的一个结点能且只能和上一层的一个结点相关,但同时可以和下一层的多个结点相关,称为“一对多”关系,常见类型有:树、堆。


3、图形结构


在图形结构中,允许多个结点之间相关,称为“多对多”关系。


(1)线性数据结构:元素之间一般存在元素之间存在一对一关系,是最常用的一类数据结构,典型的有:数组、栈、队列和线性表


(2)树形结构:结点间具有层次关系,每一层的一个结点能且只能和上一层的一个结点相关,但同时可以和下一层的多个结点相关,称为“一对多”关系,常见类型有:树、堆


(3)图形结构:在图形结构中,允许多个结点之间相关,称为“多对多”关系

㈢ C语言中,队列是什么意思,有什么用途

队列是一种特殊的线性表。

队列一种可以实现“先进先出”的存储结构,即“一端入,一端出”,队首(front)出队,队尾(rear)入队,若front指向队首,则rear指向队尾最后一个有效元素的下一个元素;若rear指向队尾,则front指向队首第一个有效元素的下一个元素。

队列特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

(3)C语言数据结构队列扩展阅读

循环队列各个参数的含义

1、队列初始化front和rear的值都是零,初始化时队列就是空的。

2、队列非空front代表队列的第一个元素rear代表了最后一个有效元素的下一个元素。

3、队列空front和rear的值相等,但是不一定是零。

㈣ 数据结构(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语言)队列

对顺序循环队列,常规的设计方法是使用队尾指针和队头指针,队尾指针用于指出当前胡队尾位置下标,队头指针用于指示当前队头位置下标。现要求:
(1)设计一个使用队头指针和计数器胡顺序循环循环队列抽象数据类型,其中包括:初始化,入队列,出队列,取队头元素肯判断队列是否非空;
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#include"conio.h"
#defineMAX80
typedefstruct
{

intdata[MAX];

intfront,rear;

intnum;
}SeQue;
SeQue*Init_SeQue()
{

SeQue*s;

s=newSeQue;

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

s->num=0;

returns;
}
intEmpty_SeQue(SeQue*s)
{

if(s->num==0)

return1;

else

return0;
}
intIn_SeQue(SeQue*s,intx)
{

if(s->num==MAX)

return0;

else

{

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

s->data[s->rear]=x;

s->num++;

return1;

}
}
intOut_SeQue(SeQue*s,int*x)
{
if(Empty_SeQue(s))

return0;
else
{

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

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

s->num--;

return1;
}
}
voidPrint_SeQue(SeQue*s)
{

inti,n;
i=(s->front+1)%MAX;
n=s->num;
while(n>0)
{printf("%d",s->data[i]);

i=(i+1)%MAX;

n--;
}
}
voidmain()
{

SeQue*s;

intk,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)

{

case1:


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


scanf("%d",&x);


flag=In_SeQue(s,x);


if(flag==0)


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


else


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


getch();


system("cls");


break;

case2:


flag=Out_SeQue(s,&x);


if(flag==0)


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


else


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


getch();


system("cls");


break;

case3:


flag=Empty_SeQue(s);


if(flag==1)


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


else


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


getch();


system("cls");


break;

case4:


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


Print_SeQue(s);


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


getch();


system("cls");


break;

}

}while(k!=0);
}

㈥ c语言数据结构的队列问题

typedefstructnode{//声明一个叫structnode的结构体
DataTypedata;//里面可以放DataType类型的数据,这个数据在当前代码中不清楚是什么类型,要看前面这么定义的
structnode*next;//结构体*next指针,指向下一个同样的结构体
}Qnode,*PQNode;//将structnode的结构体起了一个别名叫Qnode,又将structnode*起了别名叫PQNode
typedefstruct{
PQNodefront,rear;//PQNode类型的头、尾指针
}LinkQueue,*PLinkQueue;//同样的,起了两个别名,第二个是指针类型别名
PLinkQueueQ;//声明一个叫Q的PLinkQueue类型的变量,其实就是一个指针
Q=(PLinkQueue)malloc(sizeof(LinkQueue));//为这个指针赋值,赋值区域为一块sizeof(LinkQueue)那么大的内存空间

㈦ 关于数据结构中队列C语言实现

我改了pop函数。
你忘了第一个Q->head 被你指NULL了,而在后面的函数push中你if()中Q->head=Q->prev=current;语句从来没有执行过,所以head一直是指向空的。你可以看看我给你改的调试信息。
#include<stdio.h>
#include<stdlib.h>
typedef struct Qnode
{
int data;
struct Qnode * next;
}Qnode;
typedef struct listquene
{
Qnode * head;
Qnode * prev;
}listquene;
void initquene(listquene * Q)
{
Q->prev=Q->head=(Qnode*)malloc(sizeof(Qnode));
if(!Q->head)
{
Q->prev=Q->head=NULL;
}
}
void push(listquene * Q,int e)
{
Qnode * current;
if(!Q->head)
{
current=(Qnode*)malloc(sizeof(Qnode));
current->data=e;
current->next=NULL;
Q->head=Q->prev=current;
printf("$$$$\n");
}
else
{
current=(Qnode*)malloc(sizeof(Qnode));
current->data=e;
current->next=NULL;
Q->prev->next=current;
Q->prev=current;
printf("###\n");
}
}
int pop(listquene * Q)
{
if(!Q->head)
printf("empty quene can't be deleted!"),exit(1);
else
{
int temp;
Qnode * p;
temp=Q->head->next->data;
p=Q->head;
Q->head=p->next;
if(!Q->head)
Q->prev=NULL;
free(p);
p=NULL;
return temp;
}
}
void initquene(listquene *);
void push(listquene *,int );
int pop(listquene *);
int main()
{
int i;
listquene Q;
initquene(&Q);
push(&Q,1);
push(&Q,2);
push(&Q,4);
push(&Q,8);
printf("%d\n",pop(&Q));
printf("%d\n",pop(&Q));
printf("%d\n",pop(&Q));
printf("%d\n",pop(&Q));
return 0;
}

㈧ C语言链表与队列的问题

首先:链表与队列都是数据结构的一种
一.
链表
1.定义

链表(Linked
list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在由一个个节点组成,每个节点(node)中储存着数据变量(data)和指针变量(node
next),又有一个头节点(head)连接下面的节点,而最后一个节点指向空(null)。可以在链表类中定义增加,删除,插入,遍历,修改等方法,故常用来储存数据。

2.
优点

(1).使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。

(2).数据的存取往往要在不同的排列顺序中转换,而链表是一种自我指示数据类型,因为它包含指向另一个相同类型的数据的指针(链接)。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。

3.
缺点

链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。

4.
类型

主要有单向链表,双向链表以及循环链表。

5.
实例

6.
与数组(Array)的对比

链表的使用不需要知道数据的大小,而数组在创建时必须指明数组的大小。

链表没有对应的下标,只有指向下一个数据的指针,而数组中每一个都有一个相对应的下标。

链表在内存中储存的数据可以是不连续的,而数组储存的数据占内存中连续的一段,用标识符标识。
二.
队列
1.
定义

队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将最后被删除的元素,因此队列又称为“先进先出”(FIFO—first
in
first
out)的线性表。

㈨ c语言数据结构(循环队列,用顺序结构)

c++

//bc_queue.h

class Queue {
public:
virtual BOOLEAN is_empty(void)=0;
virtual BOOLEAN is_que_full(void)=0;
virtual void build_que(DATA_TYPE str[])=0;
virtual void add_que(DATA_TYPE)=0;
virtual DATA_TYPE del_from_que(void)=0;
virtual int get_que_siz(void)=0;
virtual void print_que(void)=0;
};

//Ary_Circ_Que.h

class Array_Circ_Queue:public Queue{
private:
int QUEUE_SIZ;
int front_of_queue, rear_of_queue;
DATA_TYPE *circ_queue;
void init_ary_circ_que(void);
public:
Array_Circ_Queue(int que_siz);
~Array_Circ_Queue();
BOOLEAN is_empty(void);
BOOLEAN is_que_full(void);
void build_que(DATA_TYPE str[]);
void add_que(DATA_TYPE); //put
DATA_TYPE del_from_que(void); //get
inline int get_que_siz(){return (QUEUE_SIZ);}
void print_que(void);
};

//Ary_Circ_Que.cpp
//

#include<stdio.h>
typedef int BOOLEAN;
const int UNDERFLOW=-1;
typedef char DATA_TYPE;
#include "bc_queue.h"
#include "Ary_Circ_Que.h"

Array_Circ_Queue::Array_Circ_Queue(int que_siz)
{
circ_queue=new DATA_TYPE[QUEUE_SIZ=que_siz];
init_ary_circ_que();
}

Array_Circ_Queue::~Array_Circ_Queue()
{
delete []circ_queue;
}

void Array_Circ_Queue::init_ary_circ_que(void)
{
front_of_queue=QUEUE_SIZ;
rear_of_queue=QUEUE_SIZ;//maximum size
}

BOOLEAN Array_Circ_Queue::is_empty(void)
{
return ((front_of_queue==QUEUE_SIZ)&&(rear_of_queue==QUEUE_SIZ));
}

BOOLEAN Array_Circ_Queue::is_que_full(void)
{
return (((rear_of_queue==0)&&(front_of_queue==QUEUE_SIZ))||(rear_of_queue==(front_of_queue+1)));
}

void Array_Circ_Queue::add_que(DATA_TYPE new_data)
{
if(!is_que_full()){
if(rear_of_queue<=0)
rear_of_queue=QUEUE_SIZ;
if(is_que_empty())
--front_of_queue;
circ_queue[--rear_of_queue]=new_data;
}
else
printf("\n add_que: queue overflow\n");
}

DATA_TYPE Array_Circ_Queue::del_from_que(void)
{
if(!is_que_empty()){
if(front_of_queue<0)
front_of_queue=QUEUE_SIZ;
return (circ_queue[front_of_queue--]);
}
else
{
printf("\n del_que: %s ","queue underflow"):
return (UNDERFLOW);
}
}

void Array_Circ_Queue::build_que(DATA_TYPE str[])
{
if(str[0]=='\0')
printf("\n build_que:empty string \n");
else
for(int j=0;str[j]!='\0';++j)
add_que(str[j]);
}

void Array_Circ_Queue::print_que(void)
{
if(is_que_empty()){
if((rear_of_queue<front_of_queue)&&(rear_of_queue>=0)){
printf(" c_queue[%i]=%c <-queue front\n ",front_of_queue,circ_queue[front_of_queue]);
for(int i=front_of_queue-1;i>=0;i--)
printf(" c_queue[%i]=%c \n",i,circ_queue[i]);
for(i=QUEUE_SIZ-1;i<=rear_of_queue;i--)
printf(" c_queue[%i]=%c \n",i,circ_queue[i]);
}
else{
//case:rear>0&&front<=rear
if(rear_of_queue>0)
printf(" c_queue[%i]=%c <-queue front\n ",front_of_queue,circ_queue[front_of_queue]);
for(int i=front_of_queue-1;(i>=0&&i<=rear_of_queue);i--)
printf(" c_queue[%i]=%c \n",i,circ_queue[i]);
}
else
printf("\n no element in queue.\n");
}

㈩ c语言数据结构循环队列问题

主要错在InitQueue函数里面。当声明一个指针的时候,除了指针本身占用的内存以外,是不会分配具体的内存空间的。也就是说,如果只是CircQueue *q;声明指针q,然后直接使用它的内部成员q->front,q->rear = 0是不合法的。实际上,在Visual Studio里面是编译不通过的。

修改后运行截图

CircQueue*InitQueue(){
CircQueue*q=(CircQueue*)malloc(sizeof(CircQueue));
q->front=0;
q->rear=0;
returnq;
}

热点内容
天龙八部脚本免费 发布:2025-02-12 11:30:12 浏览:501
卡罗拉的配置一般买哪个好一点 发布:2025-02-12 11:20:03 浏览:742
没有服务器的IP怎么连上 发布:2025-02-12 11:19:55 浏览:79
编程sqs 发布:2025-02-12 11:09:55 浏览:239
electron脱离编译环境 发布:2025-02-12 11:08:21 浏览:69
安卓一体机喇叭插口在哪里 发布:2025-02-12 11:07:37 浏览:866
广东江门首选dns服务器地址 发布:2025-02-12 10:56:55 浏览:954
台湾大量IP服务器 发布:2025-02-12 10:51:43 浏览:375
sship访问服务器 发布:2025-02-12 10:50:16 浏览:98
人机局脚本 发布:2025-02-12 10:48:03 浏览:66