循环队列 数据结构题 【求一完整程序 !】编写实现该循环队列的入队和出队 *** 作的算法。

循环队列 数据结构题 【求一完整程序 !】编写实现该循环队列的入队和出队 *** 作的算法。,第1张

(VC6下编译通过)

#include <stdioh>

main()

{

int a[1000],top,tail;

int i,n=1;

do

{

switch (n)

{

case 1:top=tail=0;break;

case 2:printf("输入要插入的元素:");scanf("%d",&a[++top]);break;

case 3:if (tail<top) tail++;break;

case 4:printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n队头为: %d\n",a[top]);break;

case 5:printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");if (tail==top) printf("空队列\n"); else printf("非空队列\n");

}

if (n!=5&&n!=4)

printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

printf("队列现在状态:");

for (i=tail+1;i<=top;i++)

printf(" %d",a[i]);

if (tail==top)

printf("空队列");

printf("\n");

printf("\n1队列初始化\n2入队 *** 作\n3出队 *** 作\n4输出队头元素\n5判断队列是否为空\n0退出\n请输入代码:");

scanf("%d",&n);

} while (n!=0);

}

循环单链中尾指针执行一个命令:rear=rear->next; 不就成头指针了~

插入:

InserterList_Dul(DuLNode l,Datatype p,Datatype e)/将E元素插入到循环单链表L中的P指针所指的元素前面/

{

s=(struct DuLNode )malloc(sizeof(sturct DuLNode));/申请一个节点,让指针S指向它/

s->data=e; /将S送入新节点/

s->next=p;/使新节点的后继指针指向P/

s->prior=p->prior;/使新节点的前驱指针指向P的前驱指针/

p->prior->next=s;/使P的前驱节点的后继指针指向新节点/

p->prior=s;/使P的前驱指针指向新节点/

}

删除:

DeleteList_Dul(DulNOde l,DuLnode p) /删除循环单链表L中P指针所指的元素/

{

p->prior->next=p->next;/使P的前驱节点的后继指针指向P的后继节点/

p->next->prior=p->prior;/使P的后继节点的前向指针指向P的前驱节点/

free(p);/释放P所指被删除的节点/

}

   /

      实现循环队列的基本 *** 作(初始化、判断队空、判断队满、入队、出队)

   /

   //在javascript中,可以使用数组来实现一个队列

   function stack(){

      thisdatastore = new Array();    //初始化

      thisisEmpty = isEmpty; //判断队空

      thisisFull = isFull;   //判断队满

      thisadd = add;         //入队

      thisremove = remove;   //出队

      thiscount = 0;

      function isEmpty(){

        if(thiscount == 0) return true;

        return false;

      }

      function isFull(){

        if(!isEmpty()) return true;

        return false;

      }

      function add(value){

        alert(thiscount);

        thisdatastore[thiscount ++] = value;

      }

      function remove(){

        if(thiscount <= 0) {

            thiscount = 0;

           alert('当前队列为空,无法删除!');

           return;

        }

        delete thisdatastore[-- thiscount ];

      }

   }

Leet Code 原题链接

Leet Code 原题动画演示视频

设计你的循环队列实现。 循环队列是一种线性数据结构,其 *** 作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下 *** 作:

Design your implementation of the circular queue The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle It is also called "Ring Buffer"

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue But using the circular queue, we can use the space to store new values

Your implementation should support the following operations:

我们仔细研究一下 Leet Code 原题动画演示视频 这一个视频,发现来判断队空和队满的条件。假定我们有两个指针,分别为头指针head和尾指针tail。

#include<stdioh>

#define MAXSIZE 100

typedef struct seqqueue

{

int data[MAXSIZE];

int front;

int rear;

}seqqueue;

void Initseqqueue(seqqueue &q) //循环队列初始化

{

qfront =qrear=0;

printf("初始化成功!\n");

}

int enqueue(seqqueue &q,int e) //数据元素e入队列

{

if((qrear+1)%MAXSIZE==qfront)

{

printf("循环队列满!\n");

return 0;

}

else

{

qdata[qrear]=e;

qrear=(qrear+1)%MAXSIZE;

printf("%d入队列成功!\n",e);

return 1;

}

}

int isemptyqueue(seqqueue &q) //判断循环队列是否为空

{

if(qrear ==qfront )

{

printf(" 空队列!\n");

return 1;

}

else

{

printf("非空队列!\n");

return 0;

}

}

int dequeue(seqqueue &q,int &e) //数据元素出队列,出队列元素暂存储于e中

{

if(!isemptyqueue(q))

{

e=qdata [qfront ];

qfront =(qfront +1)%MAXSIZE;

printf("出队列成功!\n");

return 1;

}

else

{

printf("出队列失败!\n");

return 0;

}

}

void main()

{

int x=0;

seqqueue qa;

Initseqqueue(qa);

isemptyqueue(qa);

dequeue(qa,x);

enqueue(qa,25);

isemptyqueue(qa);

dequeue(qa,x);

}

#include<iostream>

using namespace std;

const int Max=100;

template <class T> 

class MyQueue

{

    private:

            T aa;

            unsigned int front;

            unsigned int tail;

    public :

           void init();

           bool isEmpty();

           T DeQueue();

           void EnQueue(const T a);

           unsigned int size();

           void destroy();

                  

};

template <class T> 

void MyQueue<T>::init()

{

      aa = new T[Max+1];

      front=0;

      tail=0;

      return;    

};

template <class T> 

bool MyQueue<T>::isEmpty()

{

      if(front==tail-1) return true;

      else     return false;

};

template <class T> 

unsigned int MyQueue<T>::size()

{

      return tail-front;

};

template <class T> 

void MyQueue<T>::EnQueue(const T a)

{

      tail=tail+1;

      aa[tail]=a;    

};

template <class T> 

T MyQueue<T>::DeQueue()

{

       

      front=front+1;

      T t=aa[front]; 

      cout << t << endl;

      return t;     

};

template <class T> 

void MyQueue<T>::destroy()

{

       

      delete[] aa;

};

int main()

{

    

    MyQueue<char> st;

    stinit();

    if(stisEmpty()) cout << "MyQueue is empty" << endl;

    else cout <<"MyQueue is not empty" << endl;

    stEnQueue('a');

    stEnQueue('b');

    stEnQueue('c');

    stDeQueue();

    cout << stsize() << endl;

    stEnQueue('d');

    stEnQueue('e');

    stEnQueue('f'); 

    cout<< stsize()<<endl;

    while(!stisEmpty()) stDeQueue();

    stdestroy();

    system("pause");

    return 0;

}

using System;using SystemCollectionsGeneric;using SystemLinq;using SystemText;/ 队列是这样一种数据结构,数据项的插入在一端(队列尾),而数据项的取得或删除则在另一端(队列头)。 因为第一个插入的数据项也是第一个取得或删除的数据项,开发者普遍地将队列称为FIFO数据结构。开发者经常使用到两种队列:线性队列和循环队列。在两种队列中,数据项都是在队列尾插入, 并从队列头删除或获取,即先进先出 下面实现一个循环队列/namespace 队列{ class Student { string name; public string Name { get { return name; } set { name = value; } } public Student(string name) { thisname = name; } public Student Next; //指示下一个队列 } class Program { static void Main(string[] args)

以上就是关于循环队列 数据结构题 【求一完整程序 !】编写实现该循环队列的入队和出队 *** 作的算法。全部的内容,包括:循环队列 数据结构题 【求一完整程序 !】编写实现该循环队列的入队和出队 *** 作的算法。、数据结构如果一个循环单链表示队列(循环队列),编写程序实现循环队列的插入和删除、实现循环队列的基本 *** 作(初始化、判断队空、判断队满、入队、出队)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: https://outofmemory.cn/zz/10215508.html

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

发表评论

登录后才能评论

评论列表(0条)

保存