资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言栈和队列题目函数 c语言中栈和队列的特点

C语言栈和队列问题:停车场停车问题

#ifndef __PLOT_H__#define __PLOT_H__#define FALSE 0#define TRUE  1#define MONEY    1          // 单价可以自己定义#define MAX_STOP 10#define MAX_PAVE 100// 存放汽车牌号typedef struct{   

从网站建设到定制行业解决方案,为提供做网站、网站制作服务体系,各种行业企业客户提供网站建设解决方案,助力业务快速发展。创新互联建站将不断加快创新步伐,提供优质的建站服务。

int time1;              // 进入停车场时间

int time2;              // 离开停车场时间

char plate[10];  

// 汽车牌照号码,定义一个字符指针类型}Car;// 停放栈typedef struct{

Car Stop[MAX_STOP-1];   // 各汽车信息的存储空间

int top;                // 用来指示栈顶位置的静态指针}Stopping;// 等候队列typedef struct{    int count;              // 用来指示队中的数据个数

Car Pave[MAX_PAVE-1];   // 各汽车信息的存储空间

int front, rear;        // 用来指示队头和队尾位置的静态指针}Pavement;// 让路栈typedef struct{

Car Help[MAX_STOP-1];   // 各汽车信息的存储空间

int top;                // 用来指示栈顶位置的静态指针}Buffer;

Stopping s;

Pavement p;

Buffer   b;

Car      c;char     C[10];void stop_pave    ();       // 车停入便道void car_come     ();       // 车停入停车位void stop_to_buff ();       // 车进入让路栈void car_leave    ();       // 车离开void welcome      ();       // 主界面函数void Display      ();       // 显示车辆信息#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

源文件 PLot.c

#include "PLot.h"#include stdio.h#include time.h                           // 包含时间函数的头文件#include string.h#include stdlib.hvoid stop_to_pave()                         // 车停入便道{    // 判断队满

if (p.count  0  (p.front == (p.rear + 1) % MAX_PAVE))

{        printf ("便道已满,请下次再来\n");

}    else

{        strcpy(p.Pave[p.rear].plate, C);

p.rear = (p.rear + 1) % MAX_PAVE;   // 队尾指示器加1

p.count++;                          // 计数器加1

printf ("牌照为%s的汽车停入便道上的%d的位置\n", C, p.rear);

}

}void car_come()                             // 车停入停车位{    printf ("请输入即将停车的车牌号:");        // 输入车牌号

scanf ("%s", C);    if (s.top = MAX_STOP - 1)              // 如果停车位已满,停入便道

{

stop_to_pave();                     // 停车位-便道  函数

}    else

{

s.top++;                            // 停车位栈顶指针加1

time_t t1;        long int t = time (t1);            // 标记进入停车场的时间

char* t2;

t2 = ctime (t1);                   // 获取当前时间

c.time1 = t;        strcpy(s.Stop[s.top].plate, C);     // 将车牌号登记                   

printf ("牌照为%s的汽车停入停车位的%d车位, 当前时间:%s\n", C, s.top + 1, t2);

}    return ;

}void stop_to_buff()                         // 车进入让路栈{    // 停车位栈压入临时栈,为需要出栈的车辆让出道

while (s.top = 0)  

{   

if (0 == strcmp(s.Stop[s.top--].plate, C))

{            break;

}        // 让出的车进入让路栈

strcpy(b.Help[b.top++].plate, s.Stop[s.top + 1].plate);        printf ("牌照为%s的汽车暂时退出停车位\n", s.Stop[s.top + 1].plate);

}   

b.top --;    // 如果停车位中的车都让了道,说明停车位中无车辆需要出行

if (s.top  -1)

{        printf ("停车位上无此车消息\n");

}    else 

{        printf ("牌照为%s的汽车从停车场开走\n", s.Stop[s.top + 1].plate);

}    // 将让路栈中的车辆信息压入停车位栈

while (b.top = 0)

{        strcpy(s.Stop[++s.top].plate, b.Help[b.top--].plate);        printf ("牌照为%s的汽车停回停车位%d车位\n", b.Help[b.top + 1].plate, s.top + 1);

}    // 从便道中 - 停车位

while (s.top  MAX_STOP-1)

{        if (0 == p.count)               // 判断队列是否为空

{            break;  

}        // 不为空,将便道中优先级高的车停入停车位

else                    

{            strcpy(s.Stop[++s.top].plate, p.Pave[p.front].plate);            printf ("牌照为%s的汽车从便道中进入停车位的%d车位\n", p.Pave[p.front].plate, s.top+1);

p.front = (p.front + 1) % MAX_PAVE;

p.count--;

}

}

}void car_leave()                        // 车离开{    printf ("请输入即将离开的车牌号:\n");    scanf ("%s", C);    if (s.top  0)                      // 判断停车位是否有车辆信息

