C language implementation of indefinite long linked list queue

Source: Internet
Author: User

#ifndef _const_h_
#define _const_h_

#include <stdio.h>
#include <stdlib.h>

typedef enum
{
False = 0,
True,
}bool;

typedef int ELEMTYPE;

#define QUEUE_MAX_SIZE 10

#define STACK_INIT_SIZE 10
#define STACK_INCREMENT_SIZE 2


#define NULL ((void *) 0)

typedef enum
{
NORMAL = 0,
ERROR,
Underflow,
OVERFLOW,
Statuscount,
}status;

#endif

#ifndef _linked_queue_h_
#define _linked_queue_h_

#include "Const.h"

typedef struct NODE
{
Elemtype data;
struct node *pnext;
}node, *pnode;

typedef struct LINKEDQUEUE
{
Pnode Pfront;
Pnode prear;
}linkedqueue, *plinkedqueue;

Status initlinkedqueue (Plinkedqueue PQ);

Bool islinkedqueueempty (Plinkedqueue PQ);

Bool Enlinkedqueue (Plinkedqueue PQ, elemtype elem);

Bool Delinkedqueue (Plinkedqueue PQ, Elemtype *e);

void Destorylinkedqueue (Plinkedqueue pQ);

void Clearlinkedqueue (Plinkedqueue pQ);

Status Getlinkedqueuehead (Plinkedqueue PQ, Elemtype *e);

int Getlinkedqueuelength (Plinkedqueue PQ);

Bool invertlinkedqueue_static (Plinkedqueue PQ);

Bool invertlinkedqueue_dynamic (Plinkedqueue PQ);

#endif

#include "LinkedQueue.h"
#include "Const.h"
#include "StaticStack.h"
#include "DynamicStack.h"

Status initlinkedqueue (Plinkedqueue PQ)
{
Pnode PN = (pnode) malloc (sizeof (Node));
if (PN = = Null)
{
printf ("No accessable free memory.\n");
return ERROR;
}
Pn->pnext = Null;
Pq->pfront = PN;
Pq->prear = PN;
}

Bool islinkedqueueempty (Plinkedqueue PQ)
{
if (Pq->pfront = = pq->prear)
{
return True;
}
Else
{
return False;
}
}

Bool Enlinkedqueue (Plinkedqueue PQ, Elemtype elem)
{
Pnode Ptempnode = (pnode) malloc (sizeof (Node));
if (Ptempnode = = Null)
{
printf ("No accessable free memory.\n");
return False;
}

Ptempnode->data = Elem;
Ptempnode->pnext = Null;

Pq->prear->pnext = Ptempnode;
Pq->prear = Ptempnode;
}

Bool Delinkedqueue (Plinkedqueue PQ, Elemtype *e)
{
if (Islinkedqueueempty (PQ))
{
printf ("The Queue is empty.\n");
return False;
}
Else
{
Pnode Ptempnode = pq->pfront->pnext;
*e = ptempnode->data;
Pq->pfront->pnext = ptempnode->pnext;
Only have one node in this linked queue.
if (pq->prear = = Ptempnode)
{
Pq->prear = pq->pfront;
}
Free (Ptempnode);
return True;
}
}

void Destorylinkedqueue (Plinkedqueue pQ)
{
Pnode p = pq->pfront;
Pnode q = Null;
while (P! = Null)
{
Q = p;
p = p->pnext;
Free (q);
}
Pq->pfront = Null;
Pq->prear = Null;
}

void Clearlinkedqueue (Plinkedqueue pQ)
{
Pnode p = pq->pfront->pnext;
Pnode q = Null;
while (P! = Null)
{
Q = p;
p = p->pnext;
Free (q);
}
Pq->pfront->pnext = Null;
Pq->prear = pq->pfront;
}

Status Getlinkedqueuehead (Plinkedqueue PQ, Elemtype *e)
{
if (Islinkedqueueempty (PQ))
{
printf ("The linked queue is empty.\n");
return ERROR;
}
*e = pq->pfront->pnext->data;
return NORMAL;
}

int Getlinkedqueuelength (Plinkedqueue PQ)
{
if (Islinkedqueueempty (PQ))
{
return 0;
}
int Count = 0;
Pnode ptemp = pq->pfront;

while (ptemp->pnext! = Null)
{
count++;
Ptemp = ptemp->pnext;
}
return Count;
}

Status Queuetraverse (Plinkedqueue pQ, Void (*func) (elemtype*))
{
Pnode ptemp = pq->pfront->pnext;
while (ptemp! = Null)
{
Func (&ptemp->data);
Ptemp = ptemp->pnext;
}
return NORMAL;
}

Bool invertlinkedqueue_static (Plinkedqueue PQ)
{
Pstaticstack PSS = (pstaticstack) malloc (sizeof (staticstack));
if (NORMAL! = Initstaticstack (PSS))
{
return False;
}

Elemtype V;
while (! Islinkedqueueempty (PQ))
{
Delinkedqueue (PQ, &v);
Pushstaticstack (PSS, V);
}

while (! Isstaticstackempty (PSS))
{
Popstaticstack (PSS, &v);
Enlinkedqueue (PQ, V);
}
Destorystaticstack (PSS);
return True;
}

Bool invertlinkedqueue_dynamic (Plinkedqueue PQ)
{
Pdynamicstack PDS = (pdynamicstack) malloc (sizeof (dynamicstack));
if (NORMAL! = Initdynamicstack (PDS))
{
return False;
}

Elemtype V;
while (! Islinkedqueueempty (PQ))
{
Delinkedqueue (PQ, &v);
Pushdynamicstack (PDS, V);
}

while (! Isdynamicstackempty (PDS))
{
Popdynamicstack (PDS, &v);
Enlinkedqueue (PQ, V);
}
Destorydynamicstack (PDS);
return True;
}

C language implementation of indefinite long linked list 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.