标题:程序运行出错,请斧正一下!
只看楼主
纪昌武
Rank: 1
等 级:新手上路
帖 子:8
专家分:7
注 册:2010-11-2
结帖率:33.33%
已结贴  问题点数:20 回复次数:2 
程序运行出错,请斧正一下!
//(三)链式队列基本操作
//1、队列结点定义
//根据实际处理数据的类型定义链队中结点的值域类型ElemType
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;

typedef  struct node                 //队列结点类型定义
{   ElemType data;                 //队列的数据元素类型
     struct node  *link;             //指向后继结点的指针
}NODE;

struct QueueLk{                         //定义链队
    NODE *front,*rear;        //定义链队队头和队尾指针
};


//2、入队
NODE  *ldcr(struct QueueLk *QL,ElemType x)  //将元素x插入到链队列rear中,作为rear的新队尾
{   
    NODE *p;
    p=(NODE *)malloc(sizeof(NODE));
    p->data=x;
    p->link=NULL;  //置新结点的指针为空
      if(QL->front==NULL)        //队列为空
    QL->front=QL->rear=p;                              
    else
        QL->rear->link=p;                      //将链队列中最后一个结点的指针指向新结点
        QL->rear=p;                              //将队尾指向新结点
    return  QL->rear;
}
//3、出队
ElemType ldsc(struct QueueLk *QL)  //若链队列不为空,则删除队头元素,返回其元素值
{  
   NODE *s;
   ElemType x;
   if(QL->rear==QL->front)                 //队空,退出程序
       exit(1);  
   
s=QL->front;                //取队头保存在s中
   QL->front=QL->front->link;                       //删除队头结点
   if(QL->front->link==NULL)     //如果删除后队列为空,则处理队尾指针
       QL->rear=QL->front;
   x=s->data;                  //将刚才出队的结点值给x
    free(s) ;                               //释放出对结点的空间
   return x;
}


     main()
{
     int n,choice;
     ElemType e;
     struct QueueLk *s;
     while(choice)
     {
     printf("                 *-----------------菜单如下-----------------*\n");
     printf("                 *                                          *\n");
     printf("                 *            1.......入队列                *\n");
     printf("                 *                                          *\n");
     printf("                 *            2.......出队列                *\n");
     printf("                 *                                          *\n");
     printf("                 *            0........退出                 *\n");
     printf("                 *                                          *\n");
     printf("                 *------------------------------------------*\n\n");
     printf("请输入选择的操作:");
     scanf("%d",&choice);
   
     switch(choice)                 
             {
 case 1:
               
                    printf("请输入要入队的元素:");
              
                      scanf("%d",&e);
                     
                      ldcr(&s,e);
                    
                printf("入队成功!\n");
                break;
 case 2:
              printf("请输入要出队的元素:");
              scanf("%d",&n);
              printf("出队的元素为:\n",n);
              printf("%d ",ldsc(s));
              printf("\n");
              printf("出队成功!\n");
              break;
 
 case 0:
         printf("退出运行!");
         break;

 }
}
 return 0;
}
搜索更多相关主题的帖子: 斧正 运行 
2010-11-28 22:40
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
得分:0 
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

typedef int ElemType;

struct node
{
    ElemType data;
    struct node *link;
};

struct QueueLk
{
    struct node *front;
    struct node *rear;
};

struct node *ldcr(struct QueueLk *QL, ElemType x)
{
    struct node *p;
    p = (struct node*) malloc (sizeof(struct node));
    p->data = x;
    p->link = 0;
    if( QL->front==NULL )
        QL->front = QL->rear = p;
    else
    {
        QL->rear->link = p;
        QL->rear = p;
    }

    return QL->rear;
}

ElemType ldsc(struct QueueLk *QL)
{
    struct node *s;
    ElemType x;
    if( !QL->front )
        exit(-1);
    s = QL->front;
    QL->front = QL->front->link;
    x = s->data;
    free(s);
   
    return x;
}

