Algorithm-Data Structure-queue

Source: Internet
Author: User
Algorithm-Data Structure-queue-general Linux technology-Linux programming and kernel information. The following is a detailed description. /*************************************** ******************
Title: queue. c
Author: zhanglong
Time: 200508
**************************************** ******************/




# Include
# Include
# Include




# Define MEMASSERT (p, s) if (! P) {fprintf (stderr, "Queue-No Memory: % s! \ N ", s); exit (0 );}

# Ifdef DEBUG_AC
Int max_memory = 0;
# Endif

/*------------------------------------------------*/
Void * AC_MALLOC (int n)
{
Void * p;
P = malloc (n );
# Ifdef DEBUG_AC
If (p)
Max_memory + = n;
# Endif
Return p;
}

/*----------------------------------------------*/
Void AC_FREE (void * p)
{
If (p)
Free (p );
}

/* ------ Simple queue node ------*/
Typedef struct _ qnode
{
Int data;
Struct _ qnode * next;
} QNODE;

/* ------- Simple QUEUE Structure -------*/
Typedef struct _ queue
{
QNODE * head, * tail;
Int count;
} QUEUE;





/* ===================== Function declaration ============================== = */
Void queue_init (QUEUE * s );
Void queue_add (QUEUE * s, int data );
Int queue_remove (QUEUE * s );
Int queue_count (QUEUE * s );
Void queue_free (QUEUE * s );




/* ====================== Function implementation ============================= */
/* ------- Queue init -------*/
Void queue_init (QUEUE * s)
{
S-> head = s-> tail = 0;
S-> count = 0;
}


/* ------------ Add Tail Item to queue ----------------*/
Void queue_add (QUEUE * s, int data)
{
QNODE * q;

If (! S-> head ){
Q = s-> tail = s-> head = (QNODE *) AC_MALLOC (sizeof (QNODE ));
MEMASSERT (q, "queue_add ");
Q-> data = data;
Q-> next = 0;
}
Else {
Q = (QNODE *) AC_MALLOC (sizeof (QNODE ));
MEMASSERT (q, "queue_add ");
Q-> data = data;
Q-> next = 0;
S-> tail-> next = q;
S-> tail = q;
}

S-> count ++;
}


/* ------- Remove Head Item from queue -------------*/
Int queue_remove (QUEUE * s)
{
Int data = 0;
QNODE * q;
If (s-> head ){
Q = s-> head;
Data = q-> data;
S-> head = s-> head-> next;
S-> count --;
If (! S-> head ){
S-> tail = 0;
S-> count = 0;
}
AC_FREE (q );
}
Return data;
}


/* --------------- Queue count -----------------*/
Int queue_count (QUEUE * s)
{
Return s-> count;
}


/* --------------- Queue free ----------------*/
Void queue_free (QUEUE * s)
{
While (queue_count (s )){
Queue_remove (s );
}
}




/* =============== Main function ========= */
Int main (int argc, char * argv [])
{
QUEUE * s;
S = (QUEUE *) AC_MALLOC (sizeof (QUEUE ));
Int I;

Queue_init (s );

For (I = 0; I <100; I + = 3 ){
Queue_add (s, I );
}
Printf ("queue count is % d \ n", queue_count (s ));

Queue_remove (s );
Printf ("queue count is % d \ n", queue_count (s ));
Queue_remove (s );
Printf ("queue count is % d \ n", queue_count (s ));
Queue_remove (s );
Printf ("queue count is % d \ n", queue_count (s ));

Printf ("queue head is % d \ n", s-> head-> data );
Printf ("queue tail is % d \ n", s-> tail-> data );

Queue_free (s );
Printf ("queue count is % d \ n", queue_count (s ));

}

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.