#2
林月儿2016-03-27 09:36
程序代码: #include <stdio.h> #include <malloc.h> #define MAXSIZE 100 #define ERROR -1 #define true 0 typedef char ElementType; typedef struct LNode { ElementType a; struct LNode *next; }List; typedef struct Link { List *front,*rear; }QNde; void MakeEmpty(QNde *s) { s=(QNde*)malloc(sizeof(QNde)); s->front=s->rear=NULL; } void enter(QNde *s,ElementType x) { List *t; t=(List*)malloc(sizeof(List)); t->a=x; t->next=NULL; if(s->front==NULL) { s->front=s->rear=t; } else { s->rear->next=t; s->rear=t; } } void delete1(QNde *s) { List *q; if(s->front==NULL) printf("队空"); else { if(s->front==s->rear) { q=s->front; s->front=s->rear=NULL; } else { q=s->front; s->front=s->front->next; } free(q); } } void display(QNde *s) { List *q; q=s->front; printf("队的显示:\n"); if(s->front->next==NULL) printf("空队"); else { if(s->front->next==s->rear->next) { printf("%c",q->a); } else { while(q->next!=NULL) { printf("%c",q->a); q=q->next; } printf("%c",q->a); } } printf("\n"); } main() { QNde *qu; printf("初始化队列 qu\n"); // MakeEmpty(qu); qu=(QNde*)malloc(sizeof(QNde)); qu->front=qu->rear=NULL; printf("依次入队 a,b,c,d 元素\n"); enter(qu,'a'); enter(qu,'b'); enter(qu,'c'); enter(qu,'d'); display(qu); printf("出队一次\n"); delete1(qu); display(qu); printf("出队一次\n"); delete1(qu); display(qu); } |
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100
#define ERROR -1
#define true 0
typedef char ElementType;
typedef struct LNode {
ElementType a;
struct LNode *next;
}*List;
typedef struct Link
{
List front,rear;
}*QNde;
MakeEmpty(QNde *s)
{
(*s)->front=(*s)->rear=(List)malloc(sizeof(struct LNode));
(*s)->front=(*s)->rear;
(*s)->front->next=NULL;
}
void enter(QNde *s,ElementType x)
{
List t;
t=(List)malloc(sizeof(struct LNode));
t->a=x;
t->next=NULL;
if((*s)->front->next==NULL)
{
(*s)->front->next=t;
(*s)->rear->next=t;
}
else
{
t=(*s)->rear->next->next;
(*s)->rear->next=t;
}
}
void delete(QNde *s)
{
List q;
if((*s)->front->next==NULL)
printf("队空");
else
{
if((*s)->front->next==(*s)->rear->next)
{
q=(*s)->front->next;
(*s)->front->next=NULL;
(*s)->rear->next=NULL;
}
else
{
q=(*s)->front->next;
(*s)->front->next=q->next;
}
free(q);
}
}
void display(QNde *s)
{
List q;
q=(*s)->front->next;
printf("队的显示:\n");
if((*s)->front->next==NULL)
printf("空队");
else
{
if((*s)->front->next==(*s)->rear->next)
{
printf("%c",q->a);
}
else
{
while(q->next!=NULL)
{
printf("%c",q->a);
q=q->next;
}
printf("%c",q->a);
}
}
printf("\n");
}
main()
{
QNde *qu;
printf("初始化队列 qu\n");
MakeEmpty(qu);
printf("依次入队 a,b,c,d 元素\n");
enter(qu,'a');
enter(qu,'b');
enter(qu,'c');
enter(qu,'d');
display(qu);
printf("出队一次\n");
delete(qu);
display(qu);
printf("出队一次\n");
delete(qu);
display(qu);
}
问下哪错了,运行不出来,谢谢....