鱼C论坛

 找回密码
 立即注册
查看: 4037|回复: 0

那位大侠能不能帮我调一下这个程序,并说一下错误原因

[复制链接]
发表于 2012-12-4 15:50:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<malloc.h>
#include<string.h>

typedef struct QNode
{
char *data;
struct QNode *next;
}QNode;

typedef struct
{
QNode *front,*rear;
}LinkQueue ;

void InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=(QNode *)malloc(sizeof(QNode));
if(!Q->front)
printf("内存申请失败!!");
Q->front->next=NULL;
printf("内存申请成功,可以继续操作。");
}

LinkQueue *EnQueue(LinkQueue *Q,char *e)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
p->data=(char *)malloc(sizeof(char));
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return Q;
}

int QueueEmpty(LinkQueue *Q)
{
if(Q->front==Q->rear)
return 1;
else
return 0;
}

void  DeQueue(LinkQueue *Q,char *e)
{
   QNode *p=Q->front;
   if(QueueEmpty(Q))
   printf("队列为空,操作无法进行!!");
   p->next=p->next;
   e=p->data;
   Q->front->next=p->next;
   if(Q->rear==p)
   free(p);
   printf("出队操作成功!!");
}

int Queuelength(LinkQueue *Q)
{
   int i=0;
   QNode *p;
   p=Q->front;
   while(p->next)
   {
     i++;
     p=p->next;
    }
   if(i==1)
   {
           printf("此队列为空队列!!");
       return 0;
   }
   else
    return i;
}

void Gethead(LinkQueue *Q,char *e)
{
         QNode *p=Q->front;
         e=p->data;
  printf("%c    ",e);
  }

void DestroyQueue(LinkQueue *Q)
{
while(Q->front)
{
   Q->rear=Q->front->next;
   free(Q->front);
   Q->front=Q->rear;
  }
  printf("清理成功!!!");
}  

void visit(char *c)
{
  printf("%c",c);  
}

void QueueTraverse(LinkQueue *Q)
{
   QNode *p=Q->front;
   while(p);
  {
    visit(p->data);
    p=p->next;       
  };
}

void main()
{
int no;
char *c;
int flag=1;
LinkQueue *W;
InitQueue(W);
while(flag)
   {
   printf("请输入你要的操作: No1入队操作  No2遍历队列 No3求队列长度 No4显示首节点字符 No5清空栈 No6退出");
   scanf("%d",&no);
   switch(no)
        {
                 case 1:   printf("Please input your character, with the blank space key end ");
                           scanf("%c",c);
                                   while(strcmp(c,' '))
                           {
                                   EnQueue(W,c);
                                   scanf("%c",c);
                                   };
                                   break;
                 case 2:   QueueTraverse(W);
                           break;
                 case 3:   Queuelength(W);
                           break;
                 case 4:   Gethead(W,c);
                           break;
                 case 5:   DestroyQueue(W);
                           break;
             case 6:   break;
                 default : break;          
                };
        printf("是否继续? No1continue No2exist");
        scanf("%d",flag);               
   }   
}
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-9 20:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表