當前位置:首頁 » 編程語言 » c語言實現鏈隊列的基本操作
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言實現鏈隊列的基本操作

發布時間: 2022-08-09 22:30:23

Ⅰ 求c語言實現鏈隊列的建立和出入隊列


#include<stdio.h>
#include<stdlib.h>

structNode{
intdata;//數據類型自己根據需要定義
Node*next;
};

structQueue{
Node*head,*rear;
};

Queue*creatQueue(){
Queue*Q=newQueue;
Node*node=(Node*)malloc(sizeof(Node));//創建頭節點
node->next=NULL;
Q->head=Q->rear=node;
returnQ;
}

voidenQueue(Queue*Q,intd){
Node*node=(Node*)malloc(sizeof(Node));
node->data=d;
node->next=NULL;
Q->rear->next=node;
Q->rear=node;
}

intdeQueue(Queue*Q){
intdata;
if(Q->head==Q->rear){
printf("隊列下溢!");
return99999999;//表示錯誤
}
Node*node=Q->head->next;
data=node->data;
Q->head->next=node->next;
if(node->next==NULL){
Q->rear=Q->head;
}
free(node);
returndata;
}

voidprintQueue(Queue*Q){
Node*node=Q->head->next;
puts("Queue:");
while(node!=NULL){
printf("%d",node->data);
node=node->next;
}
putchar(' ');
}

voiddestroyQueue(Queue*Q){//回收內存
Node*node;
while(Q->head){
node=Q->head->next;
free(Q->head);
Q->head=node;
}
free(Q);
}

voidmain(){
intd,run=1;
Queue*Q=creatQueue();
while(run){
system("cls");
printQueue(Q);
puts("1、入隊 2、出隊 3、退出 請輸入操作號:");
scanf("%d",&d);
switch(d){
case1:
puts("請輸入需要入隊的數字:");
scanf("%d",&d);
enQueue(Q,d);
break;
case2:
printf("%d出隊 ",deQueue(Q));
break;
case3:
run=0;
break;
default:
puts("輸入錯誤! ");
}
system("pause");
}
destroyQueue(Q);
}

