c語言隊列代碼
㈠ c語言寫 棧隊列(先進先出的)
#include<stdio.h>
#include<malloc.h>typedef
struct
node
/*定義新類型結構體結點*/
{
int
data;/*數據成員可以是多個不同類型的數據*/
struct
node
*next;/*指針變數成員只能是一個*/
}NODE;/*節點結束*/typedef
struct
qnode
/*定義結點類型的變數名*/
{
NODE
*front;/*設定結點頭指針變數*/
NODE
*rear;/*
設定結點尾指針變數*/
}QNODE;
/*
鏈隊列的結點定義
*/
void
InitQueue(QNODE
*Q)/*定義指針Q*/
{
NODE
*p;/*定義指針p*/
p=(NODE
*)malloc(sizeof(NODE));/*分配結點位元組的容量*/
Q->front=p;
/*指定頭指針p*/
Q->front->next=NULL;/*建立空隊列*/
Q->rear=Q->front;/*改變Q的值*/
printf("The
init
is
complete!");}
void
*QueuePush(QNODE
*Q)
{
NODE
*p;
p=(NODE
*)malloc(sizeof(NODE));/*分配結點位元組的容量*/
printf("Please
input
a
number
:");/*請輸入一個數*/
scanf("%d",&p->data);/*給第一個結點賦值*/
p->next=NULL;/*指定尾結點*/
Q->rear->next=p;/*指定尾新結點p的地址*/
Q->rear=p;/*指定隊尾結束*/
printf("The
%d
has
been
pushed
into
the
Queue!",p->data);/*顯示數據成員*/
return
0;/*程序結束*/
}
void
*QueuePop(QNODE
*Q)
{
NODE
*p;/*定義結點指針*/
if(Q->front->next==NULL)
return
0;/*判斷對前是否為空,如果是就結束*/
p=Q->front->next;/*指向下以個成員*/
Q->front->next=p->next;/*依次向下循環*/
if(Q->rear==p)
Q->rear=Q->front;/*隊尾與對頭相同*/
printf("The
%d
has
been
pop
from
the
queue!
\n",p->data);/*顯示隊列成員*/
free(p);
return
0;
}
void
*PrintQueue(QNODE
*Q)
{
NODE
*p;/*定義鏈結點*/
p=Q->front->next;/*指定對頭*/
while(p!=NULL)/*如不為空*/
{
printf("%5d",p->data);/*顯示數據成員*/
p=p->next;/*指定第二個成員*/
}
return
0;
}
void
main()
{
QNODE
*T;
int
i=0;/*取值*/
printf("1.InitQueue
2.QueuePush
3.QueuePop
4.PrintQueue
5.Quit
\n");
while(i!=5)
{
printf("Please
choose
the
gongneng:");
scanf("%d",&i);
printf("\n");
switch(i)
{
case
1:
InitQueue(T);
printf("\n");
break;
case
2:
QueuePush(T);
printf("\n");
break;
case
3:
QueuePop(T);
printf("\n");
break;
case
4:
printf("The
queue's
numbers
are:");
PrintQueue(T);
printf("\n");
break;
case
5:
printf("\n");
break;}
}
}
㈡ C語言中使用隊列
如果你用vc,#include<deque>就好了,但是注意要加上using naemspace std;
我是當你用的c++的STL,STL中沒有真正的隊列和棧,他們都是通過對雙端隊列的改造得到的,所以包含的文件可能和你想的不一樣。而且這些頭文件都沒有.h結尾!很特別
如果你不是vc,當我沒說
㈢ 數據結構C語言描述的鏈隊列的基本操作(初始化,判空,入隊,出隊,取對頭,輸出隊列所有值....)
void InitQueue(LiQueue *&q)
{q=(LiQueue *)malloc(sizeof(LiQueue));
q->front=q->rear-NULL;} //初始化
int QueueEmpty(LiQueue *q)
{if(q->rear==NULL)
return 1;
else
return 0;} //判空
void enQueue( LiQueue *&q,ElemType e)
{QNode *s;
s=(QNode *)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
if(q->rear==NULL)
q->front=q->rear=s;
else
{q->大局毀rear->next=s;
q->rear=s;
}} //入隊
int deQueue( LiQueue *&q,ElemType &e)
{QNode *t;
if(q->rear==NULL)
return 0;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;
free(t);
return 1;} /臘伍/出隊
int deQueue( LiQueue *&q,ElemType &e)
{QNode *t;
if(q->rear==NULL)
return 0;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;break;
free(t);
return 1;} //取隊頭
輸出滾備隊列所有數就是出隊
㈣ C語言中循環隊列的隊滿和隊空的判斷條件各是什麼有什麼不同
隊空時: Q.front == Q.rear;
隊滿時: Q.front == (Q.rear + 1) % MAXSIZE;
front指向隊首元素,rear指向隊尾元素的下一個元素。
maxsize是隊列長度。
(4)c語言隊列代碼擴展閱讀:
實現的代碼:
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100 //最大隊列長度
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct {
ElemType *base; //隊列空間
int front; //隊頭指針
int rear; //隊尾指針,若隊尾不為空,則指向隊尾元素的下一個位置
}SqQueue;
//初始化循環隊列
Status initQueue(SqQueue &Q) {
Q.base = (ElemType *) malloc(MAXSIZE * sizeof(ElemType)); //申請空間
Q.front = Q.rear = 0; //隊空
return OK;
}
//入隊
Status enQueue(SqQueue &Q, ElemType e) {
if ((Q.rear + 1) % MAXSIZE == Q.front) return ERROR; //隊滿,無法添加
Q.base[Q.rear] = e; //插入元素
Q.rear = (Q.rear + 1) % MAXSIZE; //隊尾指針+1
return OK;
}
//出隊
Status deQueue(SqQueue &Q, ElemType &e) {
if (Q.front == Q.rear) return ERROR; //隊空,無法刪除
e = Q.base[Q.front]
Q.front = (Q.front + 1) % MAXSIZE; //隊頭指針+1
return OK;
}
//返回隊列長度
Status length(SqQueue &Q) {
return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}
㈤ C語言的隊列如何實現和表示
我能想到的有兩種方法(假設隊列元素都是int)
一,用鏈表的方法
struct A
{
int n;
struct A *a;
} *p,*head,*rear;
head=rear=NULL;/*頭指針,尾指針*/
添加元素:p=(struct A*)malloc(sizeof(struct A));......給新元素賦值.....;rear->a=p;rear=p;
當然添加第一個元素的時候要給head賦值。
刪除元素:p=head;head=head->a;free(p);
用的是單向鏈表,當然也可以用雙向鏈表,不過刪除,添加元素的過程要麻煩點。
二,利用數組,當然也可以開辟動態儲存空間
int a[N],*head,*rear; N就是個常數
head=rear=a;
添加元素:scanf("%d",rear-a==N?rear=a:++rear);
刪除元素:head-a==N?head=a:head++;
當然要檢查隊列是否溢出,可以設變數n=0;
每次添加元素n++
每次刪除元素n--
當n<0後n>N數據溢出