链式队列8种操作的实现

来源:互联网 发布:ubuntu查看显卡信息 编辑:程序博客网 时间:2024/05/16 05:21

 

操作

时间复杂度(T(n))

空间复杂度(S(n))

求长度

O(n)

O(1)

判断是否为空

O(1)

O(1)

得到队首元素

O(1)

O(1)

插入元素

O(1)

O(1)

删除元素

O(1)

O(1)

清空队列

O(n)

O(n)

/*  数据结构分析与学习专栏*  Copyright (c) 2015, 山东大学计算机科学与技术专业学生*  All rights reserved.*   作    者:   高祥*   完成日期:  2015 年 4 月 6 日*   版 本 号:012 *任务描述:针对链式队列,实现8个基本操作*   1:建立队列 ;*   2:输出队列 ;*   3:求队列的长度 ;*   4:判断队列是否为空 ;*   5:得到队列的队首元素;*   6:向队列中插入元素(插在队尾) ;*   7:删除队列的元素(删除队首元素);*   8: 清空队列;  *主要函数:*  1.Status InitQueue(LinkQueue &Q);//初始化队列*  2.void CreatQueue(LinkQueue &Q);//创建队列*  3.void Output(LinkQueue Q);//输出队列*  4.int QueueLength(LinkQueue Q);//求队列的长度*  5.Status IsEmpty(LinkQueue Q);//判断队列是否为空*  6.void GetHead(LinkQueue Q);//得到队首元素*  7.void EnQueue(LinkQueue &Q,ElemType elem);//插入元素*  8.void DeQueue(LinkQueue &Q);//删除元素*  9.void ClearQueue(LinkQueue &Q);//清空队列 */#include<iostream>#include<cstdlib>using namespace std; #define OK 1#define FALSE 0 typedef int ElemType;typedef int Status; typedef struct node{   ElemType data;    structnode* next;} Node,*QueuePtr; typedef struct{    QueuePtr first;//队首指针    QueuePtr last;//队尾指针} LinkQueue; Status InitQueue(LinkQueue &Q);//初始化队列void CreatQueue(LinkQueue &Q);//创建队列void Output(LinkQueue Q);//输出队列int QueueLength(LinkQueue Q);//求队列的长度Status IsEmpty(LinkQueue Q);//判断队列是否为空void GetHead(LinkQueue Q);//得到队首元素void EnQueue(LinkQueue &Q,ElemTypeelem);//插入元素void DeQueue(LinkQueue &Q);//删除元素void ClearQueue(LinkQueue &Q);//清空队列void Interaction(); int main(){   LinkQueue Q;    Interaction();    int operate;   while(cin>>operate)    {       switch(operate)       {       case 0:           return 0;        case 1:           CreatQueue(Q);           break;        case 2:           Output(Q);           break;        case 3:           cout<<"队列的长度为:"<<QueueLength(Q)<<endl;           break;        case 4:           if(IsEmpty(Q))           {                cout<<"队列为空。\n";           }           else           {                cout<<"队列不为空。\n";           }           break;        case 5:           GetHead(Q);           break;        case 6:           cout<<"请输入要插入的元素:";           ElemType elem;           cin>>elem;           EnQueue(Q,elem);            cout<<"新队列为:";           Output(Q);           break;        case 7:           DeQueue(Q);           break;        case 8:           ClearQueue(Q);           break;        default:           cout<<"请输入正确的操作数字!\n";           break;       }    }     return 0;} Status InitQueue(LinkQueue &Q)//初始化队列{   //为了操作方便,给队列添加一个头结点,队列为空即头指针、尾指针均指向头结点    Q.first=Q.last=(QueuePtr)malloc(sizeof(Node));   if(!Q.first)    {       cout<<"内存分配失败。\n";       return FALSE;    }     Q.first->next=NULL;//Q.first->next是存储队首元素的结点   return OK;} void CreatQueue(LinkQueue &Q)//创建队列{   if(InitQueue(Q))    {       cout<<"请输入要创建的队列的大小:";       int queuesize;       cin>>queuesize;       cout<<"请输入"<<queuesize<<"个元素的值:";       while(queuesize--)        {           ElemType elem;           cin>>elem;           EnQueue(Q,elem);//借用插入操作完成元素填充       }        cout<<"创建的队列为:";       Output(Q);    }} void Output(LinkQueue Q)//输出队列{   if(!IsEmpty(Q))    {       QueuePtr p=Q.first->next;       while(p)       {           cout<<p->data<<" ";           p=p->next;       }       cout<<endl;       return;    }    cout<<"队列为空,无法输出。\n";} int QueueLength(LinkQueue Q)//求队列的长度{   if(!IsEmpty(Q))    {       int cnt=0;       QueuePtr p=Q.first->next;       while(p)       {           cnt++;           p=p->next;       }       return cnt;    }   return 0;} Status IsEmpty(LinkQueue Q)//判断队列是否为空{   if(Q.first&&Q.last&&Q.first->next)//注意不能加Q.last->next,因为Q.last->next始终为NULL    {       return FALSE;    }   return OK;} void GetHead(LinkQueue Q)//得到队首元素{   if(!IsEmpty(Q))    {       cout<<"队列首元素是:"<<Q.first->next->data<<endl;       return;    }   cout<<"队列为空。\n";} void EnQueue(LinkQueue &Q,ElemTypeelem)//插入元素{   QueuePtr newnode=(QueuePtr)malloc(sizeof(Node));   newnode->data=elem;    Q.last->next=newnode;//尾指针指向插入的节点   newnode->next=NULL;//插入的节点指向空指针    Q.last=newnode;//尾指针更新为插入的节点} void DeQueue(LinkQueue &Q)//删除元素{   if(!IsEmpty(Q))    {       QueuePtr p=Q.first->next;//队首节点       Q.first->next=p->next;//更新链接        if(p==Q.last)       //如果此时队列中只有一个元素,删除后,尾指针指向的结点消失,令尾指针等于头指针,即队列变为空队列       {           Q.last=Q.first;       }        free(p);       p=NULL;       cout<<"删除元素后的队列为:";       Output(Q);       return;    }   cout<<"队列为空,无法删除元素。\n";} void ClearQueue(LinkQueue &Q)//清空队列{   QueuePtr p=NULL;   if(Q.first)    {       p=Q.first->next;    }    while(p)    {       QueuePtr q=p->next;       free(p);//释放队列中除头结点外的结点的内存       p=q;    }    Q.first->next=NULL;    Q.last=Q.first;   cout<<"清空队列成功。\n";} void Interaction(){   cout<<"请输入对应操作的序号:\n";   cout<<"0:退出程序;\n";   cout<<"1:建立队列;\n";   cout<<"2:输出队列;\n";    cout<<"3:求队列的长度 ;\n";   cout<<"4:判断队列是否为空;\n";   cout<<"5:得到队列的队首元素;\n";   cout<<"6:向队列中插入元素(插在队尾);\n";   cout<<"7:删除队列的元素(删除队首元素);\n";   cout<<"8:清空队列;\n";}

0 0
原创粉丝点击