1. 在循環隊列中怎樣實現入隊和出隊操作 數據結構 c語言
有個比較死板的經驗:把所有需要操作的變數改成指針SqQueue &Q改成SqQueue *Q,比如把所有的.改成->(因為用C語言需要指針操作);例如
int InitQueue(SqQueue *Q)
{
Q->base=(char *)malloc(MAXSIZE*sizeof(Elemtype));
if(Q->base==NULL) exit(0);
Q->front=Q->rear=0;
return 1;
} //初始化一個循環隊列;
2. 數據結構(使用C語言)隊列
對順序循環隊列,常規的設計方法是使用隊尾指針和隊頭指針,隊尾指針用於指出當前胡隊尾位置下標,隊頭指針用於指示當前隊頭位置下標。現要求:
(1)設計一個使用隊頭指針和計數器胡順序循環循環隊列抽象數據類型,其中包括:初始化,入隊列,出隊列,取隊頭元素肯判斷隊列是否非空;
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#include"conio.h"
#defineMAX80
typedefstruct
{
intdata[MAX];
intfront,rear;
intnum;
}SeQue;
SeQue*Init_SeQue()
{
SeQue*s;
s=newSeQue;
s->front=s->rear=MAX-1;
s->num=0;
returns;
}
intEmpty_SeQue(SeQue*s)
{
if(s->num==0)
return1;
else
return0;
}
intIn_SeQue(SeQue*s,intx)
{
if(s->num==MAX)
return0;
else
{
s->rear=(s->rear+1)%MAX;
s->data[s->rear]=x;
s->num++;
return1;
}
}
intOut_SeQue(SeQue*s,int*x)
{
if(Empty_SeQue(s))
return0;
else
{
s->front=(s->front+1)%MAX;
*x=s->data[s->front];
s->num--;
return1;
}
}
voidPrint_SeQue(SeQue*s)
{
inti,n;
i=(s->front+1)%MAX;
n=s->num;
while(n>0)
{printf("%d",s->data[i]);
i=(i+1)%MAX;
n--;
}
}
voidmain()
{
SeQue*s;
intk,flag,x;
s=Init_SeQue();
do{
printf("\");
printf("\t\t\t循環順序隊列");
printf("\t\t\t***********************");
printf("\t\t\t**1-入隊**");
printf("\t\t\t**2-出隊**");
printf("\t\t\t**3-判隊空**");
printf("\t\t\t**4-隊列顯示**");
printf("\t\t\t**0-返回**");
printf("\t\t\t***********************");
printf("\t\t\t請輸入菜單項(0-4):");
scanf("%d",&k);
switch(k)
{
case1:
printf("請輸入入隊元素:");
scanf("%d",&x);
flag=In_SeQue(s,x);
if(flag==0)
printf("隊滿不能入隊!按任意鍵返回..");
else
printf("元素已入隊!按任意鍵返回..");
getch();
system("cls");
break;
case2:
flag=Out_SeQue(s,&x);
if(flag==0)
printf("隊列空出隊失敗!按任意鍵返回..");
else
printf("隊列頭元素已出隊~!按任意鍵返回..");
getch();
system("cls");
break;
case3:
flag=Empty_SeQue(s);
if(flag==1)
printf("該隊列為空!按任意鍵返回..");
else
printf("該隊列不為空!按任意鍵返回..");
getch();
system("cls");
break;
case4:
printf("該隊列元素為:");
Print_SeQue(s);
printf("按任意鍵返回..");
getch();
system("cls");
break;
}
}while(k!=0);
}
3. 計算機c語言中什麼是循環對列
隊列是一種數據結構,屬於一種特殊的線性表,具有先進先出的特性,由front和rear兩個指針分別指向隊頭和隊尾,類似於一個隧道。循環隊列是一種特殊的隊列,它的front=rear,即隊頭和隊尾相連構成循環,因而叫循環隊列。
4. c語言循環隊列
隊列是一種特殊的線性表,循環隊列是將向量空間想像為一個首尾相接的圓環。
隊列是一個特殊的線性表,它的特殊之處在於它只允許表的前面的操作刪除,而在表的後面的操作插入,就像堆棧一樣,隊列100是一個線性表,具有有限的操作。
循環隊列就是把向量空間想像成一個首尾相連的環,把這樣的向量稱為循環向量。存儲學位的隊列稱為循環隊列。
在順序隊列中,當指向隊列末端的指針到達數組的上界時,不能有更多的隊列條目,但數組中仍然有一個空位置。這稱為「假溢出」。
(4)循環隊列數據結構c語言擴展閱讀:
判斷滿隊列狀態:
1.計數;你通常使用count
Count等於隊列的MAXSIZE
2.國旗int
Queueinflag=1Queueoutflag=0
= && flag = = 0的前面和後面
3.放一個存儲應答單元為空,不存儲數據
後面+1=前面
註:(不)順序結構,SeqQueuemyQueue;
5. 數據結構循環隊列(C語言實現)
這是約瑟夫問題,參見
http://ke..com/link?url=T1pJ7-
6. c語言數據結構循環隊列問題
主要錯在InitQueue函數裡面。當聲明一個指針的時候,除了指針本身佔用的內存以外,是不會分配具體的內存空間的。也就是說,如果只是CircQueue *q;聲明指針q,然後直接使用它的內部成員q->front,q->rear = 0是不合法的。實際上,在Visual Studio裡面是編譯不通過的。
修改後運行截圖
CircQueue*InitQueue(){
CircQueue*q=(CircQueue*)malloc(sizeof(CircQueue));
q->front=0;
q->rear=0;
returnq;
}
7. c語言數據結構(循環隊列,用順序結構)
c++
//bc_queue.h
class Queue {
public:
virtual BOOLEAN is_empty(void)=0;
virtual BOOLEAN is_que_full(void)=0;
virtual void build_que(DATA_TYPE str[])=0;
virtual void add_que(DATA_TYPE)=0;
virtual DATA_TYPE del_from_que(void)=0;
virtual int get_que_siz(void)=0;
virtual void print_que(void)=0;
};
//Ary_Circ_Que.h
class Array_Circ_Queue:public Queue{
private:
int QUEUE_SIZ;
int front_of_queue, rear_of_queue;
DATA_TYPE *circ_queue;
void init_ary_circ_que(void);
public:
Array_Circ_Queue(int que_siz);
~Array_Circ_Queue();
BOOLEAN is_empty(void);
BOOLEAN is_que_full(void);
void build_que(DATA_TYPE str[]);
void add_que(DATA_TYPE); //put
DATA_TYPE del_from_que(void); //get
inline int get_que_siz(){return (QUEUE_SIZ);}
void print_que(void);
};
//Ary_Circ_Que.cpp
//
#include<stdio.h>
typedef int BOOLEAN;
const int UNDERFLOW=-1;
typedef char DATA_TYPE;
#include "bc_queue.h"
#include "Ary_Circ_Que.h"
Array_Circ_Queue::Array_Circ_Queue(int que_siz)
{
circ_queue=new DATA_TYPE[QUEUE_SIZ=que_siz];
init_ary_circ_que();
}
Array_Circ_Queue::~Array_Circ_Queue()
{
delete []circ_queue;
}
void Array_Circ_Queue::init_ary_circ_que(void)
{
front_of_queue=QUEUE_SIZ;
rear_of_queue=QUEUE_SIZ;//maximum size
}
BOOLEAN Array_Circ_Queue::is_empty(void)
{
return ((front_of_queue==QUEUE_SIZ)&&(rear_of_queue==QUEUE_SIZ));
}
BOOLEAN Array_Circ_Queue::is_que_full(void)
{
return (((rear_of_queue==0)&&(front_of_queue==QUEUE_SIZ))||(rear_of_queue==(front_of_queue+1)));
}
void Array_Circ_Queue::add_que(DATA_TYPE new_data)
{
if(!is_que_full()){
if(rear_of_queue<=0)
rear_of_queue=QUEUE_SIZ;
if(is_que_empty())
--front_of_queue;
circ_queue[--rear_of_queue]=new_data;
}
else
printf("\n add_que: queue overflow\n");
}
DATA_TYPE Array_Circ_Queue::del_from_que(void)
{
if(!is_que_empty()){
if(front_of_queue<0)
front_of_queue=QUEUE_SIZ;
return (circ_queue[front_of_queue--]);
}
else
{
printf("\n del_que: %s ","queue underflow"):
return (UNDERFLOW);
}
}
void Array_Circ_Queue::build_que(DATA_TYPE str[])
{
if(str[0]=='\0')
printf("\n build_que:empty string \n");
else
for(int j=0;str[j]!='\0';++j)
add_que(str[j]);
}
void Array_Circ_Queue::print_que(void)
{
if(is_que_empty()){
if((rear_of_queue<front_of_queue)&&(rear_of_queue>=0)){
printf(" c_queue[%i]=%c <-queue front\n ",front_of_queue,circ_queue[front_of_queue]);
for(int i=front_of_queue-1;i>=0;i--)
printf(" c_queue[%i]=%c \n",i,circ_queue[i]);
for(i=QUEUE_SIZ-1;i<=rear_of_queue;i--)
printf(" c_queue[%i]=%c \n",i,circ_queue[i]);
}
else{
//case:rear>0&&front<=rear
if(rear_of_queue>0)
printf(" c_queue[%i]=%c <-queue front\n ",front_of_queue,circ_queue[front_of_queue]);
for(int i=front_of_queue-1;(i>=0&&i<=rear_of_queue);i--)
printf(" c_queue[%i]=%c \n",i,circ_queue[i]);
}
else
printf("\n no element in queue.\n");
}
8. 二級c語言,隊列、循環隊列是什麼
隊列是一種特殊的線性表,循環隊列是將向量空間想像為一個首尾相接的圓環。
1、隊列是一種特殊的線性表,特殊之處在於它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作,和棧一樣,隊列是一種操作受限制的線性表。
2、循環隊列是將向量空間想像為一個首尾相接的圓環,並稱這種向量為循環向量。存儲在其中的隊列稱為循環隊列。在順序隊列中,當隊尾指針已經到數組的上界,不能再有入隊操作,但其實數組中還有空位置,這就叫做「假溢出」,解決假溢出的途徑----採用循環隊列。
(8)循環隊列數據結構c語言擴展閱讀
判斷隊列滿的情況:
1、count來計數;通常使用count
Count等於隊列的MAXSIZE
2、Flag標志 int
入隊列 flag=1 出隊列flag=0
Front=rear&&flag==0
3、把一個存儲單元空出來,不存放數據
Rear+1==front
注意事項:(不要) 順序結構,SeqQueue myQueue;
9. C語言 數據結構 循環隊列插入操作
#include<stdio.h>
#include<malloc.h>
struct link_cqueue
{
int data;
struct link_cqueue *next;
};
//初始化循環鏈隊列
struct link_cqueue *init_link_cqueue()
{
struct link_cqueue *rear;
rear=NULL; /*隊尾指針設置為空*/
return rear;
}
//(1)插入(即入隊)演算法:
struct link_cqueue *EnCQueue(struct link_cqueue *rear, int x)
{ //設循環鏈隊列的隊尾指針為rear,x為待插入的元素
struct link_cqueue *p;
p=(struct link_cqueue *)malloc(sizeof(struct link_cqueue));
p->data=x;
if(rear==NULL) //如為空隊,建立循環鏈隊列的第一個結點
{
rear=p;
rear->next=p; //鏈接成循環鏈表
}
else //否則在隊尾插入p結點
{
p->next=rear->next;
rear->next=p;
rear=p;
}
return rear;
}
//(2)刪除(即出隊)演算法:
struct link_cqueue *DeCQueue(struct link_cqueue *rear)
{ //設循環鏈隊列的隊尾指針為rear
if (rear==NULL) //空隊
printf("隊列為空無法刪除!\n");
else if(rear->next==rear) //隊中只有一個結點
rear=NULL;
else
rear->next=rear->next->next; //rear->next指向的結點為循環鏈隊列的隊頭結點
return rear;
}
//循環隊列的輸出
void print_link_cqueue(struct link_cqueue *rear)
{
struct link_cqueue *p;
if(!rear)
printf("隊列為空!\n");
else
{
printf("%5d",rear->next->data);
p=rear->next;
while(p!=rear)
{
printf("%5d",p->next->data);
p=p->next;
}
}
printf("\n");
}
main()
{
struct link_cqueue *rear;
int x;
int c;
rear=init_link_cqueue();
do
{
printf("請選擇入隊或出隊操作:1:入隊;2:出隊;3:輸出!\n");
scanf("%d",&c);
if(c==1)
{
printf("請輸入要入隊的元素:");
scanf("%d",&x);
rear=EnCQueue(rear,x);
}
else if(c==2)
{
rear=DeCQueue(rear);
}
else if(c==3)
print_link_cqueue(rear);
else
printf("選擇錯誤,請重新選擇");
}while(1);
}
10. 數據結構如何通過C語言來實現,請舉例說明,盡可能詳細
數據的結構無非就是表:線性表、鏈表,棧,隊列,串,數組,樹、二叉樹,圖,這幾種。
常用的使用指針,或數組建立數據結構,然後對其進行插入、刪除、查找、排序等操作。
以下是C語言實現的循環隊列:
#include<stdio.h>
#include<stdlib.h>
#define MAX_QSIZE 5
struct SqQueue
{ QElemType *base; // 初始化的動態分配存儲空間
int front; // 頭指針,若隊列不空,指向隊列頭元素
int rear; // 尾指針,若隊列不空,指向隊列尾元素的下一個位置
};
// bo3-4.cpp 循環隊列(存儲結構由c3-3.h定義)的基本操作(9個)
void InitQueue(SqQueue &Q)
{ // 構造一個空隊列Q。在教科書第64頁
Q.base=(QElemType*)malloc(MAX_QSIZE*sizeof(QElemType));
if(!Q.base) // 存儲分配失敗
exit(OVERFLOW);
Q.front=Q.rear=0;
}
void DestroyQueue(SqQueue &Q)
{ // 銷毀隊列Q,Q不再存在
if(Q.base) // 隊列Q存在
free(Q.base); // 釋放Q.base所指的存儲空間
Q.base=NULL; // Q.base不指向任何存儲單元
Q.front=Q.rear=0;
}
void ClearQueue(SqQueue &Q)
{ // 將隊列Q清為空隊列
Q.front=Q.rear=0;
}
int QueueEmpty(SqQueue Q)
{ // 若隊列Q為空隊列,則返回TRUE;否則返回FALSE
if(Q.front==Q.rear) // 隊列空的標志
return TRUE;
else
return FALSE;
}
int GetHead(SqQueue Q,QElemType &e)
{ // 若隊列Q不空,則用e返回Q的隊頭元素,並返回OK;否則返回ERROR
if(Q.front==Q.rear) // 隊列空
return ERROR;
e=Q.base[Q.front]; // 將隊頭元素的值賦給e
return OK;
}
int EnQueue(SqQueue &Q,QElemType e)
{ // 插入元素e為隊列Q的新的隊尾元素。在教科書第65頁
if((Q.rear+1)%MAX_QSIZE==Q.front) // 隊列滿
return ERROR;
Q.base[Q.rear]=e; // 將e插在隊尾
Q.rear=(Q.rear+1)%MAX_QSIZE; // 隊尾指針+1後對MAX_QSIZE取余
return OK;
}
int QueueLength(SqQueue Q)
{ // 返回隊列Q的元素個數,即隊列的長度。在教科書第64頁
return(Q.rear-Q.front+MAX_QSIZE)%MAX_QSIZE;
}
int DeQueue(SqQueue &Q,QElemType &e) // 在教科書第65頁
{ // 若隊列Q不空,則刪除Q的隊頭元素,用e返回其值,並返回OK;否則返回ERROR
if(Q.front==Q.rear) // 隊列空
return ERROR;
e=Q.base[Q.front]; // 將隊頭元素的值賦給e
Q.front=(Q.front+1)%MAX_QSIZE; // 移動隊頭指針
return OK;
}
void QueueTraverse(SqQueue Q,void(*visit)(QElemType))
{ // 從隊頭到隊尾依次對隊列Q中每個元素調用函數visit()
int i=Q.front; // i最初指向隊頭元素
while(i!=Q.rear) // i指向隊列Q中的元素
{ visit(Q.base[i]); // 對i所指元素調用函數visit()
i=(i+1)%MAX_QSIZE; // i指向下一個元素
}
printf("\n");
}
void main()
{
int j;
int i=0,m;
int d;
SqQueue Q;
InitQueue(Q); // 初始化隊列Q,失敗則退出
printf("初始化隊列後,隊列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
printf("請輸入整型隊列元素(不超過%d個),-1為提前結束符:",MAX_QSIZE-1);
do
{ scanf("%d",&d); // 由鍵盤輸入整型隊列元素
if(d==-1) // 輸入的是提前結束符
break; // 退出輸入數據循環
i++; // 計數器+1
EnQueue(Q,d); // 入隊輸入的元素
}while(i<MAX_QSIZE-1); // 隊列元素的個數不超過允許的范圍
printf("隊列長度為%d,",QueueLength(Q));
printf("現在隊列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
printf("連續%d次由隊頭刪除元素,隊尾插入元素:\n",MAX_QSIZE);
for(m=1;m<=MAX_QSIZE;m++)
{ DeQueue(Q,d); // 刪除隊頭元素,其值賦給d
printf("刪除的元素是%d,請輸入待插入的元素:",d);
scanf("%d",&d); // 輸入要入隊的元素給d
EnQueue(Q,d); // 將d入隊
}
m=QueueLength(Q); // m為隊列Q的長度
printf("現在隊列中的元素為");
QueueTraverse(Q,print); // 從隊頭到隊尾依次對隊列Q的每個元素調用函數print()
printf("共向隊尾插入了%d個元素。",i+MAX_QSIZE);
if(m-2>0)
printf("現在由隊頭刪除%d個元素,",m-2);
while(QueueLength(Q)>2)
{ DeQueue(Q,d); // 刪除隊頭元素,其值賦給d
printf("刪除的元素值為%d,",d);
}
j=GetHead(Q,d); // 將隊頭元素賦給d
if(j) // 隊列Q不空
printf("現在隊頭元素為%d\n",d);
ClearQueue(Q); // 清空隊列Q
printf("清空隊列後,隊列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
DestroyQueue(Q); // 銷毀隊列Q
}