Ⅱ 數據結構(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語言結構體分析與定義

鏈式隊列

鏈式存儲結構的隊列稱作鏈式隊列。

鏈式隊列的隊頭指針指在隊列的當前隊頭結點位置,隊尾指針指在隊列的當前隊尾結點位置。不帶頭結點的鏈式隊列時可直接刪除隊頭指針所指的結點。

鏈式隊列中結點的結構體可定義如下:

typedef struct qnode

{

DataType datal;

Struct qnode *next;

}LQNode;

為了方便參數調用,通常把鏈式隊列的隊頭指針front和隊尾指針rear也定義為如下的結構體類型LQueue:

typedef struct

{

LQNode *front;

LQNode *rear;

}LQueue;

鏈式隊列操作的實現

(1) 初始化QueueInitiate(LQueue *Q)

void QueueInitiate(LQueue *Q)

{

Q->rear=NULL;

Q->front=NULL;

}

(2)非空否QueueNotEmpty(LQueue Q)

int QueueNotEmpty(LQueue Q)

/*判斷鏈式隊列Q非空否,非空返回1,否則返回0*/

{

if(Q.front==NULL)return 0;

else return 1;

}

(3)入隊列QueueAppend(LQueue *Q,DataType x)

int QueueAppend(LQueue *Q,DataType x)

/*把數據元素x插入鏈式隊列Q隊列的隊尾,入隊列成功返回1,否則返回0*/

{

LQNode *p;

if((p=(LQNode*)malloc(sizeof(LQNode)))==NULL)

{

printf(「內存不足無法插入!\n);

return 0;

}

p->data=x;

p->next=NULL;

if(Q->rear!=NULL)Q->rear->next=p;

Q->rear=p;

if(Q->front==NULL)Q->front=p;

return 1;

}

(4)出隊列QueueDelete(LQueue *Q,DataType *d)

int QueueDelete(LQueue *Q,DataType *d)

/*刪除鏈式隊列Q的隊頭數據元素值到d,出隊列成功返回1,否則返回0*/

{

LQNode *p;

if(Q->front==NULL)

{

printf(「隊列已空無數據元素出隊列!\n」);

return 0;

}

else

{

*d=Q->front->data;

p=Q->front;

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

if(Q->front==NULL)Q->rear=NULL;

free(p);

return 1;

}

}

(5)取隊頭數據元素QueueGet(LQueue *Q,DataType *d)

int QueueGet(LQueue *Q,DataType *d)

/*取鏈式隊列Q的當前隊頭數據元素值到d,成功返回1,否則返回0*/

{

if(Q.front==NULL)

{

printf(「隊列已空無數據元素出隊列!\n);

return 0;

}

else

{

*d=Q.front->data;

return 1;

}

}

(6)撤銷動態申請空間Destory(LQueue *head)

int Destory(LQueue *head)

{

LQNode *p,*p1;

p=Q.front;

while(p!=NULL)

{

p1=p;

p=p->next;

free(p1);

}

}
幫你轉的,我覺得他描述的很清楚。希望對你有幫助。

Ⅳ 誰做過c語言的 隊列及其操作啊

我自己寫了一個,練練手
由於沒有指定數據類型,在第三行處我用float類型了
如果改成其它類型,代碼中有//Type處,涉及輸入輸出,也要相應的改一下
這個代碼只做了簡單的測試,不保證正確性

#include<stdio.h>
#include<malloc.h>
#defineTypefloat
structnode{
Typedata;
structnode*next;
};
structqueue{
structnode*front,*rear;
};
structnode*push(Typet,structqueue*q)
{
structnode*last;
last=(structnode*)malloc(sizeof(structnode));
last->data=t;
last->next=NULL;
if(q->rear!=NULL){
q->rear->next=last;
q->rear=last;
}else{
q->front=last;
q->rear=last;
}
returnlast;
}
Typepop(structqueue*q)
{
structnodend;
if(q->front==NULL){
printf("<<<Error:queueisblank ");
return0;
}
nd=*(q->front);
free(q->front);
if(q->front!=q->rear)
q->front=nd.next;
else{
q->front=NULL;
q->rear=NULL;
}
returnnd.data;
}
voidclear(structqueue*q)
{
structnode*np,*np1;
if(q->front!=NULL){
np=q->front;
while(1){
np1=np;
free(np);
if(np==q->rear)
break;
np=np1->next;
}
q->front=NULL;
q->rear=NULL;
}
printf("<<<Thequeuehasbeencleared ");
}
structqueue*init()
{
structqueue*q=(structqueue*)malloc(sizeof(structqueue));
q->front=NULL;
q->rear=NULL;
returnq;
}
voiddisplay(structqueue*q)
{
structnode*np;
np=q->front;
if(np==NULL){
printf("<<<Thequeueisblank ");
return;
}
while(1){
printf(">>>%f ",np->data);//Type
if(np==q->rear)
break;
np=np->next;
}
}
intmain()
{
structqueue*q;
unsignedintc;
Typet;
q=init();
printf("<<<Oneblankqueuehasbeencreated ");
while(1){
printf("Menu:
*******************************************************
1:pushanelementtoqueue
2:popanelementfromqueue
3:clearqueue
4:displayqueue
5:exit
*******************************************************
Pleaseselectoperator ");
scanf("%d",&c);
switch(c){
case1:
printf(": ");
scanf("%f",&t);//Type
push(t,q);
break;
case2:
printf(">>>Theelementpopedfromqueueis%f ",pop(q));//Type
break;
case3:
clear(q);
break;
case4:
display(q);
break;
case5:
clear(q);
free(q);
break;
default:
printf("Invalidinput ");
break;
}
if(c==5)
break;
}
return0;
}

Ⅳ 建立循環鏈隊列,並在循環鏈隊列上實現入隊、出隊基本操作

/* 實現循環隊列的基本操作(初始化、判斷隊空、判斷隊滿、入隊、出隊) */ //在javascript中,可以使用數組來實現一個隊列 function stack(){ this.datastore = new Array(); //初始化 this.isEmpty = isEmpty; //判斷隊空 this.isFull = isFull; //判斷隊滿 this.add = add; //入隊 this.remove = remove; //出隊 this.count = 0; function isEmpty(){ if(this.count == 0) return true; return false; } function isFull(){ if(!isEmpty()) return true; return false; } function add(value){ alert(this.count); this.datastore[this.count ++] = value; } function remove(){ if(this.count <= 0) { this.count = 0; alert('當前隊列為空,無法刪除!'); return; } delete this.datastore[-- this.count ]; } }

Ⅵ 數據結構中的鏈式隊列的基本操作的實現C或C++。。幫我找一下下面程序問題的所在。謝謝

#include<iostream>
using namespace std;
#define true 1
#define false 0

typedef struct Node //定義一個隊列
{
int data;
struct Node *next;
}LinkQueueNode;

typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;

void InitQueue(LinkQueue *Q) //初始化一個隊列,把聲明的隊列的對象作為初始化的參數,不能用局部聲明的變數
{
Q->front=Q->rear=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
Q->front->next=NULL;
}

void CreatQueue(LinkQueue *Q) //建立一個隊列
{
int i,n;
LinkQueueNode *s;
cout<<"輸入隊列中的元素的個數n"<<endl;
cin>>n;
cout<<"輸入隊列中的元素"<<endl;
for(i=0;i<n;i++)
{
s=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));//要分配空間
cin>>s->data;
Q->rear->next=s;
Q->rear=s;
//s=s->next; 無用
}
Q->rear->next=NULL;
}

