⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sy3_4.c

📁 数据结构实验与学习指导
💻 C
字号:
/* sy3_4.c */
#include"sj.h"                    /*调用自定义库文件*/
/*队列的基本运算*/
typedef char ElemType;
typedef  struct{
    ElemType elem[MAXSIZE] ;        /*数据的存储区*/
    int  front,rear;                  /*队头、队尾指针*/
  }CirSeQueue;                        /*循环队*/
  CirSeQueue  sq;
 void    Init_SeQueue(CirSeQueue *sq)
  {sq->front=sq->rear=0 ;
}
Status  Empty_SeQueue(CirSeQueue  *sq)
{ if  (sq->front==sq->rear)  return TRUE;
        else return FALSE;
   }
Status   In_SeQueue (CirSeQueue  *sq , ElemType  x)
  { if((sq->rear+1) % MAXSIZE==sq->front)
     { printf("队满!\n");
       return OVERFLOW;             /* 队满不能入队 */
     }
   else
    { sq->elem[sq->rear]=x ;
      sq->rear=(sq->rear+1) % MAXSIZE ;
      return OK;     }             /* 入队完成 */
  }
Status  Out_SeQueue (CirSeQueue *sq , ElemType *y)
  {  if (Empty_SeQueue(sq) )
     {printf("队空!\n");
      return OVERFLOW;                       /* 队空不能出队 */
     }
    else
     {  *y=sq->elem[sq->front];             /* 读出队头元素 */
        sq->front=(sq->front+1) % MAXSIZE;
return OK;  }                        /* 出队完成 */
     }
void Print_SeQueue(CirSeQueue *sq)        /*输出队列里的元素*/
 { int i;
   printf("队列成员: \n");
   for(i=sq->front;i<=sq->rear-1;i++)
   printf("%c ",sq->elem[i]);
   printf("\n");
  }/*Print_SeQueue*/
void InQue_Op()
 { /*入队操作*/
   char ch_x;      int i;
   getchar();
   printf("输入一个入队的字符(如果字符是 '0',输入结束):\n");
   scanf("%c",&ch_x);
   while(ch_x!='0')
    if ( In_SeQueue(&sq ,ch_x)== OVERFLOW) 
       { printf("入队失败!\n"); return;}
     else
       {
         printf("入队成功!\n 输入一个入队的字符\n");
         getchar();
         scanf("%c",&ch_x);
       }
      /*while*/
   }/*InQue_Op*/
void OutQue_Op()
    { /*出队操作*/
     char e,ch_y;
     getchar();
printf("如果想进行出队操作,请按空格键然后回车!\n");
     scanf("%c",&ch_y);
     while(ch_y==' ')
       {
        if(Out_SeQueue(&sq,&e)==OK)
              { printf("出队操作成功!\n");
                printf("出队的字符是%c\n",e);
                getchar();
                scanf("%c",&ch_y);
             } /*if*/
       else return;
      } /*while*/
   } /*OutQue_Op*/
void main()
    { int i,flag=1;
      Init_SeQueue(&sq);
      while(flag)
         {
          printf("请选择操作: 1---入队; 2----出队; 0----退出程序: \n");
     scanf("%d",&flag);
     switch(flag)  {
       case  0: Print_SeQueue(&sq);
       printf("\n程序结束\n"); return;
       case  1: InQue_Op(); break;
       case  2: OutQue_Op(); break;
                 default:printf("选择的操作无效\n"); break;
       }  /*switch*/
        Print_SeQueue(&sq);
    }                                       /*外层循环结束*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -