zoukankan      html  css  js  c++  java
  • 链式队列的基本操作

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 typedef int ElemType;
     4 //结点结构体 
     5 typedef struct node{
     6     ElemType data;
     7     struct node *next; 
     8 }LinkQueueNode;
     9 //头结点的前一个节点
    10 typedef struct Node{
    11     LinkQueueNode *front;
    12     LinkQueueNode *rear; 
    13 }LinkQueue;
    14 //初始化结点
    15 int CreateQueue(LinkQueue *L){
    16     LinkQueueNode *phead=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
    17     L->front=L->rear=phead;
    18     L->front->next=NULL;
    19 } 
    20 //入队操作
    21 int InputQueue(LinkQueue *L){
    22     int n,m;
    23     LinkQueueNode *pre=L->front;
    24     
    25     printf("请输入你想入队的长度:");
    26     scanf("%d",&n);
    27     for(int i=0;i<n;i++){
    28         LinkQueueNode *NewNode = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
    29         printf("请输入第%d个数字:",i+1);
    30         scanf("%d",&m);
    31         NewNode->data=m;
    32         NewNode->next=NULL;
    33         pre->next=NewNode;
    34         L->rear->next=NewNode;//因为这个是动态的所以用rear不用front 
    35         L->rear=NewNode;
    36         pre=pre->next;
    37     }
    38 } 
    39 //打印队列
    40 int OutElem(LinkQueue *L){
    41     LinkQueueNode *pre=L->front;
    42     LinkQueueNode *p=pre->next;
    43     if(pre->next==NULL){
    44         return printf("队列中没有元素了!");
    45     }
    46     while(p->next!=NULL){
    47         printf("%d ",p->data);
    48         p=p->next;
    49     }printf("%d ",p->data);
    50     printf("
    ");
    51     return printf("打印成功!
    ");
    52 }
    53 //出队操作
    54 int DeleteLinkQueue(LinkQueue *L){
    55     LinkQueueNode *pre=L->front;
    56     LinkQueueNode *p=pre->next;
    57     pre->next=p->next;//p->next可能是空这种做法不严谨
    58      if (L->rear = p)   
    59     {
    60         L->rear = L->front;
    61         printf("置空成功!"); 
    62     }
    63     free(p);
    64     return printf("出队成功!
    ");
    65 } 
    66 int main(){
    67     LinkQueue L;
    68     CreateQueue(&L);
    69     InputQueue(&L);
    70     OutElem(&L);
    71     DeleteLinkQueue(&L);
    72     OutElem(&L);
    73 }
  • 相关阅读:
    基本的Dos命令
    OneCloud记录
    Wireguard笔记
    windows网络流量监控
    CoreDNS笔记
    Goland 使用[临时]
    js for循环的同步代码
    看我如何用微信上线CobaltStrike
    图数据库 Nebula Graph 在 Boss 直聘的应用
    熵池 在计算机科学与金融学中的应用
  • 原文地址:https://www.cnblogs.com/longlonglonglong/p/11012698.html
Copyright © 2011-2022 走看看