{        printf ("车位已空,无车辆信息!\n");

}    else

{

stop_to_buff();

}

time_t t1;      

long int t = time (t1);                

c.time2 = t;                        // 标记离开停车场的时间

char* t2;

t2 = ctime (t1);                   // 获取当前时间

printf ("离开时间%s\n需付%ld元\n", t2, MONEY * (c.time2 - c.time1) / 10);

}void Display()

{    int i = s.top;    if (-1 == i)

{        printf ("停车场为空\n");

}

time_t t1;    long int t = time(t1);             // 标记显示时的时间

printf ("\t车牌号\t\t\t停放时间\t\t当前所需支付金额\n");    while (i != -1)

{       

printf ("\t%s\t\t%d秒\t\t\t%d元\n", s.Stop[i].plate, t - c.time1, MONEY * (t - c.time1) / 10);

i--;

}

}void welcome()

{        printf ("\t*******************目前停车场状况***********************\n");        printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front)

% MAX_PAVE);        printf ("\t********************************************************\n");        printf ("\t---------------Welcome to our Car Parking---------------\n");  

printf ("\t*                     1.Parking                        *\n");  

printf ("\t*                     2.leaving                        *\n");  

printf ("\t*                     3.situation                      *\n");  

printf ("\t*                     4.exit                           *\n");  

printf ("\t--------------------------------------------------------\n");

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150

主函数 main.c

/**********************************************************

问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,

汽车按到达的先后次序停放。若车场满了,车要在门外的便道上等候

,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由

于通道窄,在它后面的车要先退出,待它走后依次进入。汽车离开

时按停放时间收费。

基本功能要求:

1)建立三个数据结构分别是:停放队列,让路栈,等候队列

2)输入数据模拟管理过程,数据(入或出,车号)。

***********************************************************/#include "PLot.h"int main()

{    // 初始化

s.top   = -1;

b.top   =  0;

p.rear  =  0;

p.count =  0;

p.front =  0;    while(1)

{

system("clear");

welcome();        int i, cho;

scanf ("%d", i);        if (1 == i) car_come(); 

if (2 == i) car_leave();        if (3 == i) Display();        if (4 == i) break;

printf ("返回请输入1\n");

scanf ("%d", cho);        if (1 == cho)

{            continue;

}        else

{

printf ("您的输入有误,请重新输入\n");

scanf ("%d", cho);            continue;

}

}    return 0;

}

C语言栈及队列问题

1、按链表的相反顺序打印出链表中各节点的值

typedef struct _l

{

int data;

struct _l *next;

} link;

void func(link *p)

{

if(p!=NULL)

{

func(p-next);

printf("%d",p-data);

}

}

2、一个队列管理的模拟系统

typedef struct _l

{

int data;

struct _l *next;

} link;

link *tail=NULL;

//1.队列初始化

int init()

{

tail=(link *)malloc(sizeof(link));

if(tail!=NULL)

{

tail-next=tail;

return 1;

}

return 0;

}

//2.当键盘输入一个正整数时,把它作为一个队列元素入队列

int insert(int n)

{

link *p;

p=(link *)malloc(sizeof(link));

if(p==NULL)

{

return 0;

}

p-data=n;

p-next=tail-next;

tail-next=p;

return 0;

}

//3.当键盘输入一个负整数时,则队头元素出列

int delete()

{

link *p=tail,*q=p;

while(p-next!=tail)

{

q=p;p=p-next;

}

if(p==q)

{

return 0;//队列空

}

else

{

q-next=p-next;

free(p);

return 1;

}

}

//4.当键盘输入0时,退出系统

//5.每输入一个整数,输出经过队列操作后队列中的所有元素

void trans()

{

link *p=tail-next;

while(p!=tail)

{

printf("%d",p-data);

p=p-next;

}

}

int main()

{

int n;

init();

while(true)

{

scanf("%d",n);

if(n==0)

{

break;

}

else if(n0)

{

insert(n);

}

else

{

delete();

}

trans();

}

}

c++里关于栈的函数哪些常用

1、鼠标双击或者右击打开桌面上DEVc++软件,让其运行起来。

2、栈是一种应用范围广泛的数据结构,适用于各种具有“后进先出”特性的问题。

3、首先构建顺序栈的储存结构。

4、顺序栈的初始化int InitStack(SqStack S)//顺序栈的初始化。

5、入栈int Push(SqStack S,int e)//入栈{if(S.top-S.base==S.stacksize)return 0;*S.top=e;S.top++;return 1;}。

6、最后,倒序输出栈中的单词,就完成了。


分享名称:c语言栈和队列题目函数 c语言中栈和队列的特点
URL网址:http://cdkjz.cn/article/hgchjj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220