C language to implement parking management system

  • 2020-06-19 11:28:57
  • OfStack

Problem description: The parking lot is a narrow passageway for n cars with only one gate. The cars are parked in the order of arrival. If the yard is full, the car must stop on the sidewalk outside the gate and wait. Once a car is walking, the first car on the sidewalk will enter. When the car in the parking lot leaves, due to the narrow passageway, the car behind it has to exit first, and then enter later. Cars are charged by the time they leave.

Basic functional requirements:

(1) Establish three data structures: parking queue, give way stack and waiting queue.
(2) Input data to simulate the management process, data (in or out, vehicle number).

The parking management system is a relatively simple application of queues and stacks in C language. It needs to pay attention to the construction of parking queues, waiting queues and give way stacks. When writing code, the out on the stack and out on the queue, Pointers are prone to error, resulting in segment errors, should be noted. The code I wrote is as follows:

// Define the structure


#include <stdio.h>
#include <stdlib.h>

#define F 0
#define T 1
#define MAX 3

typedef struct Node // data 
{
 int number;
 int time;
}Node;

typedef struct QueueNode // Queue node 
{
 struct Node infom;
 struct QueueNode * next;
}*QueueNode;

typedef struct LinkQueue // Chain queue 
{
 struct QueueNode * front;
 struct QueueNode * rear;
}LinkQueue;

typedef struct stack   // Stack node 
{
 struct Node data;
 struct stack *next;
}*StackNode;

typedef struct LinkStack  // Chain stack 
{
 StackNode top;
 int count;
}LinkStack;

// Function implementation


void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);// The menu 
int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway);// Initialize the 
int linklength(LinkQueue q);// Check the length 
int enqueue(LinkQueue *q,int num,int t);// In the queue 
int dequeue(LinkQueue *q,int *num,int *t);// Out of the queue 
void park1(LinkQueue *wait,LinkQueue *park);// Stop function 
int push(LinkStack *s,int num,int t);// Into the stack 
int pop(LinkStack *s,int *num,int *t);// Out of the stack 
void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);// Leave the function 
void view3(LinkQueue wait,LinkQueue park);// Check the status of parking lot 

int main()
{
 LinkQueue wait;
 LinkQueue park;
 LinkStack giveway;
 int num = 0;
 int t = 0;
 init(&wait,&park,&giveway);
 menu(&wait,&park,&giveway,num,t);
 return 0;
}

int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway)
{
 QueueNode newnode1 = (QueueNode)malloc(sizeof(struct QueueNode));
 if(NULL == newnode1)
 {
  return F;
 }
 newnode1->next = NULL;
 wait->front = newnode1;
 wait->rear = newnode1;

 QueueNode newnode2 = (QueueNode)malloc(sizeof(struct QueueNode));
 if(NULL == newnode2)
 {
  return F;
 }
 newnode2->next = NULL;
 park->front = newnode2;
 park->rear = newnode2;

 giveway->top = NULL;
 giveway->count = 0;
}

void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t)
{
 printf("**********Welcome to our Car Parking !**********\n");
 printf("********** Please choose function **********\n");
 printf("**********   1 : park.   **********\n");
 printf("**********   2 : leave.   **********\n");
 printf("**********   3 : view.   **********\n");
 printf("**********   4 : exit.   **********\n");
 int option;
 scanf("%d",&option);
 switch(option)
 {
  case 1:{
   park1(wait,park);
   printf(" Stop! \n");
   menu(wait,park,giveway,num,t);
   break;
  }
  case 2:{
   leave2(wait,park,giveway,num,t);
   menu(wait,park,giveway,num,t);
   break;
  }
  case 3:{
   view3(*wait,*park);
   menu(wait,park,giveway,num,t);
   break;
  }
  case 4:{
   printf("**********   Welcome to use again, thank you!  **********\n");
   break;
  }
  default:{
   printf("**********   Please enter the correct instruction!  **********\n");
   menu(wait,park,giveway,num,t);
   break;
  }
 }

}

int linklength(LinkQueue q)
{
 int i = 0;
 while(q.front != q.rear)
 {
  i++;
  q.front = q.front->next;
 }
 return i;
}

int enqueue(LinkQueue *q,int num,int t)
{
 QueueNode newnode = (QueueNode)malloc(sizeof(struct QueueNode));
 if(NULL == newnode)
 {
  return F;
 }
 newnode->infom.number = num;
 newnode->infom.time = t;
 newnode->next = NULL;
 q->rear->next = newnode;
 q->rear = newnode;
 return T;
}

int dequeue(LinkQueue *q,int *num,int *t)
{
 if(q->front == q->rear)
 {
  printf("the queue is empty!\n");
  return F;
 }
 *num = q->front->next->infom.number;
 *t = q->front->next->infom.time;
 QueueNode temp = q->front->next;
 q->front->next = temp->next;
 if(temp->next == NULL)
 {
  q->rear = q->front;
 }
 free(temp);
 return T;
}

void park1(LinkQueue *wait,LinkQueue *park)
{
 printf(" Please enter your car number and stop time \n");
 int num,t;
 scanf("%d,%d",&num,&t);
 if(linklength(*park) >= MAX)
 {
  printf(" The parking lot is full, enter the holding area! \n");
  enqueue(wait,num,t);
 }
 else
 {
  enqueue(park,num,t);
 }
}

int push(LinkStack *s,int num,int t)
{
 StackNode newnode = (StackNode)malloc(sizeof(struct stack));
 if(NULL == newnode)
 {
  return F;
 }
 newnode->data.number = num;
 newnode->data.time = t;
 newnode->next = s->top;
 s->top = newnode;
 s->count++;
 return T;
}

int pop(LinkStack *s,int *num,int *t)
{
 if(0 == s->count)
 {
  printf("the stack is empty !\n");
  return F;
 }
 *num = s->top->data.number;
 *t = s->top->data.time;
 StackNode temp = s->top;
 s->top = s->top->next;
 free(temp);
 s->count--;
 return T;
}

void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t)
{
 printf(" Please enter the number of the vehicle to leave \n");
 int leavenumber;
 scanf("%d",&leavenumber);
 int i = 0;
 QueueNode head = park->front;
 while(head != park->rear)
 {
  if(head->next->infom.number != leavenumber)
  {
   head = head->next;
   i++;
  }
  else
   break;
 }
 int j = 0;
 if(i <= MAX-1)
 {
  while(j != i)
  {
   dequeue(park,&num,&t);
   push(giveway,num,t);
   j++;
  }
  dequeue(park,&num,&t);
 }
 else
 {
  printf(" Check this car! \n");
 }
 while(giveway->top != NULL)
 {
  pop(giveway,&num,&t);
  enqueue(park,num,t);
 }
 if(linklength(*wait) != 0)
 {
  dequeue(wait,&num,&t);
  enqueue(park,num,t);
 }
}

void view3(LinkQueue wait,LinkQueue park)
{
 printf("********************  Current parking lot condition  ********************\n");
 printf(" The parking lot, %d Total parking Spaces, current parking lots %d Measuring car, waiting area total %d Amount of car \n",
  MAX,linklength(park),linklength(wait));
 printf("**************************************************************\n");
 printf(" car   No. : ");
 QueueNode head1 = park.front;
 QueueNode head2 = park.front;
 while(head1 != park.rear)
 {
  printf("%d ",head1->next->infom.number);
  head1 = head1->next;
 }
 printf("\n");
 printf(" Parking time: ");
 while(head2 != park.rear)
 {
  printf("%d ",head2->next->infom.time);
  head2 = head2->next;
 }
 printf("\n");
}

For more information, please pay attention to the topic management System Development.


Related articles: