資料結構(C實現)------- 鏈隊列,-------隊列

來源:互聯網
上載者:User

資料結構(C實現)------- 鏈隊列,-------隊列

      鏈隊列,即隊列的鏈式儲存結構,它是僅在表頭刪除和表尾插入的單鏈表,因此一個鏈隊列需要設定兩個分別指示隊頭元素和隊尾元素的指標,為了操作方便,給鏈隊列添加一個頭結點,並令隊頭指標指向頭結點,由此,空的鏈隊列的判斷條件就是隊頭指標和隊尾指標均指向頭結點。

鏈隊列的類型描述:

//鏈隊列類型描述typedef int QElemType;typedef struct node{QElemType data;struct node *next;}QNode,*QueuePtr;typedef struct{QueuePtr front; //隊頭指標QueuePtr rear;  //隊尾指標}LinkQueue;

基本操作:  1. 鏈隊列的初始化(帶頭結點)Init_LinkQueue(LinkQueue *Q)

//鏈隊列的初始化(帶頭結點)void Init_LinkQueue(LinkQueue *Q){    QueuePtr head = (QueuePtr)malloc(sizeof(QNode));    if(!head)exit(OVERFLOW);    head->next = NULL;Q->front = Q->rear = head;}

  2. 銷毀鏈隊列Destroy_LinkQueue(LinkQueue *Q)

//銷毀鏈隊列void Destroy_LinkQueue(LinkQueue *Q){//從頭結點開始釋放鏈隊列中所有的結點while(Q->front){Q->rear = Q->front->next;free(Q->front);Q->front = Q->rear;}}

  3. 清空鏈隊列Clear_LinkQueue(LinkQueue *Q)

//清空鏈隊列void Clear_LinkQueue(LinkQueue *Q){Destroy_LinkQueue(Q);Init_LinkQueue(Q);}

        4. 判斷鏈隊列是否為空白IsEmpty_LinkQueue(LinkQueue *Q)

//判斷鏈隊列是否為空白int IsEmpty_LinkQueue(LinkQueue *Q){return Q->front == Q->rear;}

        5. 求鏈隊列的長度GetLength_LinkQueue(LinkQueue *Q)

//求鏈隊列的長度int GetLength_LinkQueue(LinkQueue *Q){int count = 0;//指向存放資料的第一個結點QueuePtr p = Q->front->next;while(p){count++;p = p->next;}return count;}

       6. 取得鏈隊列的頭部元素GetHead_LinkQueue(LinkQueue *Q,QElemType *x)

//取得鏈隊列的頭部元素void GetHead_LinkQueue(LinkQueue *Q,QElemType *x){if(IsEmpty_LinkQueue(Q)){printf("鏈隊列為空白!\n");exit(0);}else{*x = Q->front->next->data;}}

        7. 取得鏈隊尾的頭部元素GetRear_LinkQueue(LinkQueue *Q,QElemType *x)

//取得鏈隊尾的頭部元素void GetRear_LinkQueue(LinkQueue *Q,QElemType *x){if(IsEmpty_LinkQueue(Q)){printf("鏈隊列為空白!\n");exit(0);}else{*x = Q->rear->data;}}

        8.  入鏈隊列En_LinkQueue(LinkQueue *Q,QElemType x)

//入鏈隊列void En_LinkQueue(LinkQueue *Q,QElemType x){QueuePtr q = (QueuePtr)malloc(sizeof(QNode));if(!q)exit(OVERFLOW);q->data = x;q->next = NULL;Q->rear->next = q;Q->rear = q;}

       9.  出鏈隊列De_LinkQueue(LinkQueue *Q,QElemType *x)

//出鏈隊列void De_LinkQueue(LinkQueue *Q,QElemType *x){QueuePtr q;if(IsEmpty_LinkQueue(Q)){printf("鏈隊列為空白!\n");exit(0);}else{*x = Q->front->next->data;q = Q->front->next;*x = q->data;Q->front->next = q->next;//刪除元素後隊列為空白if(q->next == NULL)Q->rear = Q->front;free(q);}}

        10.  輸出鏈隊列Print_LinkQueue(LinkQueue *Q)

//輸出鏈隊列void Print_LinkQueue(LinkQueue *Q){//p指向頭結點的下一個結點,即存放資料的第一個結點QueuePtr p = Q->front->next;if(IsEmpty_LinkQueue(Q)){printf("鏈隊列為空白!\n");exit(0);}else{while(p){printf("%d\t",p->data);p = p->next;}printf("\n");} }

       




資料結構——鏈式隊列,,,,用C語言實現

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
int ifnull(Node *tail)
{
if(tail->next==NULL)
return 0;
return 1;
}
Node *qinit(Node *tail)
{
tail=(Node *)malloc(sizeof(Node));
tail->data=0;
tail->next=NULL;
return tail;
}
void push(Node *tail,int data)
{
if(tail==NULL)
{
perror("error!");
return;
}
Node *tmp=NULL;
if(ifnull(tail)==0)
tmp=tail;
else
tmp=(Node *)malloc(sizeof(Node));
tmp->data=data;
tmp->next=tail->next;
tail->next=tmp;
tail=tmp;
}
void pop(Node *tail)
{
if(tail==NULL)
{
perror("error!");
return;
}
if(ifnull(tail)==0)
printf("queue is empty!");
if(tail->next==tail)
{
free(tail);
tail=NULL;
return;
}
Node *tmp=tail->next;
while(1)
{
if(tmp->next==tail)
{
Node *n=tail;
tail=tmp;
tail->next=n->next;
free(n);
break;
}
tmp=tmp->next;
}
}
Node *getdata(Node *tail,int *data)
{
if(tail==NULL)
{
perror("error!");
return;
}
if(ifnull(tail)==0)
{
printf("queue is empty!");
return;
}
if(tail->next==tail)
{
*data=tail->data;
tail->data=0;
tail->next=NULL;
return tail;
}
Node *tmp=tail->next;
while(1)
{
if(tmp->next==tail)
{
Node *n=tail->next;
*data=tail->data;
free(tail);
tail=tmp;
tail->next=n;
break;
}
tmp=tmp->next;
}
return tail;......餘下全文>>
 
資料結構中的鏈式隊列的基本操作的實現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) // 輸出隊......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.