|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef char Elemtype;
typedef struct Qnode
{
Elemtype data;
struct Qnode *next;
}Qnode, * queueprt;
typedef struct
{
queueprt front, read;
}qutelist;
void createlist(qutelist* s)
{
s->front = s->read = (queueprt)malloc(sizeof(Qnode));
if (!s->front)
{
exit(0);
}
s->front->next = NULL;
}
void push(qutelist* s, Elemtype e)
{
queueprt p;
p = (queueprt)malloc(sizeof(Qnode));
if (p == NULL)
{
exit(0);
}
p->data = e;
p->next = NULL;
s->read->next = p;
s->read = p;
free(p)//这里加释放掉p就会出错
}
void pop(qutelist* s, Elemtype* e)
{
queueprt p;
p = (queueprt)malloc(sizeof(Qnode));
if (s->front == s->read)
{
exit(0);
}
p = s->front->next;
*e = p->data;
s->front->next = p->next;
if (s->read == p)
{
s->read = s->front;
}
free(p);
}
void freelist(qutelist* s)
{
while (s->front)
{
s->read = s->front->next;
free(s->front);
s->front = s->read;
}
}
int nulllist(qutelist s)
{
if (s.front == s.read)
{
return 1;
}
else
return 0;
}
void main()
{
qutelist s;
Elemtype c;
createlist(&s);
scanf_s("%c", &c);
while (c != '#')//输入一串字符,以#号结束
{
push(&s, c);
scanf_s("%c", &c);
}
getchar();
pop(&s, &c);
while (!nulllist(s))
{
printf("%c", c);
pop(&s, &c);
}
printf("%c", c);
} |
|