int main()
{
     int choice = 1;
     ElemType e;
     struct QueueLk *s = (struct QueueLk*) malloc (sizeof(struct QueueLk));
     s->front = s->rear = NULL;
     while(choice)
     {
         printf("                 *-----------------菜单如下-----------------*\n");
         printf("                 *            1.......入队列                *\n");
         printf("                 *            2.......出队列                *\n");
         printf("                 *            0........退出                 *\n");
         printf("                 *------------------------------------------*\n\n");
         printf("请输入选择的操作:");
         scanf("%d",&choice);

        switch(choice)                 
        {
        case 1:
            printf("请输入要入队的元素:");
            scanf("%d",&e);
            ldcr(s,e);
            printf("入队成功!\n");
            break;
        case 2:
            printf("%d ",ldsc(s));
            printf("\n");
            printf("出队成功!\n");
            break;
        case 0:
             printf("退出运行!");
             break;
        }
     }

    return 0;
}
2010-11-29 15:36
a422100231
Rank: 2
等 级:论坛游民
帖 子:9
专家分:20
注 册:2010-11-26
得分:20 
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;

typedef  struct node                 //队列结点类型定义
{   ElemType data;                 //队列的数据元素类型
     struct node  *link;             //指向后继结点的指针
}NODE;

struct QueueLk{                         //定义链队
    NODE *front,*rear;        //定义链队队头和队尾指针
};
void initqueue(struct QueueLk *q)
{
    q->front=q->rear=(NODE *)malloc(sizeof(NODE));
    q->front->link=NULL;
}
//2、入队
int  ldcr(struct QueueLk *QL,ElemType x)  //将元素x插入到链队列rear中,作为rear的新队尾
{   
    NODE *p;
    p=(NODE *)malloc(sizeof(NODE));
    p->data=x;
    p->link=NULL;  //置新结点的指针为                                
        QL->rear->link=p;                      //将链队列中最后一个结点的指针指向新结点
        QL->rear=p;                              //将队尾指向新结点
    return 1;
}
//3、出队
ElemType ldsc(struct QueueLk *QL,int *e)  //若链队列不为空,则删除队头元素,返回其元素值
{  
   NODE *s;
   if(QL->rear==QL->front)                 //队空,退出程序
       exit(1);  
   
    s=QL->front->link;                //取队头保存在s中
   QL->front=QL->front->link;                       //删除队头结点
   if(QL->front->link==NULL)     //如果删除后队列为空,则处理队尾指针
       QL->rear=QL->front;
   *e=s->data;  
   printf("%d\n",*e);                //将刚才出队的结点值给x
    free(s) ;                               //释放出对结点的空间
   return 1;
}

 main()
{
     int choice;
     ElemType e,n;
     struct QueueLk s;
   initqueue(&s);
     printf("                 *-----------------菜单如下-----------------*\n");
     printf("                 *                                          *\n");
     printf("                 *            1.......入队列                *\n");
     printf("                 *                                          *\n");
     printf("                 *            2.......出队列                *\n");
     printf("                 *                                          *\n");
     printf("                 *            0........退出                 *\n");
     printf("                 *                                          *\n");
     printf("                 *------------------------------------------*\n\n");
       while(choice)
     {
     printf("请输入选择的操作:");
     scanf("%d",&choice);
   
     switch(choice)                 
             {
case 1:
               
                    printf("请输入要入队的元素:");
                                   scanf("%d",&e);                  
                      ldcr(&s,e);
                printf("入队成功!\n");
                break;
case 2:
              ldsc(&s,&n);
              printf("出队的元素为:\n");
              //printf("%d ",&n);
              printf("\n");
              printf("出队成功!\n");
              break;

case 0:
         printf("退出运行!");
         break;

             }
     }return 0;
}
2010-12-01 13:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-327193-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.022251 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved