• 数据结构笔记6链队列


    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<malloc.h>
    //定义一个节点
    typedef struct{
     int data;
      struct *next;
    }QNode;
    //定义一个链,其中front表示链头,rear表示链尾
    typedef struct{
      QNode *front,*rear;
    }LQ;
    LQ q;
    //初始化
    int Init(LQ q){
      QNode *hnode = NULL;
      hnode = (QNode *)malloc(sizeof(QNode));
     if (NULL == hnode)
        return -1;//内存分配失败
     hnode->next=NULL;
      return 0;
    }
    //判空
    int Empty(LQ q){
      if(q.front == q.rear)
         return 1;
      return 0;
    }
    //求长度
    int Length(LQ q){
      int count = 0;
      QNode *p=q.front;
      while(p!=q.rear){
        p=p->next;
        count++;
      }
      return count;
    }
    //入队
    int In(LQ q,int e){
      QNode *p;
      p =(QNode *)malloc(sizeof(QNode));
      if(NULL==p)
       return -1;
      p->data=e;
      p->next=NULL;
      q.rear->next=p;
      q.rear=p;
      return 0;
    }
    //出队
    int Out(LQ q){
      QNode *p;
      if(Empty(q))
        return -1;
      p = (q.front)->next;
      q.front->next=p->next;
      free(p);
      return 0;
    }
    //显示 
    int Show(LQ q){
      for(;q.front!=q.rear;q.front=q.front->next)
        printf("%d",q.front->data);
      return 0;
    }
    int main(){
      LQ q;
      int i,e,j=0;
      if(!Init(q)){
        printf("创建失败!按任意键结束:");
        getch();
        exit(0);
      }
      printf(" 1-入队并且显示 ");
      printf(" 2-出队并且显示 ");
      printf(" 3-显示 ");
      printf(" 4-退出 ");
      loop:
       printf("请输入你要进行的操作:");
       scanf("%d",&i);
       switch(i){
        case 1:
          printf("请输入你要入队的数目:");
          scanf("%d",&i);
          if(Empty(q)){
            printf("队列为空!");
            return 0;
          }
          else{
            printf("请输入你的数字:");
            for(j=0;j<i;j++){
             scanf("%d",&e);
             In(q,e);
            }
            Show(q);
          }
           break;
        case 2:
          if(Empty(q)){
            printf("队列为空!");
            return 0;
          }
          else{
            printf("请输入你要出队的次数:");
            scanf("%d",&i);
            for(j=0;j<i;j++)
            Out(q);
            Show(q);
          }
           break;
        case 3:
          if(Empty(q)){
            printf("队列为空!");
            return 0;
          }
          else
           Show(q);
          break;
        case 4:
          exit(0);
          break;
         case 5:
          if(Empty(q)){
            printf("队列为空!");
            return 0;
          }
          else
          Length(q);
          break;
        default:
          printf("你的输入有误请再次输入在1-4之间选择:");
          goto loop;
       }
      printf("是否继续进行链队列操作?1-yes");
       scanf("%d",&i);
       if(i==1)
         goto loop;
       else{
         printf("按任意键退出!");
         getch();
         exit(0);
       }
      return 0;
    }

  • 相关阅读:
    地图校正方法心得
    投影的心得点滴
    android 打包 apk keystore
    scp命令详解
    ubuntu11.10真机调试nopermissions
    android adb server is out of date
    ubuntu删除默认jdk
    android 运行 错误 总结
    android file .apk is not a valid zip file adb install
    ubuntu系统目录结构
  • 原文地址:https://www.cnblogs.com/100114jerro/p/4998743.html
Copyright © 2020-2023  润新知