很久没有写新东西了,然后就把最近学的一些东西整理一下
做了一个个人实践项目 停车管理系统 运用的栈和队列的结构(C)
以前写代码时总会图方便直接使用一些封装好的东西,后来仔细想想
自己并不能独立的完整地写出这些东西,从新写一下,让自己学到了不少东西
主要功能:停车,取车,显示信息
主要是利用栈和队列去实现一些简单功能
基于linux系统的小程序1
main.c//主函数
/*****************************
author:***
mail:***3370834@qq.com
desc:停车管理系统主函数
*****************************/
#include "car.h"
#include "seqStack.h"
#include "seqQueue.h"
int main()
{
SqStack S;
queue q;
SqStack Sf;
InitStack(&Sf);
InitStack(&S);
initQueue(&q, 100);
while(1){
welcome(&S, &q);
int i;
i = get_num();
int flag = 1;
switch(i)
{
case 1:in_car(&S, &q);
break;
case 2:out_car(&S, &Sf, &q);
break;
case 3:put(&S, &q);
break;
case 4:flag = 0;
break;
default:printf("输入信息错误 请重新输入!\n");
break;
}
if(flag == 0)break;
}
destroyQueue(&q);
DestroyStack(&S);
DestroyStack(&Sf);
return 0;
}
car.h//关于停车取车方面的头文件
#ifndef CAR_H
#define CAR_H
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "seqStack.h"
#include "seqQueue.h"
/*#define OK 1 //正确
#define ERROR 0 //出错
#define TRUE 1 //为真
#define FALSE 0 //为假
typedef int status;
typedef int ElemType; //宏定义队列的数据类型
#define MAX_SIZE 20 //.
#define MAX_Car 10
#define STACK_INIT_SIZE 20
#define OVERFLOW 0
#define STACK_INCREMENT 10*/
void welcome(SqStack *S, queue *q);
int out_1(int num, SqStack *S, SqStack *Sf);
void out_car(SqStack *S, SqStack *Sf, queue *q);
void in_car(SqStack *S, queue *q);
int get_num();
void put(SqStack *S, queue *q);
#endif
car.c
/*****************************
author:***
mail:***3370834@qq.com
desc:停车操作函数
*****************************/
#include "car.h"
/*
return:
无
parm:
S:栈
q:队列
desc:
输出S和q中的信息
*/
void put(SqStack *S, queue *q)
{
printf ("\t*******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n",MAX_Car, LengthStack(S), getQueueLen(q));
printf ("\t********************************************************\n");
printf ("\t 车牌号 车辆状态 \n");
TrverseStack(S);//输出S
queueTraverse(q);//输出q
}
/*
return:
-1:输入信息不合法
num:输入的信息
parm:
无
desc:
获取数据并检验是否合法
*/
int get_num()
{
char sh[100];
fgets(sh, 80, stdin);
int num = 0;
int flag = 0;
int len = 0;
len = strlen(sh);
int i = 0;
if(len > 9 || sh[0] == '\n')
{
flag = 1;
}
while(i < len - 1)//通过循环把num的值计算出
{
if(sh[i] >= '0' && sh[i] <= '9')
{
num *= 10;
num += (sh[i] - '0');
}
else
{
flag = 1;
break;
}
i++;
}
if(flag == 1)return -1;
else return num;
}
/*
return:
flag:是否查找成功
parm:
S:主队列
Sf:辅助队列
desc:
判断栈中是否有待取元素信息
*/
int out_1(int num, SqStack *S, SqStack *Sf)
{
int flag = 0;
while(IsEmptyStack(S) == 0)
{
int num1 = -1;
Pop(S, &num1);
if(num1 == num)
{
flag = 1;
break;
}
Push(Sf, num1);
}
while(IsEmptyStack(Sf) == 0)
{
int num1 = -1;
Pop(Sf, &num1);
Push(S, num1);
}
return flag;
}
/*
return:
无
parm:
S:栈
Sf:辅助栈
q:队列
desc:
取车
*/
void out_car(SqStack *S, SqStack *Sf, queue *q)
{
int num;
printf("请输入要取的车牌号!\n");
while(1)
{
num = get_num();
if(num < 0)
{
printf("输入信息错误 请重新输入\n");
}
else{
break;
}
}
if(num == queuefront(q))
{
printf("车牌号为%d的车辆取消排队\n", num);
int linshi;
deQueue(q, &linshi);
}
else
{
if(out_1(num, S, Sf) == 1)
{
printf("车牌号为%d的车辆离开了停车场.\n", num);
if(isEmpityQueue(q) == 0)
{
int xx;
deQueue(q, &xx);
//printf("*************** %d ********\n", xx);
Push(S, xx);
printf("车牌号为%d的车辆进入了停车场.\n", xx);
}
}
else
{
printf("停车场中无此车信息\n");
}
}
}
/*
return:
无
parm:
S:栈
q:队列
desc:
进停车场
*/
void in_car(SqStack *S, queue *q)
{
printf("请输入车辆牌号:\n");
int num;
while(1)
{
num = get_num();
if(num < 0)
{
printf("输入信息错误 请重新输入\n");
}
else{
break;
}
}
if(LengthStack(S) < MAX_Car)
{
Push(S, num);
printf("车牌号为%d的车辆进入了停车场.\n", num);
}
else
{
enQueue(q, num);
printf("车牌号为%d的车辆开始排队.\n", num);
}
}
/*
return:
操作界面
parm:
S:栈
q:队列
desc:
主操作界面
*/
void welcome(SqStack *S, queue *q)
{
printf ("\t*******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n",MAX_Car, LengthStack(S), getQueueLen(q));
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");
printf ("请输入服务编号:\n");
}
seqQueue.h//队列
#ifndef SEQQUEUE_H
#define SEQQUEUE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<assert.h>
#define OK 1 //正确
#define ERROR 0 //出错
#define TRUE 1 //为真
#define FALSE 0 //为假
typedef int status;
typedef int Status;
typedef int ElemType; //宏定义队列的数据类型
#define MAX_SIZE 20
#define MAX_Car 10
#define STACK_INIT_SIZE 20
#define OVERFLOW 0
#define STACK_INCREMENT 10
typedef struct
{
ElemType *pBase; //数组指针
ElemType front; //队头索引
ElemType rear; //队尾索引
int maxSize; //当前分配的最大容量
}queue;
//创建空队列 queueCapacity-队列容量
status initQueue(queue *PQueue,int queueCapacity);
//销毁队列
void destroyQueue(queue *PQueue);
//清空队列
void clearQueue(queue *PQueue);
//判断队列是否为空
status isEmpityQueue(queue *PQueue);
//判断队列是否为满
status isFullQueue(queue *PQueue);
//获得队列长度
int getQueueLen(queue *PQueue);
//新元素入队 [先进先出原则:在队尾的位置插入] element-要插入元素
status enQueue(queue *PQueue,ElemType element);
//新元素出队,同时保存出队的元素 [先进先出原则:在队头的位置删除]
status deQueue(queue *PQueue,ElemType *pElement);
//遍历队列
void queueTraverse(queue *PQueue);
status queuefront(queue *PQueue);
#endif
seqQueue.c
/*****************************
author:***
mail:***3370834@qq.com
desc:队列的线性实现函数
*****************************/
#include "seqQueue.h"
/*
return:
ERROR:分配内存失败
OK:初始化成功a
parm:
PQueue:队列
queueCapacity:申请内存的大小
desc:
初始化队列
*/
status initQueue(queue *PQueue,int queueCapacity)
{
//给数组指针分配内存
PQueue->pBase = (ElemType *)malloc(sizeof(ElemType)*queueCapacity);
if(!PQueue->pBase)
{
printf("给数组指针分配内存失败\n");
return ERROR;
}
PQueue->front = 0; //最开始创建时,队头索引为0
PQueue->rear = 0; //最开始创建时,队尾索引为0
PQueue->maxSize = queueCapacity;
return OK;
}
/*
return:
无;
parm:
PQueue:队列
desc:
销毁队列
*/
void destroyQueue(queue *PQueue)
{
free((*PQueue).pBase); //释放队列数组指针指向的内存
(*PQueue).pBase = NULL; //队列数组指针重新指向NULL,避免成为野指针
}
/*
return:
无
parm:
PQueue:队列
desc:
清空队列
*/
void clearQueue(queue *PQueue)
{
PQueue->front = 0; //队头索引清0
PQueue->rear = 0; //队尾索引清0
}
/*
return:
TRUE:队空
FALSE:队不空
parm:
PQueue:队列
desc:
判断队列是否为空
*/
status isEmpityQueue(queue *PQueue)
{
if( PQueue->front == PQueue->rear ) //队头==队尾,说明为空
return TRUE;
return FALSE;
}
/*
return:
TRUE:队满
FALSE:队不满
parm:
PQueue:队列
desc:
判断队满
*/
status isFullQueue(queue *PQueue)
{
if( (PQueue->rear+1)%PQueue->maxSize == PQueue->front ) //队列满
return TRUE;
return FALSE;
}
/*
return:
返回队列长度
parm:
PQueue:队列
desc:
获取队列长度
*/
int getQueueLen(queue *PQueue)
{
//正常情况下,队列长度为队尾队头指针之差,但如果首尾指针跨容量最大值时,要%
return (PQueue->rear - PQueue->front + PQueue->maxSize)%PQueue->maxSize;
}
/*
return:
TRUE:入队成功
FALSE:队满 入队失败
parm:
PQueue:队列
element:入队元素
desc:
向队列中添加新的元素
*/
status enQueue(queue *PQueue,ElemType element)
{
if(isFullQueue(PQueue)==TRUE)
{
printf("队列已满,不能再插入元素了!\n");
return FALSE;
}
//向队列中添加新元素
PQueue->pBase[PQueue->rear] = element;
PQueue->rear = (PQueue->rear+1) % PQueue->maxSize; //将rear赋予新的合适的值
return TRUE;
}
/*
return:
TRUE:出队列成功
FALSE:出队列失败
parm:
PQueue:队列
pElement:记录出队元素
desc:
出队
*/
status deQueue(queue *PQueue,ElemType *pElement)
{
//如果队列为空,则返回false
if(isEmpityQueue(PQueue)==TRUE)
{
printf("队列为空,出队失败!\n");
return FALSE;
}
*pElement = PQueue->pBase[PQueue->front]; //先进先出
PQueue->front = (PQueue->front+1) % PQueue->maxSize; //移到下一位置
return TRUE;
}
/*
return:
无
parm:
PQueue:队列
desc:
遍历输出队列元素
*/
void queueTraverse(queue *PQueue)
{
int i = PQueue->front; //从头开始遍历
while(i != PQueue->rear)
{
printf ("\t%10d 排队中\n", PQueue->pBase[i]);
i = (i+1) % PQueue->maxSize;
}
printf("\n");
}
status queuefront(queue *PQueue)
{
if(isEmpityQueue(PQueue) == 0)
{
return PQueue->pBase[PQueue->front];
}
return -1;
}
seqStack.h
#ifndef SEQSTACK_H
#define SEQSTACK_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<assert.h>
//进行宏定义
/*#define STACK_INIT_SIZE 20 //空间初始分配
#define STACK_INCREMENT 10 /// 存储空间分配增量
#define ElemType int
#define Status int
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW 0*/
//顺序栈结构体
typedef struct
{
int *base;
int *top;
int stacksize;
} SqStack;
//函数声明
int InitStack (SqStack *S);
int DestroyStack(SqStack *S);
int ClearStack(SqStack *S);
int IsEmptyStack(SqStack *S);
int LengthStack(SqStack *S);
int GetTop(SqStack *S);
int Push(SqStack *S,int e);
int Pop(SqStack *S, int *e);
int TrverseStack(SqStack *S);
#endif
seqStack.c
/*****************************
author:***
mail:***3370834@qq.com
desc:栈的线性实现函数
*****************************/
#include "seqStack.h"
#include "seqQueue.h"
/*
* return:
* OVERFLOW:分配内存失败
* OK:初始化成功
* parm:
* S:栈
* desc:
* 初始化栈S 为栈分配空间
* */
int InitStack (SqStack *S)
{
assert(NULL != S);
(*S).base = (ElemType * )malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (NULL == (*S).base) exit(0);//内存分配失败
(*S).top = (*S).base;
(*S).stacksize = STACK_INIT_SIZE;
return 1;//初始化成功
}
/*
* return:
* OK:销毁栈成功
* parm:
* S:栈
* desc:
* 销毁栈
* */
Status DestroyStack(SqStack *S)
{
assert(NULL != S);
free((*S).base);
(*S).base = NULL;
(*S).top = NULL;
return OK;
}
Status ClearStack(SqStack *S)
{
assert(NULL != S);
ElemType *index;
(*S).top = (*S).base;
return OK;
}
/*
return:
TRUE:栈为空
FALSE:栈不空
parm:
S:栈
desc:
判定栈是否为空
*/
Status IsEmptyStack(SqStack *S)
{
assert(NULL != S);
if ((*S).base == (*S).top) return TRUE;//栈空
else return FALSE;//栈不空
}
/*
* return:
* (*S).top -(*S).base: 栈的长度
* parm:
* S:栈
* desc:
* 求栈的长度
* */
Status LengthStack(SqStack *S)
{
assert(NULL != S);
return (*S).top - (*S).base;
}
/*
* return:
* ERROR:栈空出错
* e:栈定元素
* parm:
* S:栈
* desc:
* 求栈顶元素
* */
ElemType GetTop(SqStack *S)
{
assert(NULL != S);
if ((*S).top == (*S).base) return ERROR;//栈空
ElemType e = *((*S).top - 1);//把栈定元素赋值给e
return e;
}
/*
* return:
* OVERFLOW:分配内存失败
* OK:插入成功
* parm:
* S:栈
* e:待插入元素
* desc:
* 把e入栈
* */
Status Push(SqStack *S,ElemType e)
{
assert(NULL != S);
if ((*S).top - (*S).base >= (*S).stacksize)//内存不够时额外申请内存
{
(*S).base = (ElemType *) realloc ((*S).base,((*S).stacksize + STACK_INCREMENT) * sizeof(ElemType));
if (NULL == (*S).base)exit(OVERFLOW);//申请内存失败
(*S).top = (*S).base + (*S).stacksize;
(*S).stacksize += STACK_INCREMENT;
}
*((*S).top) = e;
(*S).top ++;
return OK;
}
/*
* return:
* ERROR:栈空 无法弹出
* OK:弹出栈顶成功
* parm:
* S:栈
* e:记录栈顶值
* desc:
* 弹出栈顶值
* */
Status Pop(SqStack *S, ElemType *e)
{
assert(NULL != S);
if ((*S).top == (*S).base) return ERROR;//栈空
*e = * --(*S).top;
return OK;
}
/*
* return:
* OK:输出成功
* parm:
* S:栈
* desc:
* 从栈低到栈顶输出栈中元素
* */
Status TrverseStack(SqStack *S)
{
assert(NULL != S);
ElemType *index;
for (index = (*S).base; index != (*S).top; index ++)
{
printf ("\t%10d 停车中\n", *index);
}
printf("\n");
return OK;
}
makefile
main:main.o seqQueue.o car.o seqStack.o
gcc -g main.o seqQueue.o car.o seqStack.o -o main
main.o:main.c seqQueue.h car.h seqStack.h
gcc -g -c main.c -o main.o
car.o:car.c car.h seqQueue.h
gcc -g -c car.c -o car.o
seqQueue.o:seqQueue.c seqQueue.h
gcc -g -c seqQueue.c -o seqQueue.o
seqStack.o:seqStack.c seqStack.h seqQueue.h
gcc -g -c seqStack.c -o seqStack.o
clean:
rm -fr *.o
rm -fr main