Data Structure queue C

Source: Internet
Author: User

 

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

# Define queuelen 15

Typedef struct
{
Char name [10];
Int age;
} Data;

Typedef struct
{
Data data [queuelen];
Int head;
Int tail;
} Sqtype;

Sqtype * sqtypeinit ()
{
Sqtype * q;
If (q = (sqtype *) malloc (sizeof (sqtype )))
{
Q-> head = 0;
Q-> tail = 0;
Return Q;
}
Else
{
Return NULL;
}
}

Int sqtypeisempty (sqtype * q)
{
Int temp;
Temp = Q-> head = Q-> tail;
Return temp;
}

Int sqtypeisfull (sqtype * q)
{
Int temp;
Temp = Q-> tail = queuelen;
Return temp;
}

Void sqtypeclear (sqtype * q)
{
Q-> head = 0;
Q-> tail = 0;
}

Void sqtypefree (sqtype * q)
{
If (q)
{
Free (Q );
}
}

Int insqtype (sqtype * q, data)
{
If (Q-> tail = queuelen)
{
Printf ("the queue is full! Operation failed! \ N ");
Return 0;
}
Else
{
Q-> data [q-> tail ++] = data;
Return 1;
}
}

Data * outsqtype (sqtype * q)
{
If (Q-> head = Q-> tail)
{
Printf ("the queue is empty! Operation failed! \ N ");
Exit (0 );
}
Else
{
Return & (Q-> data [q-> head ++]);
}
}

Data * peeksqtype (sqtype * q)
{
If (sqtypeisempty (q ))
{
Printf ("the queue is empty! \ N ");
Return NULL;
}
Else
{
Return & (Q-> data [q-> head]);
}
}

Int sqtypelen (sqtype * q)
{
Int temp;
Temp = Q-> tail-Q-> head;
Return temp;
}

Int main ()
{
Sqtype * queue;
Data data;
Data * data1;

queue = sqtypeinit ();
printf ("Incoming queue operation: \ n");
printf ("input name age: \ n ");
DO
{< br> scanf (" % S % d ", Data. name, & data. age);
If (strcmp (data. name, "0") = 0)
{< br> break;
}< br> else
{< br> insqtype (queue, data);
}< BR >}while (1);

Do
{
Printf ("out queue operation: press any key for out queue operation: \ n ");
Getchar ();
Data1 = outsqtype (Queue );
Printf ("the data in the output queue is (% s, % d) \ n", data1-> name, data1-> age );
} While (1 );

Sqtypefree (Queue );
}

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.