数据结构c语言版,出队入队及依次输出一个队列的 *** 作。

数据结构c语言版,出队入队及依次输出一个队列的 *** 作。,第1张

#include <stdio.h>

#include <stdlib.h>

#define ElemType int 

#define Status int

#define OK 1

#define ERROR 0

typedef struct QNode{

    ElemType data

    struct QNode *next

}QNode

typedef struct LinkQueue{

    QNode *front

    QNode *rear

}LinkQueue

Status InitQueue(LinkQueue *q) { //建立队列 

    q->front=q->rear=(QNode *)malloc(sizeof(QNode))

    if(!q->front)

          return ERROR

    q->front->next=NULL

    return OK

}

Status EnQueue(LinkQueue *q,ElemType e) { //入队 

     QNode *p=(QNode *)malloc(sizeof(QNode))

     if(!p)

           return ERROR

     p->data=e

     p->next=NULL

     q->rear->next=p //入队 *** 作,从队尾(rear)进入

     q->rear=p

     return OK

}

Status DeQueue(LinkQueue *q,ElemType *e) { //出队 

    QNode *p=(QNode *)malloc(sizeof(QNode))

    if(!p)

         return ERROR

 

    p=q->front->next //q指向的是front指针的下一个位置,亦即队首元素 

    *e=p->data

    q->front->next=p->next //出队 *** 作后,front++

    if(q->rear==p) //判断是否全部出队

          q->rear=q->front //如果全部出队,则将队列置为空

    return OK

}

Status PrintfQueue(LinkQueue *Q) {

    QNode *p

     

    for(p=Q->front->nextp!=NULLp=p->next)

          printf("%d\t",p->data)

}

int main(void)

{

    int i,n

    ElemType e,de

    LinkQueue *q=(LinkQueue *)malloc(sizeof(QNode))

    if(!q)

         return ERROR

    InitQueue(q)

    

    printf("以下开始构造初始队列:\n\n")

    

    printf("请输入元素个数:")

    scanf("%d",&n)

    printf("\n")

    

    for(i=0i<n++i) {

     printf("请输入第%d个元素:",i+1)

        scanf("%d",&e)

        EnQueue(q,e)

    }

    printf("\n")

    printf ("初始队列构造完毕!\n\n")

    

    printf ("初始队列:\n")

    PrintfQueue(q)

    printf("\n\n")

    printf ("======================================================\n\n")

    

    printf("以下开始执行入队 *** 作:\n\n")

    

    printf("请输入需入队的元素个数:")

    scanf("%d",&n)

    printf("\n")

    

    for(i=0i<n++i) {

     printf("请输入第%d个元素:",i+1)

        scanf("%d",&e)

        EnQueue(q,e)

    }

    printf("\n")

    printf ("入队%d个元素 *** 作完毕!\n\n",n)

    

    printf("此时队列:\n")

    PrintfQueue(q)

    printf("\n\n")

    printf ("======================================================\n\n")

    

    printf("以下开始执行出队 *** 作:\n\n")

    

    printf("请输入需出队的元素个数:")

    scanf("%d",&n)

    printf("\n")

    

    for(i=0i<n++i)

        DeQueue(q,&de)

    printf("\n")

    printf ("出队%d个元素 *** 作完毕!\n\n",n)

    

    printf("此时队列:\n")

    PrintfQueue(q)

    printf("\n\n")

    printf ("======================================================\n\n")

   

    free(q)

    return 0

}

执行结果

字符入队列

struct node

{

char c

struct node *next

}

struct node *Q//Q为队头,无数据

//用栈实现队列;

//输入ESC则退出,输入回车则换行

struct node *Input(void)

{

struct node *p,*t//临时结点指针

char char

p=struct node * malloc(sizeof(struct node))

Q=pQ->next=NULL

t=p

scanf("%c",&char)

while(char!=27)//输入字符,直到ESC结束

{

p=struct node * malloc(sizeof(struct node))

p->c=char

t->next=p//把输入的字符插入队列中

t=p

p->next=NULL

scanf("%c",&char)

}

}

void Print(struct node *Q)

{

struct node *p

p=Q->next

while(p->c!=27&&p)

{

if(p->c==13)printf("\n")//输出换行符

else printf("%c",p->c)

}

}

这样的话应该符合你的要求:

#include<stdio.h>

void add(int queue[],int x)

int Top(int queue[])

void del(int queue[])

int end=0

int main()

{

 int n

 scanf("%d",&n)//将要入队列n个元素

 int queue[1000]

 for(int i=1i<=ni++)//输入n个元素

 {

  add(queue,i)//将i加入队列

 }

 //验证加入队列的元素,将队列中的元素按照输入的顺序输出:

 for( i=1i<=ni++)

 {

  printf("%d ",Top(queue))//Top函数返回队头元素

  del(queue)//删除队头元素

 }

 //验证输出已经出队列后的队列(数组)元素:

 printf("\n")

 for(i=1i<=ni++)

  printf("%d ",queue[i])

 printf("\n")

 return 0

}

void add(int queue[],int x)

{

    queue[++end]=x

}

int Top(int queue[])

{

    return queue[1]//注意,这里的函数始终return queue[1]这里是和将普通数组中的元素输出最大的不同之处。!!!!!!

}

void del(int queue[])

{

    for(int i=2i<=endi++)

 {

  queue[i-1]=queue[i]

 }

 queue[end]=0//将删除后的地方置0

 end--

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12070497.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存