Dynamic Array cyclic queue in C Language

Source: Internet
Author: User

# Include <iostream>
# Include <assert. h>
Using namespace STD;
/*
Struct Node
{
Int Info;
Node * next;
};

Struct queuelk
{
Struct node * front;
Struct node * back;
};

Void init_queue (struct queuelk * q)
{
Q-> back = Q-> front = NULL;
}

Void queue_is_empty (struct queuelk * q)
{
If (Q-> front = NULL)
{
Return;
}
}

Void enqueue (struct queuelk * q, int info)
{
Struct node * temp = NULL;
Temp = (node *) malloc (sizeof (node ));
Assert (temp! = NULL );
If (Q-> front = NULL) // This is the first node of the queue.
{
Q-> front = Q-> back = temp;
Temp-> info = Info;
Return;
}
Q-> back-> next = temp;
Temp-> info = Info;
Q-> back = temp;
}

Bool dequeue (struct queuelk * q, int * info)
{
If (Q-> front = NULL)
{
Return false;
}
* Info = Q-> front-> Info;
If (Q-> front = Q-> back)
{
Free (Q-> front );
Q-> front = Q-> back = NULL;
Return true;
}
Q-> front = Q-> front-> next;
Return true;
}
*/

// This is a common queue.

// Write a circular queue.
//

 

Struct queuelk
{
Int * arr;
Int rear;
Int size;
Int front;
};

Void init_queue (struct queuelk * q, int size)
{
Q-> front =-1; // This-1 is very important
Q-> rear =-1;
If (size <= 0)
{
Printf ("the queue length is negative, please note/N ");
Exit (1 );
}
Q-> size = size;
Q-> arr = (int *) malloc (sizeof (INT) * size );
If (Q-> arr = NULL)
{
Printf ("failed space application ");
Exit (1 );
}
}

Void againmalloc (struct queuelk * q, int newsize)
{
Int * pnew = (int *) malloc (sizeof (INT) * newsize );
Int * pold = Q-> arr;
Int I = 0;
Int Limit = 0;
// Memcpy (pnew, Q-> arr, Q-> size); // is it more convenient to directly call memcpy?
Limit = (newsize> q-> size )? Q-> size: newsize;
For (; I <limit; I ++)
{
Pnew [I] = Q-> arr [I];
}
Free (Q-> ARR );
Q-> arr = pnew;
Q-> size = newsize;
}

 

Void enqueue (struct queuelk * q, int info)
{
If (Q-> front = 0 & Q-> rear + 1 = Q-> size | Q-> rear + 1 = Q-> rear)
{
Againmalloc (Q, Q-> size <1); // multiply the space
If (Q-> rear) <q-> front)
{
Int I = Q-> size> 1-1; // original queue size
Int J = Q-> size-1; // the size of the new space.
For (; I> = Q-> front; I --)
{
Q-> arr [J] = Q-> arr [I];
J --;
}
Q-> front = ++ J; // the last position J must have moved forward to another position, so ++ 1;
}
}
Q-> rear = (Q-> rear = Q-> size )? 0: ++ Q-> rear;
If (Q-> front =-1) // The first element.
{
Q-> front = 0; // The queue is full only when you catch up.
}
Q-> arr [q-> rear] = Info; // enter the team at the end of each time, and analyze the conditions used to determine if the team is full. What are the conditions?
}

Bool dequeue (struct queuelk * q, int * info)
{
Int newsize = 0; // calculate the number of elements in the new queue.
Int trysize = Q-> size;
If (Q-> front =-1) // first determine whether the team is empty
{
Return false;
}
* Info = Q-> arr [q-> front];
If (Q-> front = Q-> rear) // determine whether the team is the last element.
{
Q-> front = Q-> rear =-1; // The data structure in which the end mark is important ..
}
Else
{
Q-> front = (Q-> front = Q-> size )? 0: ++ Q-> front;
}
Newsize = (Q-> front <= Q-> rear )? Q-> rear-Q-> front + 1: Q-> rear + q-> size-Q-> front + 1;
While (newsize <= trysize> 2 & trysize> 2) // really troublesome.
{
Trysize> = 1; // The capacity is halved.
}

If (trysize <q-> size) // If the array capacity changes, move the elements in the array and then halved the capacity.
{
If (Q-> rear> = trysize & Q-> front <= Q-> rear) // If the tail index> = new space, start to move to the array element, array indexes start from 0, and the smaller the index is.
{
Int I = Q-> front;
Int J = 0;
For (; I <= Q-> rear; I ++)
{
Q-> arr [J] = Q-> arr [I];
J ++;
}
Q-> front = 0;
Q-> rear = -- J; // generate a new head and tail pointer.
}
Else if (Q-> front> q-> rear)
{
Int I = Q-> size-1;
Int J = trysize-1;
For (; I> = Q-> front; I --)
{
Q-> arr [J] = Q-> arr [I];
J --;
}
Q-> front = ++ J;
}
Againmalloc (Q, trysize );
}
Return true;
}

Int main ()
{
Struct queuelk queue;
Init_queue (& queue, 2 );
For (INT I = 0; I <5000; I ++)
{
Enqueue (& queue, I );
}
Int ou = 0;

While (dequeue (& queue, & ou ))
{
Cout <ou <Endl;
}
 
Return 0;
}

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.