本文目錄一覽:
- 1、C語言隊列
- 2、隊列的源代碼(c語言)
- 3、用C語言編寫隊列程序
- 4、c語言隊列操作
- 5、隊列(c語言代碼)
- 6、求一段C語言的 隊列 數據結構代碼,能用的就加分啊!
C語言隊列
C語言的隊列(queue),是指先進先出(FIFO, First-In-First-Out)的線性表。在具體應用中通常用鏈表或者數組來實現。隊列只允許在後端(稱為rear)進行插入操作,在前端(稱為front)進行刪除操作
單鏈表形式(單鏈隊列使用鏈表作為基本數據結果,因此不存在偽溢出的問題,隊列長度也沒有限制。但插入和讀取的時間代價會比較高)
隊列的源代碼(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++;
coutsetw(2)bsetw(2)” 進入隊列 “endl;
}
void Queue::pop()
{
Node *p;
p=head-next;
cout” “setw(2)p-asetw(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)
{
coutp-a” – “;
p=p-next;
}
cout”NULL”endl;
}
Queue::Queue()
{
rear=head;
};
用C語言編寫隊列程序
#includestdio.h
#includestdlib.h
#includemalloc.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);
c語言隊列操作
pq-rear-next
=
pnew這個代碼從隊列的尾部增加新節點,
然後pq-rear
=
pnew更新隊列尾部指針。隊列的數據結構形式就是由一個頭front指針,一個尾rear指針來表徵,items的設計是用空間換時間,涉及隊列大小的操作會非常方便。
隊列的特徵是先進先出,你給出的鏈式實現,其實就跟一個鏈表一樣,鏈表的添加刪除如果能理解了,隊列只是鏈表的元素增加/刪除
按先進先出特點的一種實現。
但對於隊列來說,實現方式不是重點,先進先出的性質才是重點,這在實際應用中很多,比如排隊叫號。
隊列(c語言代碼)
隊頭是front,隊尾是rear;
隊頭出數據,隊尾進數據;
隊頭指針front 不存數據;
當front == rear時 隊列為空;
清空或取出數據至隊列為空時記得將rear指針移到front上;
求一段C語言的 隊列 數據結構代碼,能用的就加分啊!
#ifndef QUEUE_H
#define QUEUE_H
#include iostream
using std::cout;
using std::endl;
using std::ostream;
template class Type
class Queue {
friend ostream operator (ostream , const QueueType );
public:
static const int DefaultSize;
Queue(int = DefaultSize); //創建一個最大容量為MaxQueueSize的空隊列
Queue(Type [], int, int = DefaultSize);
~Queue() {delete [] queue;}
bool IsFull(); //若元素個數等於隊列的最大容量則返回true,否則返回false
void Add(const Type ); //若IsFull()為true,調用外處理函數QueueFull(),否則將item插入隊尾
bool IsEmpty() const; //若隊列中元素個數等於0,則返回true,否則返回false
Type * Delete(Type ); //若IsEmpty()為true,調用QueueEmpty()並返回0,否則刪除隊列前段元素並返回其指針
void Empty(); //清空隊列
void QueueFull(); //擴充1倍的存儲空間
void QueueEmpty(); //提示隊列已空,元素不能出隊
private:
int front, rear;
Type * queue;
int maxSize;
};
template class Type
const int QueueType::DefaultSize = 10;
template class Type
QueueType::Queue(int pMaxSize) {
queue = new Type [pMaxSize];
maxSize = pMaxSize;
front = rear = -1;
}
template class Type
QueueType::Queue(Type pArray[], int pSize, int pMaxSize) {
queue = new Type [pMaxSize];
for (int i = 0; i pSize; i++)
{
queue[i] = pArray[i];
}
front = -1; rear = pSize – 1;
maxSize = pMaxSize;
}
template class Type
bool QueueType::IsFull() {
if ( rear == maxSize – 1 ) return true;
else return false;
}
template class Type
bool QueueType::IsEmpty() const {
if ( rear == front ) return true;
else return false;
}
template class Type
void QueueType::Add(const Type pX) {
if (IsFull())
{
QueueFull();
queue[++rear] = pX;
}
else
{
queue[++rear] = pX;
}
}
template class Type
Type * QueueType::Delete(Type pX) {
if (IsEmpty())
{
QueueEmpty();
return 0;
}
else
{
pX = queue[++front];
return pX;
}
}
template class Type
void QueueType::QueueEmpty() {
cout “隊列為空,不能進行出隊操作!” endl;
}
template class Type
void QueueType::QueueFull() {
Type * newQueue = new Type [maxSize * 2];
for ( int i = front + 1; i = rear; i++ )
{
newQueue[i] = queue[i];
}
maxSize = maxSize * 2;
delete [] queue;
queue = newQueue;
cout “隊列已滿,現已增加空間為原來的2倍 ” maxSize;
}
template class Type
void QueueType::Empty() {
front = rear = -1;
}
template class Type
ostream operator (ostream pOutput, const QueueType pQ) {
if (pQ.IsEmpty())
{
cout “空隊列” endl;
return pOutput;
}
for ( int i = pQ.front + 1; i = pQ.rear; i++ )
{
pOutput pQ.queue[i] ” “;
}
cout endl;
return pOutput;
}
#endif
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238401.html