- 0
- 0
- 约2.17千字
- 约 6页
- 2018-05-27 发布于湖北
- 举报
利栈和队列进行组数逆置操作算法
# include stdio.h
# include stdlib.h
# include malloc.h
# define size 6
typedef struct Node
{
int data;
struct Node *pNext;
}NODE, *PNODE;
struct stack
{
PNODE ptop;
};
struct Queue
{
int *pBase;
int front;
int rear;
};
void init_s(stack *);
void init_q(Queue *);
void Enqueue(Queue *, int);
void OutQpushS(Queue *, stack *);
void PopOut(stack *, Queue *);
void Out(Queue *);
void Travese(Queue *);
int main(void)
{
stack S;//建立栈S
init_s(S);
Queue Q;//建立循环队列Q
init_q(Q);
Enqueue(Q, 1);
Enqueue(Q, 2);
Enqueue(Q, 3);
printf(对列中的元素是:);
Travese(Q);
printf(逆置后对列中的元素是: );
OutQpushS(Q, S);// 出对入栈
PopOut(S, Q);//出栈并入队
Travese(Q);//对Q 出对
return 0;
}
void init_s(stack *pS)
{
pS-ptop = NULL;
}
void init_q(Queue *pQ)
{
pQ-pBase = (int *)malloc(sizeof(int) * size);
if (pQ-pBase == NULL)
{
printf(动态内存分配失败!);
exit(-1);
}
pQ-front = pQ-rear = 0;
}
void Enqueue(Queue * pQ, int val)
{
pQ-pBase[pQ-rear] = val;
pQ-rear = (pQ-rear + 1) % size;
}
void OutQpushS(Queue *pQ, stack *pS)
{
while (pQ-front != pQ-rear)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (pNew == NULL)
{
printf(动态内存分配失败!\n);
exit(-1);
}
pNew-data = pQ-pBase[pQ-front];
pQ-front = (pQ-front + 1) % size;
pNew-pNext = pS-ptop;
pS-ptop = pNew;
}
}
void PopOut(stack *pS, Queue *pQ)
{
while (pS-ptop != NULL)
{
pQ-pBase[pQ-rear]= pS-ptop-data;
pQ-rear = (pQ-rear + 1) % size;
pS-ptop = pS-ptop-pNext;
}
}
void Travese(Queue *pQ)
{
int q = pQ-front;
while(q != pQ-rear)
{
printf(%d
原创力文档

文档评论(0)