C语言用数组实现循环队列的入队出队
//定义一个int型数组que,长度为N(常量切大于2).
int que[N];
int rear=0,front=0; //队尾 队头
判断队列已满:
if((front+1)%N==rear%N) //成立则队列已满判断队列为空
if((rear==front)) //成立则队列空入队(一般在入队前判断队列是否已满)
//将val入队que[front++]=val;
front%=N;
出队(一般在出队前判断队列是否为空)
rear=(rear+1)%N;下一个要出队的元素(一般先判断是否为空)
que[rear];Cè¯è¨å车åºç®¡çç³»ç»
/*----------------------------------------------------------------
// Copyright (C) æ²é³å·¥ç¨å¦é¢ä¿¡æ¯å®å ¨å·¥ä½å®¤
// çæææã
//
// æ件åï¼æ¨¡æå车åºé®é¢.cpp
// æ件åè½æè¿°ï¼æ¨¡æå车åºé®é¢
//
//
// å建æ è¯ï¼
//
// ä¿®æ¹æ è¯ï¼
// ä¿®æ¹æè¿°ï¼å®æç¼ç
//----------------------------------------------------------------*/
//头æ件
#include <iostream>
#include <malloc.h>
#include <string>
#include <windows.h>
//常éå®ä¹
#define MAX_STOP 4 //å®ä¹å车åºæ大å车æ°
#define MAX_PLATE //å®ä¹è½¦çå·æ大é¿åº¦
#define TIME_COUNT "ç§" //å®ä¹æ¶é´åä½
#define TIME_MS_TO_CONUT //å®ä¹æ¶é´è¿å¶ï¼æ为ç±TIME_COUNTå°æ¯«ç§çè¿å¶
#define UNIT_PRICE //å®ä¹åä½æ¶é´æ¶è´¹æ å
using namespace std; //使ç¨stdå½å空é´
//æ°æ®ç»æå®ä¹
//å®ä¹åå¨æ±½è½¦ä¿¡æ¯çç»æä½
typedef struct
{
char license_plate[MAX_PLATE]; //汽车çç §å·ç ï¼å®ä¹ä¸ºä¸ä¸ªå符æéç±»å
char state; //汽车å½åç¶æï¼å符p表示åæ¾å¨å车ä½ä¸ï¼å符s表示åæ¾å¨ä¾¿éä¸ï¼æ¯è¾è½¦çåå§ç¶æç¨å符iæ¥è¿è¡è¡¨ç¤º
int time; //汽车åå ¥å车åºæ¶çæ¶é´ï¼ç¨æ¥è®¡æ¶æ¶è´¹
}CAR;
//å®ä¹æ¨¡æå车åºçæ ç»æ
typedef struct
{
CAR STOP[MAX_STOP]; //汽车信æ¯çåå¨ç©ºé´
int top; //ç¨æ¥æ示æ 顶ä½ç½®çéææé
}SeqStack;
//å®ä¹æ¨¡æ便éçéåç»æ
typedef struct node
{
CAR WAIT; //汽车信æ¯çåå¨ç©ºé´
struct node *next; //ç¨æ¥æ示éåä½ç½®çå¨ææé
}QNode; //é¾éåèç¹çç±»å
//å®ä¹é¾éåçæ¶å°¾æé
typedef struct
{
QNode *front,*rear;
}LQueue; //å°å¤´å°¾æéå°è£ å¨ä¸èµ·çé¾é
//å½æ°å£°æ
int Empty_LQueue(LQueue *q); //å¤é空
int LeaveCheck(SeqStack parking , char *license_plate); //æ£æ¥ç¦»å¼ç车æ¯å¦å¨å车åºä¸
int QueueLength(LQueue *q); //å¤éé¿åº¦
int Out_LQueue(LQueue *&sidewalk , char *license_plate); //åºéæä½
int StackEmpty(SeqStack parking); //å¤ææ æ¯å¦ä¸ºç©º
int StackFull(SeqStack parking); //å¤ææ æ¯å¦ä¸ºæ»¡
int StackPop(SeqStack &parking); //åºæ æä½
int StackTop(SeqStack parking , char *license_plate , int &time);//åæ 顶å ç´
void Car_come(SeqStack &parking , LQueue *&sidewalk); //æ车å°æ¥æ¶çæä½
void Car_leave(SeqStack &parking , LQueue *&sidewalk); //æ车离å¼çæä½
void Display(SeqStack parking); //æ¾ç¤ºå车åºå çææä¿¡æ¯ è°è¯æ¶ç¨
void InitStack(SeqStack &parking); //åå§åæ
void InitList(LQueue *&sidewalk); //åå§åéå
void In_LQueue(LQueue *&sidewalk , char *license_plate); //è¿éæä½
void Input_Check(char *license_plate); ////æ£éªè¾å ¥ç车çæ¯å¦åæ³
void StackPush(SeqStack &parking , char *license_plate , int stop_time);//è¿æ æä½
void main()
{
//å®ä¹åé
SeqStack parking;
LQueue *sidewalk = NULL;
char *choice = new char;
int flag = 1; //å®ä¹ä¸ä¸ªåé å¤ææ¯å¦éåº
//åå§åä¸ä¸ªä¸ºç©ºçå车åº
InitStack(parking);
//åå§åä¸ä¸ªä¸ºç©ºç便é
InitList(sidewalk);
//è¿è¡çé¢ååè½éæ©
while(flag)
{
cout<<"\n\t å车åºæ¨¡æ管çç³»ç» \n\n";
cout<<"\t|--------------------------------------------------|\n\n";
cout<<"\t|æ¬ç¨åºä¸ºå车åºç模æ管çç³»ç»ï¼æ车å°æ¥æ¶è¯·æCé®ã|\n\n";
cout<<"\t|ç¶åæ ¹æ®å±å¹æ示è¿è¡ç¸å ³æä½ï¼æ车è¦èµ°æ¶è¯·ælé®ã|\n\n";
cout<<"\t|ç¶åæ ¹æ®å±å¹æ示è¿è¡ç¸å ³æä½ï¼æ¥çå车åºè¯·æDé®ã|\n\n";
cout<<"\t|ç¶åæ ¹æ®å±å¹æ示è¿è¡ç¸å ³æä½ï¼è¦éåºç³»ç»è¯·æQé®ã|\n\n";
cout<<"\t|--------------------------------------------------|\n\n";
cout<<"请éæ©æä½ï¼";
gets(choice);
if(1 != strlen(choice))
{
cout<<"请æ£ç¡®è¾å ¥é项!";
continue;
}
else
{
switch(*choice)
{
case 'c':
case 'C':
{
Car_come(parking,sidewalk);break;
}
case 'l':
case 'L':
{
Car_leave(parking,sidewalk);break;
}
case 'q':
case 'Q':
{
flag=0;break;
}
case 'd':
case 'D':
{
Display(parking);break;
}
default:
cout<<"éæ©ä¸æ£ç¡®ï¼è¯·éæ°éæ©ï¼\n";
}
}
}
}
//æ车å°æ¥æ¶çæä½
void Car_come(SeqStack &parking , LQueue *&sidewalk)
{
//å®ä¹åé
char license_plate[MAX_PLATE];
cout<<"请è¾å ¥è½¦è¾ç车çå·ç ï¼";
Input_Check(license_plate);
//å¤æå车åºæ¯å¦å·²æ»¡ï¼æ»¡åè¿å ¥ä¾¿éï¼ä¸æ»¡è¿å ¥å车åº
if(StackFull(parking))
{
In_LQueue(sidewalk , license_plate); //è¿å ¥ä¾¿é
cout<<"å车åºå·²æ»¡è¯·å¨ä¾¿éçå,æ¨çä½ç½®ä¸º"<<QueueLength(sidewalk)
<<endl;
}
else
{
StackPush(parking , license_plate , GetTickCount()); //è¿å ¥å车åº
cout<<"请è¿å ¥å车åºä¸ç"<<parking.top+1<<"å·å车ä½\n";
}
// Display(parking);
}
//æ车离å¼æ¶çæä½
void Car_leave(SeqStack &parking , LQueue *&sidewalk)
{
//å®ä¹åé
SeqStack tmpparking; //å®ä¹ä¸´æ¶å车åº
char leave_license_plate[MAX_PLATE]; //è¦ç¦»å¼ç车çå·
char license_plate[MAX_PLATE]; //åæ¾ä»å车åºä¸è¯»åºæ¥ç车çä¿¡æ¯
int time;
InitStack(tmpparking); //åå§å临æ¶å车åº
//å¤æå车åºä¸æ¯å¦æ车
if(StackEmpty(parking))
{
cout<<"å½åå车åºä¸æ²¡æ车\n";
return; //éåºåå½æ°
}
cout<<"请è¾å ¥è¦ç¦»å¼ç车çç §ï¼";
Input_Check(leave_license_plate);
cout<<"å½åå车åºä¸æ"<<parking.top+1<<"è¾è½¦\n";
if(LeaveCheck(parking , leave_license_plate)) //å¤æ车æ¯å¦å¨å车åºä¸
{
//车å¨å车åºä¸
cout<<"æ¨ç车å¨"<<LeaveCheck(parking , leave_license_plate)<<"å·è½¦ä½ä¸\n";
while(StackTop(parking , license_plate , time)
&& (strcmp(parking.STOP[parking.top].license_plate , leave_license_plate) != 0))
{
strcpy(parking.STOP[parking.top].license_plate , license_plate);
cout<<"çç §ä¸º"<<license_plate<<"ç车ææ¶éåºå车åº"<<parking.top+1<<"å·ä½\n";
StackPush(tmpparking , license_plate , time); //å车åºä¸ç车ææ¶éåº è¿å ¥ä¸´æ¶å车åº
StackPop(parking); //åºæ
}
cout<<"çç §ä¸º"<<license_plate<<"ç车离å¼å车åº"<<parking.top+1<<"å·ä½\n";
cout<<"æ¨å¨å车åºä¸åäº"<<(GetTickCount()-time)/TIME_MS_TO_CONUT<<TIME_COUNT<<endl; //è¾åºæåæ¶é´ä¿¡æ¯
cout<<"åºç¼´è´¹ç¨ä¸º"<<(GetTickCount()-time)/TIME_MS_TO_CONUT*UNIT_PRICE<<"å \n";; //è¾åºè´¹ç¨ä¿¡æ¯
StackPop(parking); //åºæ
//å°ä¸´æ¶å车åºä¸ç车ååå车åº
while(StackEmpty(tmpparking) != 1)
{
StackTop(tmpparking , license_plate , time);
StackPush(parking , license_plate , time);
cout<<"çç §ä¸º"<<license_plate<<"ç车è¿å ¥å车åº"<<parking.top+1<<"å·ä½\n";
license_plate[0] = '\0';
StackPop(tmpparking);
}
if(parking.top+1 == MAX_STOP-1) //å¤æ车离å¼åå车åºæ¯å¦å满
if(QueueLength(sidewalk)) //å¦æå满åå¤æ便éä¸æ¯å¦æ车
{
//便éä¸æ车 åä»ä¾¿éä¸åå ¥å车åº
Out_LQueue(sidewalk , license_plate); //åºé
StackPush(parking , license_plate , GetTickCount()); //å ¥æ
cout<<"å¨ä¾¿éä¸çç §ä¸º"<<license_plate<<"ç车è¿å ¥å车åº"<<parking.top+1<<"å·ä½\n";
}
}
else
//车ä¸å¨å车åºä¸
cout<<"æ¨ç车ä¸å¨å车åºä¸ï¼\n";
}
//åå§å顺åºæ
void InitStack(SeqStack &parking)
{
parking.top = -1;
}
//å¤æ 空
int StackEmpty(SeqStack parking)
{
if(parking.top == -1)
return 1;
else
return 0;
}
//å¤æ 满
int StackFull(SeqStack parking)
{
if(parking.top == MAX_STOP-1)
return 1;
else
return 0;
}
//å ¥æ
void StackPush(SeqStack &parking , char *license_plate , int stop_time)
{
parking.top++;
strcpy(parking.STOP[parking.top].license_plate , license_plate);
parking.STOP[parking.top].state = 'p';
parking.STOP[parking.top].time = stop_time;
}
//åºæ è¿åæ 顶æé
int StackPop(SeqStack &parking)
{
if(StackEmpty(parking))
return 0;
else
return parking.top--;
}
//åæ 顶å ç´
int StackTop(SeqStack parking , char *license_plate , int &time)
{
if(StackEmpty(parking))
return 0;
else
{
strcpy(license_plate , parking.STOP[parking.top].license_plate);
time = parking.STOP[parking.top].time;
return 1;
}
}
//æ¾ç¤ºææ
void Display(SeqStack parking)
{
if(parking.top == -1)
printf("å车åºä¸ºç©º\n");
else
{
while(parking.top != -1)
{
cout<<"车çå·ä¸ºï¼"<<parking.STOP[parking.top].license_plate;
cout<<"ï¼åå¨"<<parking.top + 1 <<"å·è½¦ä½ä¸";
cout<<"ï¼å·²å"<<(GetTickCount()-parking.STOP[parking.top].time)/TIME_MS_TO_CONUT<<TIME_COUNT<<endl;
parking.top--;
}
}
}
//åå§åéå
void InitList(LQueue *&sidewalk)
{
sidewalk = (LQueue *)malloc(sizeof(LQueue));
sidewalk->front=sidewalk->rear = NULL;
}
//å ¥é
void In_LQueue(LQueue *&sidewalk,char *license_plate)
{
QNode *car_on_sidewalk;
car_on_sidewalk = (QNode *)malloc(sizeof(QNode)); //为æ°èç¹å¼è¾æ°ç©ºé´
strcpy(car_on_sidewalk->WAIT.license_plate , license_plate); //å°æ°æ®åå ¥èç¹
car_on_sidewalk->WAIT.state = 's'; //åå ¥å车信æ¯
car_on_sidewalk->WAIT.time = GetTickCount(); //åå ¥å车æ¶é´
car_on_sidewalk->next = NULL;
if(Empty_LQueue(sidewalk)) //é空åå建第ä¸ä¸ªèç¹
sidewalk->front = sidewalk->rear = car_on_sidewalk;
else
{
//éé空æå ¥éå°¾
sidewalk->rear->next = car_on_sidewalk;
sidewalk->rear = car_on_sidewalk;
}
}
//å¤é空
int Empty_LQueue(LQueue *q)
{
if(q->front == NULL)
return 1;
else
return 0;
}
//å¤éé¿åº¦ è¿åéé¿
int QueueLength(LQueue *q)
{
QNode *p=q->front;
int i=0;
while(p != NULL)
{
i++;
p=p->next;
}
return i;
}
//åºé æåè¿å1 é空è¿å0
int Out_LQueue(LQueue *&sidewalk,char *license_plate)
{
QNode *car_on_sidewalk;
if(Empty_LQueue(sidewalk)) //å¦æé空è¿å0
return 0;
car_on_sidewalk = sidewalk->front;
strcpy(license_plate , car_on_sidewalk->WAIT.license_plate);//ååºé头å ç´
if(sidewalk->front == sidewalk->rear) //éä¸åªæä¸ä¸ªå ç´
sidewalk->front = sidewalk->rear=NULL; //å é¤å ç´
else
sidewalk->front = sidewalk->front->next; //é头æéå移
free(car_on_sidewalk); //éæ¾æé
return 1;
}
//æ£æ¥ç¦»å¼ç车æ¯å¦å¨å车åºä¸ è¿å车å¨å车åºä¸ä½ç½® ä¸å¨åè¿å0
int LeaveCheck(SeqStack parking,char *license_plate)
{
int flag = parking.top+1; //å®ä¹åéè®°å½å½å车å¨å车åºä¸ä½ç½®
if(StackEmpty(parking))
return 0;
else
{
//æ¥æ¾ç¦»å¼è½¦æå¨ä½ç½®
while(parking.top != -1 && strcmp(parking.STOP[parking.top].license_plate , license_plate) != 0)
{
flag--;
parking.top--;
}
return flag;
}
}
//æ£éªè¾å ¥ç车çæ¯å¦åæ³
void Input_Check(char *license_plate)
{
int flag = 1;
int i;
string tmpstr;
while(flag)
{
cin>>tmpstr;
getchar();
if(tmpstr.length()<MAX_PLATE)
{
for(i=0;i<;i++)
license_plate[i] = tmpstr.c_str()[i];
flag = 0;
}
else
cout<<"è¾å ¥æ误ï¼è¯·éæ°è¾å ¥ï¼";
}
}
以åç课设 ä½ ççå§ çº¯æå·¥ç~~
谁能告诉一下用循环队列实现打印杨辉三角形的C语言代码?
#include <stdio.h>
#include <iostream.h>
#include "queue.h"
void YANGHUI(int n) {
SeqQueue q(n+2); //队列初始化p
q.EnQueue(1); q.EnQueue(1);
int s = 0, t;
for (int i = 1; i <= n; i++) { //逐行计算
cout << endl;
q.EnQueue(0);
for (int j = 1; j <= i+2; j++) { //下一行
q.DeQueue(t);
q.EnQueue(s + t);
s = t;
if (j != i+2) cout << s << ' ';
}
}
}
课件地址:t=0;
void AStar(int *from,int *to)
{
priority_queue<state>q;
ULL x=ToULL(from);
ULL y=ToULL(to);
fill_hval(to);
q.push((state){ h(x),x});
insert(x,0);
int d[];
while(!q.empty())
{
cnt++;
state s=q.top();
ULL i=s.x; q.pop();
int step=find(i);
int z=ToARR(i,d);
//printf("%lld %d %d\n",i,step,z);
if(i==y) return;
if(z-3>0)
{
swap(d[z],d[z-3]);
ULL j=ToULL(d);
swap(d[z],d[z-3]);
if(find(j)!=-1) goto out1;
q.push((state){ step+h(j),j});
insert(j,step+1);
}
out1:
if(z+3<)
{
swap(d[z],d[z+3]);
ULL j=ToULL(d);
swap(d[z],d[z+3]);
if(find(j)!=-1) goto out2;
q.push((state){ step+h(j),j});
insert(j,step+1);
}
out2:
if(z%3!=0)
{
swap(d[z],d[z+1]);
ULL j=ToULL(d);
swap(d[z],d[z+1]);
if(find(j)!=-1) goto out3;
q.push((state){ step+h(j),j});
insert(j,step+1);
}
out3:
if(z%3!=1)
{
swap(d[z],d[z-1]);
ULL j=ToULL(d);
swap(d[z],d[z-1]);
if(find(j)!=-1) continue;
q.push((state){ step+h(j),j});
insert(j,step+1);
}
}
}
int from[],to[];
void work()
{
for(int i=1;i<=9;i++)
scanf("%d",&from[i]);
for(int i=1;i<=9;i++)
scanf("%d",&to[i]);
AStar(from,to);
ULL y=ToULL(to);
printf("%d ",find(y));
#ifdef DEBUG
printf("%d ",clock());
printf("%d ",cnt);
#endif
}
int main()
{
#ifdef DEBUG
freopen("debug.in","r",stdin);
freopen("debug.out","w",stdout);
#endif
work();
return 0;
}
这是基于曼哈顿距离的估价函数的Astar
2024-11-30 08:14
2024-11-30 08:06
2024-11-30 07:42
2024-11-30 07:39
2024-11-30 07:18