An Application of stack and queue

Source: Internet
Author: User
Tags define null


I am a beginner. This is my notebook. There are many flaws in it, which make the experts laugh. Please advise. Contact me QQ 358271030.

This is a parking lot management system simulated by stacks and queues. When a car is registered, it enters the yard. If the parking lot is full, the incoming car enters the driveway and waits. Once a car leaves, the driveway automatically enters the garage.

A stack is used to simulate a parking lot, and a queue is used to simulate an off-site sidewalk.
# Include "stdio. h"
# Define NULL 0
# Define ERROR 0
# Define OK 1
# Define OVERFLOW 0
# Define STACK_INIT_SIZE 2
Typedef struct
{
Char label;
Float time;
} Car, Car2;
Typedef struct
{
Car * top;
Car * base;
Int stacksize;
} SqStack;
Int InitStack (SqStack * S)
{
S-> base = (Car *) malloc (STACK_INIT_SIZE * sizeof (Car ));
If (! (S-> base) return ERROR;
S-> top = S-> base;
S-> stacksize = STACK_INIT_SIZE;
Return OK;
}
Int StackEmpty (SqStack S)
{
If (S. top = S. base)
Return OK;
Else
Return ERROR;
}
Int StackFull (SqStack S)
{
If (S. top-S.base> = STACK_INIT_SIZE)
Return OK;
Else
Return ERROR;
}
Int Push (SqStack * S, Car e)
{
If (S-> top-S-> base> = STACK_INIT_SIZE)
Return OVERFLOW;
Else
{
* (S-> top ++) = e;
Return OK;
}
}
Int Pop (SqStack * S, Car * e)
{
If (S-> top = S-> base)
Return ERROR;
* E = * (-- (S-> top ));
}
Typedef struct
{
Car2 * top2;
Car2 * base2;
Int stacksize2;
} SqStack2;
Int InitStack2 (SqStack2 * S2)
{
S2-> base2 = (Car2 *) malloc (STACK_INIT_SIZE * sizeof (Car2 ));
If (! (S2-> top2) return ERROR;
S2-> top2 = S2-> base2;
S2-> stacksize2 = STACK_INIT_SIZE;
Return OK;
}
Int Push2 (SqStack2 * S2, Car2 e2)
{
If (S2> top2-S2> base2> = STACK_INIT_SIZE)
Return OVERFLOW;
* (S2-> top2 ++) = e2;
Return OK;
}
Int Pop2 (SqStack2 * S2, Car2 * e2)
{
If (S2-> top2 = S2-> base2)
Exit (OVERFLOW );
* E2 = * (-- (S2-> top2 ));
Return OK;
}
Int StackEmpty2 (SqStack2 S2)
{
If (S2.top2 = S2.base2)
Retu

Related Article

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.