|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我写的:
#include <stdio.h>
#include <stdlib.h>
typedef char Elemtype;
typedef struct QNode
{
Elemtype data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
initQueue(LinkQueue *q)
{
q->front=q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!q->front)
{
exit(0);
}
q->front->next=NULL;
}
InsertQueue(LinkQueue *q, Elemtype e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!q->front)
{
exit(0);
}
p->data=e;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
void DeleteQueue (LinkQueue *q , Elemtype *e)
{
QueuePtr p;
if(q->front = q-> rear)
{
return;
}
p=q->front->next;
*e=p->data;
q->front->next=p->next;
if(q->rear=p)
{
q->rear=q->front;
}
free(p);
}
int main()
{
Elemtype e;
LinkQueue q;
initQueue(&q);
scanf("%c",&e);
while(e != '#')
{
InsertQueue(&q,e);
scanf("%c",&e);
DeleteQueue(&q,&e);
printf("%c",e);
}
return 0 ;
}
小甲鱼写的:
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct QNode
{
ElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct
{
QueuePtr front, rear;
} LinkQueue;
initQueue(LinkQueue *q)
{
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if( !q->front )
exit(0);
q->front->next = NULL;
}
InsertQueue(LinkQueue *q, ElemType e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if( !q->front )
exit(0);
p->data = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
void DeleteQueue(LinkQueue *q, ElemType *e)
{
QueuePtr p;
if( q->front == q->rear )
return;
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if( q->rear == p )
{
q->rear = q->front;
}
free(p);
}
int main()
{
ElemType e;
LinkQueue q;
initQueue(&q);
printf("请输入一个字符串,以井号键结束输入:");
scanf("%c", &e);
while( e != '#' )
{
InsertQueue( &q, e );
scanf("%c", &e);
}
printf("打印队列中的元素:");
while( q.front != q.rear )
{
DeleteQueue( &q, &e );
printf("%c", e);
}
return 0;
}
你的插入和删除写在了同一个循环中
比如是你输入 abc#
while循环外的scanf接收了a
然后将其 InsertQueue
而while循环内的scanf接收了b
执行 DeleteQueue
此时e的值是 b ,所以打印的是b
|
|