int EnterQueue(LinkQueue *Q,int x) //入隊操作
{
LinkQueueNode *NewNode;
NewNode=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
if(NewNode!=NULL)
{
NewNode->data=x;
NewNode->next=NULL;
Q->rear->next=NewNode;
Q->rear=NewNode;
return(true);
}
else
return(false);
}

void DeleteQueue(LinkQueue *Q) //出對操作
{
int data;
LinkQueueNode *p;
if(Q->front==Q->rear)
{
cout<<"隊列為空"<<endl;

}
else
{
p=Q->front->next;
Q->front->next=p->next;
data=p->data;
free(p);
cout<<data<<endl;
}
}

void Output(LinkQueue *Q) // 輸出隊列中的值
{
LinkQueue *s;
s=Q;
LinkQueueNode *p;
p=s->front->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}

int main()
{
LinkQueue *Q=(LinkQueue*)malloc(sizeof(LinkQueue));
InitQueue(Q);
int aa=1,x;
while(aa)
{
cout<<"建隊列請按1"<<endl;
cout<<"進隊列請按2"<<endl;
cout<<"出隊列請按3"<<endl;
cout<<"輸出請按4"<<endl;
cout<<"退出請按0"<<endl;
cin>>aa;
switch(aa)
{
case 1:
CreatQueue(Q);
break;
case 2:
cout<<"輸入進隊列的值x"<<endl;
cin>>x;
EnterQueue(Q,x);
break;
case 3:
DeleteQueue(Q);
break;
case 4:
Output(Q);
break;
case 0:return(0);
break;
default:aa=0;
break;
}
}
return 0;
}

已經測試,沒的問題

Ⅶ 數據結構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語言隊列操作

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

Ⅸ 用C語言編寫隊列的各種基本操作,我不是非常明白:注釋里有些問題:請大家講講,如果可能,請詳細講講各種

ont)進行刪除操作,而在表的後端(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為新入出元素)。 隊列和棧一樣,有著非常廣泛的應用。

具體網上查:

另外,虛機團上產品團購,超級便宜