Sequential Queue Basic Operations

Source: Internet
Author: User

#include "stdafx.h"
#include "stdio.h"
#include "Stdlib.h"
#define SMALL 1

#if (SMALL)
#define MAX 7
int Queuedata[max] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '};
#else
#define MAX 14
int Queuedata[max] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N '};
#endif

/* Sequential queue type definition */
typedef struct
{
int Data[max];
int head;
int tail;
}sqqueue;
/* Chain queue type definition */
struct Linkqueue
{
int data;
struct Linkqueue *link;
};

typedef struct LINKQUEUE Lkqueue;
//-----------------------------------------------------------------------------------------
/* Sequential Queue basic operation function definition part * *
Initialization functions for sequential queues sqinitialize ()
Sqqueue *sq_initialize ()
{
Sqqueue *p;
p = (Sqqueue *) malloc (sizeof (sqqueue));
if (p==null)
{
return (NULL);
}
Else
{
P->data[0] = 0;
P->head = 0;
P->tail = 0;
return (p);
}
}

Null-qisempty function for sequential queues ()
int Qisempty (Sqqueue *queue)
{
if (Queue->head==queue->tail)
return (1);
Else
return (0);
}

A full function of a sequential queue qisfull ()
int Qisfull (Sqqueue *queue)
{
if (Queue->head==max)
return (1);
Else
return (0);
}

Queue functions for sequential queues Qinqueue ()
int Qinqueue (sqqueue *queue,int data)
{
if (Queue->tail==max)
{
printf (the queue is full!) \ n ");
return (0);
}
Else
{
queue->data[queue->tail++] = data;
return (1);
}
}

Out-team functions for sequential queues Qoutqueue ()
int Qoutqueue (sqqueue *queue,int *p)
{
if (Queue->head==queue->tail)
{
printf (the queue is empty!) \ n ");
return (0);
}
Else
{
*p = queue->data[queue->head++];
return (1);
}
}

Data queue operation function for sequential queues Qinputvalue ()
void Qinputvalue (Sqqueue *queue,int array[])
{
int i=0;
while (Qinqueue (Queue,array[i]))
printf ("queue[%d] =%c\t", i,array[i++]);
}

Data out-team operation function Qoutputvalue of sequential queues ()
void Qoutputvalue (Sqqueue *queue)
{
int i,queuedata;
i=0;
while (Qoutqueue (Queue,&queuedata))
printf ("queue[%d] =%c\t", i++,queuedata);
printf ("\ n");
}
//-----------------------------------------------------------------------------------------
/* Chain-type queue basic operation function definition part * *
initialization function for chained queues lk_initialize ()
Lkqueue *lk_initialize ()
{
Lkqueue *p;
p = (Lkqueue *) malloc (sizeof (lkqueue));
if (p==null)
{
return (NULL);
}
Else
{
P->data = 0;
P->link = NULL;
return (p);
}
}
Queue function of chained queues Lkinqueue ()
Note: When you join the team, the parameter is the tail pointer and the return value is the tail pointer
Lkqueue *lkinqueue (lkqueue *tail,int data)
{
Lkqueue *p;
p = (Lkqueue *) malloc (sizeof (lkqueue));
if (p==null)
{
printf ("Memory overflow when adding chained queue elements!") \ n ");
return (tail);
}
Else
{
P->data = data;
P->link = tail;
return (p);
}
}
Out-team function of chained queues Lkinqueue ()
Note: When you are out of the team, the parameter is the first pointer and the return value is the first pointer of the team
Lkqueue *lkoutqueue (lkqueue *head,int *data)
{
*p = head->data;

Lkqueue *p;
p = (Lkqueue *) malloc (sizeof (lkqueue));
if (p==null)
{
printf ("Memory overflow when creating chained queues!") \ n ");
return (tail);
}
Else
{
P->data = data;
P->link = tail;
return (p);
}
}
//-----------------------------------------------------------------------------------------
int main (int argc, char* argv[])
{
Sqqueue *queue1;
Lkqueue **lkhead,**lktail;
Queue1 = Sq_initialize ();
if (queue1==null)
{
printf ("Memory overflow when creating sequential queues!") \ n ");
return 0;
}
Else
{
printf ("\ n sequential queues [queued] data ... \ n");
Qinputvalue (Queue1,queuedata);
printf ("\ n order queue [out of team] data ... \ n");
Qoutputvalue (queue1);
}
printf ("\ n run complete!") \ n ");
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.