Stack and queue applications: parking lot management

Source: Internet
Author: User

The parking lot is a long and narrow channel for parking N vehicles, and there is only one door for the car to enter and exit. In the parking lot, cars are arranged in the order of arrival from north to south (assuming the door is at the southernmost tip ). If N vehicles have been fully parked in the garage, the subsequent cars need to wait on the sidewalk outside the garage. When a car leaves, the first car on the driveway can be opened. When a car in the parking lot is about to leave, the vehicle that enters after it must first exit the parking lot to make way for it. After the car leaves the door, other vehicles will return to the parking lot in the original order. When a car leaves the parking lot, it should be paid according to the length of its stay (no fee is charged for the stay time on the sidewalk ).

Write a program to simulate the above management process. It is required to simulate the parking lot with the stack in sequence and simulate the convenient path with the chain queue. The data of the arrival or departure of a car is read from the terminal. Each group of data includes three items: (1) "Arrival" or "departure"; (2) the vehicle license number; ③ "Arrival" or "departure" moment (Obtains the system time, in seconds.). The output information corresponding to the input information of each group is: if the vehicle is arriving, the location of the vehicle in the parking lot or on the sidewalk; if the vehicle is departing, the time of its stay in the parking lot and the payable fees are displayed. (Note: You need to set up another stack to temporarily park a car that exits from the parking garage to make way .)

The Code is as follows:

 

# Include <stdio. h> # include <time. h> # include <stdlib. h> # include <string. h> # include <math. h> # define park_size 5 // define the car's struct typedef struct {int platenum; time_t aritine; // The Database Import time time_t leatime; // The Database Export time} car; // the garage, use the sequential stack to represent typedef struct {car park [park_size]; int top;} seqparklist; // convenient path. Use the chain queue to represent typedef struct loadnode {car loadnode; loadnode * Next ;} loadnode, * loadlist; // park1 is the garage, and park2 is the vehicle seqparkli to be temporarily shelved in park1. St park1, park2; // load is the convenient loadlist load; // initialize the garage (stack) void initseqstack (seqparklist * Top) {top-> Top =-1 ;} // initialize the channel (Queue) void initqueue (loadlist * q) {* q = (loadlist) malloc (sizeof (loadnode); (* q)-> next = NULL ;} // car exit (exit Queue) int deletqueue (car * car) {loadnode * tcar; tcar = load-> next; If (tcar = NULL) return 0; * car = tcar-> loadnode; load-> next = tcar-> next; free (tcar); return 1 ;}// the car enters the path, and return the int ent at the location of the trail. Erqueue (car * car) {// tail insert create queue int order = 0; loadnode * P1, * P2; P1 = P2 = load; loadnode * newcar = (loadnode *) malloc (sizeof (loadnode); // find the end of the team and return the location where the team is about to be placed. While (P1! = NULL) {order ++; P2 = p1; P1 = p1-> next;} newcar-> loadnode = * car; P2-> next = newcar; newcar-> next = NULL; return order;} // determine whether the garage is fully stopped bool isfull () {If (park1.top = park_size-1) return true; return false ;} // get the car into the garage (stack) int push (seqparklist * s, car * car) {If (S-> Top = park_size-1) Return-1; s-> top ++; // obtain the time (& Car-> aritine) of the current system when entering the database; s-> park [S-> top] = * car; return S-> top;} // outbound vehicle (outbound stack) int POP (seqpa Rklist * s, car * car) {If (S-> Top =-1) return S-> top; * car = s-> park [S-> top]; // obtain the time (& Car-> leatime) of the current system when exiting the garage; s-> top --; return S-> top + 1 ;} // get the outermost car of the garage (get the top element of the stack) int gettop (seqparklist * s, car * car) {If (S-> Top =-1) return S-> top; * car = s-> park [S-> top]; return S-> top;} // return to the garage (stack) empty bool isparkempty (seqparklist * Park) {If (park-> Top =-1) return true; return false ;} // compare the information of two vehicles bool comcar (CAR C1, Car C2) {If (c1.platenum = c2.platenum) return true; return false;} // perform the void Carin (car * car) {int order when the vehicle arrives; // as long as the car arrives, it will enter the convenient path. Next we will see if we want to enter the garage order = enterqueue (CAR); If (isfull ()) {// If the garage is full, print the information printf ("car in the driveway % d position \ n", order);} else {// otherwise the garage is not full of car tcar; deletqueue (& tcar); // exit, order = Push (& park1, & tcar); // enter the garage printf ("car in the garage % d position \ n ", order + 1) ;}}// the vehicle leaves for the corresponding operation void carout (car * car) {If (isparkempty (& par K1) {// If the garage is empty, print the relevant information printf ("the garage is empty! \ N ") ;}else {car C1; gettop (& park1, & C1); // If the outermost side of the garage is not the car to be checked out, pop up from the stack // save it to another stack until the outermost side of the garage is the car to be checked out while (! Comcar (C1, * car) {Pop (& park1, & C1); push (& park2, & C1); gettop (& park1, & C1 );} int order = POP (& park1, & C1); // output the departure time of the delivery car in the parking lot and calculate the fee. Assume 10 seconds, 2 RMB, 4 RMB printf for 11-20 s ("this car stays in the garage % LDS and costs % d RMB (10 S/2 RMB )! \ N ", c1.leatime-c1.aritine, (c1.leatime-c1.aritine + 9)/10*2); // then put the car in park2 back into park1 while (! Isparkempty (& park2) {Pop (& park2, & C1); push (& park1, & C1) ;}// a car exists in the garage, the car on the driveway will enter the Garage Car C1; If (deletqueue (& C1) {push (& park1, & C1) ;}} int main () {initqueue (& load); initseqstack (& park1); initseqstack (& park2); seqparklist park1, park2; loadlist load; car car1; printf ("Enter the license plate number, action \ n "); char action [6]; scanf (" % d % s ", & car1.platenum, action); // ends the program until the input license plate number is 0 while (car1.platenum) {If (strcmp (action, "Arrive") = 0) {Carin (& car1);} else if (strcmp (action, "quit") = 0) {carout (& car1) ;}printf ("Enter the license plate number, Action \ n"); scanf ("% d % s", & car1.platenum, Action );}}

 

 

Running result:

Stack and queue applications: parking lot management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.