當前位置:首頁 » 編程語言 » 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;
}

熱點內容
編程vlb 發布:2025-02-12 13:33:17 瀏覽:783
電腦出現無法解析伺服器的dns對策 發布:2025-02-12 13:29:12 瀏覽:158
硬碟的存儲空間是以簇為單位 發布:2025-02-12 13:26:06 瀏覽:356
我的帳號密碼是什麼 發布:2025-02-12 13:24:37 瀏覽:281
網頁版傳奇源碼下載 發布:2025-02-12 13:23:48 瀏覽:828
模型預估演算法 發布:2025-02-12 13:09:46 瀏覽:708
武漢存儲 發布:2025-02-12 13:09:43 瀏覽:204
國內外密碼箱鎖哪裡有賣 發布:2025-02-12 13:02:47 瀏覽:237
傑傑腳本 發布:2025-02-12 13:02:07 瀏覽:35
uc高級編程 發布:2025-02-12 13:01:57 瀏覽:788