![]() |
#2
寒风中的细雨2012-10-06 16:16
|

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define TRUE 1
typedef int ElemType;
typedef struct Circle_QueueList
{
ElemType data;
struct Circle_QueueList *next;
}CirQ_List,*Node;
void InitialQ(CirQ_List *Q)
{ /* is here expected to assign a fixed memory to Q? */
Q = (Node)malloc(sizeof(CirQ_List));
Q->next = Q;
}
int Enter_Queue(CirQ_List *Q,ElemType e)
{
Node q,rear;
rear = Q;
if( (q = (Node)malloc(sizeof(CirQ_List)) )==NULL)
return ERROR;
q->data = e;
while(rear->next != Q)
rear = rear->next;
rear->next = q;
q->next = Q;
return TRUE;
}
int Out_Queue(CirQ_List *Q,ElemType *e)
{
Node P;
P = (Node)malloc(sizeof(CirQ_List));
if(Q->next == Q)
return ERROR;
P = Q->next;
*e = P->data;
Q->next = P->next;
free(P);
return TRUE;
}
void main()
{
CirQ_List CQL,*CQL_p;
ElemType Enter_v,Out_v;
InitialQ(&CQL);
puts("insert elements into the queue:");
scanf("%d",&Enter_v);
while(Enter_v!=0)
{
Enter_Queue(&CQL,Enter_v);
scanf("%d",&Enter_v);
}
CQL_p = CQL.next;
while(CQL_p!=(&CQL))
{
printf("%5d",CQL_p->data);
CQL_p = CQL_p->next;
}
// Out_Queue(&CQL,&Out_v);
// printf("%d\n",Out_v);
getch();
}
#include<malloc.h>
#define ERROR 0
#define TRUE 1
typedef int ElemType;
typedef struct Circle_QueueList
{
ElemType data;
struct Circle_QueueList *next;
}CirQ_List,*Node;
void InitialQ(CirQ_List *Q)
{ /* is here expected to assign a fixed memory to Q? */
Q = (Node)malloc(sizeof(CirQ_List));
Q->next = Q;
}
int Enter_Queue(CirQ_List *Q,ElemType e)
{
Node q,rear;
rear = Q;
if( (q = (Node)malloc(sizeof(CirQ_List)) )==NULL)
return ERROR;
q->data = e;
while(rear->next != Q)
rear = rear->next;
rear->next = q;
q->next = Q;
return TRUE;
}
int Out_Queue(CirQ_List *Q,ElemType *e)
{
Node P;
P = (Node)malloc(sizeof(CirQ_List));
if(Q->next == Q)
return ERROR;
P = Q->next;
*e = P->data;
Q->next = P->next;
free(P);
return TRUE;
}
void main()
{
CirQ_List CQL,*CQL_p;
ElemType Enter_v,Out_v;
InitialQ(&CQL);
puts("insert elements into the queue:");
scanf("%d",&Enter_v);
while(Enter_v!=0)
{
Enter_Queue(&CQL,Enter_v);
scanf("%d",&Enter_v);
}
CQL_p = CQL.next;
while(CQL_p!=(&CQL))
{
printf("%5d",CQL_p->data);
CQL_p = CQL_p->next;
}
// Out_Queue(&CQL,&Out_v);
// printf("%d\n",Out_v);
getch();
}