當前位置:首頁 » 編程語言 » 隊列c語言

隊列c語言

發布時間: 2022-01-12 13:39:58

A. 用c語言編寫隊列程序

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define NULL 0
#define OK 1
#define OVERFLOW 0
#define ERROR 0
typedef int QElemType;
typedef int Status;
typedef struct QNode
{
QElemType data;
QNode *next;
}*QueuePtr;
struct LinkQueue
{
QueuePtr front,rear;//隊頭,隊尾指針
};
//函數列表
void InitQueue(LinkQueue &Q)
{//初始化一個隊列
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)//生成頭結點失敗
exit(OVERFLOW);
Q.front->next=NULL;
}
void DestoryQueue(LinkQueue &Q)
{ //銷毀隊列
while(Q.front)
{
Q.rear=Q.front->next;//Q.rear指向Q.front的下一個結點
free(Q.front);//釋放Q.front所指結點
Q.front=Q.rear;//Q.front指向Q.front的下一個結點
}
}
void ClearQueue(LinkQueue &Q)
{ //將隊列清為空
DestoryQueue(Q);//銷毀隊列
InitQueue(Q);//重新構造隊列
}
Status QueueEmpty(LinkQueue Q)
{ //判斷隊列是否為空
if(Q.front->next==NULL)
return TRUE;
else return FALSE;
}
int QueueLength(LinkQueue Q)
{ //求隊列的長度
int i=0;//計數器清0
QueuePtr p=Q.front;//p指向結點
while(Q.rear!=p)//p所指向的不是尾結點
{
i++;//計數器加1
p=p->next;
}
return i;
}
Status GetHead(LinkQueue Q,QElemType &e)
{ //若隊列不空,則用e返回隊頭元素
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;//p指向隊頭結點
e=p->data;//將隊頭元素的值賦給e
return OK;
}
void EnQueue(LinkQueue &Q,QElemType e)
{ //插入元素e為隊列Q的新的隊尾元素
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
//動態生成新結點
if(!p)
exit(OVERFLOW);
p->data=e;//將e的值賦給新結點
p->next=NULL;//新結點的指針為空
Q.rear->next=p;//原隊尾結點的指針域為指向新結點
Q.rear=p;//尾指針指向新結點
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{ //若隊列不為空,刪除Q的隊頭元素,用e返回其值
QueuePtr p;
if(Q.front==Q.rear)//隊列為空
return ERROR;
p=Q.front->next;//p指向隊頭結點
e=p->data;//隊頭元素賦給e
Q.front->next=p->next;//頭結點指向下一個結點
if(Q.rear==p)//如果刪除的隊尾結點
Q.rear=Q.front;//修改隊尾指針指向頭結點
free(p);
return OK;
}
void QueueTraverse(LinkQueue Q,void(*visit)(QElemType))
{ //對隊頭到隊尾依次對隊列中每個元素調用函數visit()
QueuePtr p;
p=Q.front->next;
while(p)
{
visit(p->data);//對p所指元素調用visit()
p=p->next;
}
printf("\n");
}
void print(QElemType e)
{
printf("%2d",e);
}
void main()
{
int i,k;
QElemType d;
LinkQueue q;
InitQueue(q);//構造一個空棧
for(i=1;i<=5;i++)
{
EnQueue(q,i);
}
printf("棧的元素為:");
QueueTraverse(q,print);
k=QueueEmpty(q);
printf("判斷棧是否為空,k=%d(1:為空9;0:不為空)\n",k);
printf("將隊頭元素賦給d\n");
k=GetHead(q,d);
printf("隊頭元素為d=%d\n",d);
printf("刪除隊頭元素:\n");
DeQueue(q,d);
k=GetHead(q,d);
printf("刪除後新的隊頭元素為d=%d\n",d);
printf("此時隊列的長度為%d\n",QueueLength(q));
ClearQueue(q);//清空隊列
printf("清空隊列後q.front=%u,q.rear=%u,q.front->next=%u\n",q.front,q.rear,q.front->next);
DestoryQueue(q);
printf("銷毀隊列後,q.front=%u,q.rear=%u\n",q.front,q.rear);

B. 隊列 c語言程序編程

SeqCQueue queue;
初始化
void init(void)
{
for(int i=0 ; i<=MaxSize ; i++)
queue.data[i] = 0;
queue.front = 0;
queue.rear = -1;
}

插入
void insert(int x)
{
queue.rear ++;
queue.data[queue.rear] = x;
}

void delete(void)
{
queue.data[queue.front] = 0;
queue.front ++;
}
自己直接寫的。
main和其他的,自己搞定吧。

C. 隊列的源代碼(c語言)

以前寫的.你可以看看咯..

class Queue
{
struct Node
{
int a;
Node * next;
};
public:
Queue();
void pump(int b);
void pop();
int getlength();
virtual void print();

private:
Node * head;
Node * rear;
};

void Queue::pump(int b)
{
Node *p1=new Node;
p1->a=b;
p1->next=NULL;
rear->next=p1;
rear=p1;
head->a++;
cout<<setw(2)<<b<<setw(2)<<" 進入隊列 "<<endl;
}
void Queue::pop()
{
Node *p;
p=head->next;
cout<<" "<<setw(2)<<p->a<<setw(2)<<"出隊"<<endl;
head->next=p->next;
delete p;
head->a--;
}
int Queue::getlength()
{
return head->a;
}
void Queue::print()
{
Node *p;
p=head->next;
cout<<"隊列中的元素是:"<<endl;
while(p)
{
cout<<p->a<<" -> ";
p=p->next;
}
cout<<"NULL"<<endl;
}

Queue::Queue()
{
rear=head;

};

D. 關於C語言的隊列

可以使用c++中的容器如queue,頭文件在#include <queue>

E. 關於數據結構中隊列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;
}

F. c語言隊列操作

pq->rear->next
=
pnew這個代碼從隊列的尾部增加新節點,
然後pq->rear
=
pnew更新隊列尾部指針。隊列的數據結構形式就是由一個頭front指針,一個尾rear指針來表徵,items的設計是用空間換時間,涉及隊列大小的操作會非常方便。
隊列的特徵是先進先出,你給出的鏈式實現,其實就跟一個鏈表一樣,鏈表的添加刪除如果能理解了,隊列只是鏈表的元素增加/刪除
按先進先出特點的一種實現。
但對於隊列來說,實現方式不是重點,先進先出的性質才是重點,這在實際應用中很多,比如排隊叫號。

G. C語言中使用隊列

如果你用vc,#include<deque>就好了,但是注意要加上using naemspace std;
我是當你用的c++的STL,STL中沒有真正的隊列和棧,他們都是通過對雙端隊列的改造得到的,所以包含的文件可能和你想的不一樣。而且這些頭文件都沒有.h結尾!很特別
如果你不是vc,當我沒說

H. C語言中隊列怎樣創建

隊列是一種特殊的鏈表,在你寫GET和PUT方法時只需要返回鏈表的第一個值,把你要輸的值放鏈表的最後就是隊列了!!!

I. 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數據溢出

熱點內容
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:170
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:100
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:208
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554
雲伺服器的優點與缺點 發布:2024-09-08 06:30:34 瀏覽:734