双向队列

一个双向队列是限定在两端end1,end2都可以进行插入删除操作的线性表。对空调间是end1=end2.若用顺序方式来组织双端队列,试根据下列要求,定义双端队列的结构,并给出指定端(i=1,2)进行插入和删除操作

 

#include<iostream.h>
#include<malloc.h>
#define MaxLen 6
typedef char elemtype;
typedef struct node
{
 elemtype data[MaxLen];
 int end1,end2;
}squeue;


void init(squeue *sq)
{
 sq->end1=0;
 sq->end2=0;
}

squeue *add(squeue *sq,char x,int tag)
{
 switch(tag)
 {
 case 1:
  if((sq->end1-1)%MaxLen!=sq->end2)
  {
   sq->end1=(sq->end1-1+MaxLen)%MaxLen;
   sq->data[sq->end1]=x;
   return sq;
  }
  else
   return sq;
 case 2:
  if((sq->end2+1)%MaxLen!=sq->end1)
  {
   sq->data[sq->end2]=x;
   sq->end2=sq->end2+1;
   return sq;
  }
  else
   return sq;

 }
}

 

void print(squeue *sq)
{
 if(sq->end2==sq->end1)
 {
  cout<<"队列为空"<<endl;
  return;
 }
 else if(sq->end2>sq->end1)
 {
  for(int i=sq->end1;i<sq->end2;i++)
   cout<<(int)sq->data[i]<<" ";
  cout<<endl;
 }
 else if(sq->end2<sq->end1)
 {
  for(int i=sq->end1;(i+MaxLen)%MaxLen!=sq->end2;i++)
   cout<<(int)sq->data[i%MaxLen]<<" ";
  cout<<endl;
 }
}

 


void main()
{
 elemtype p;
 squeue *sq=(squeue *)malloc(100*sizeof(squeue));
 init(sq);
 sq=add(sq,45,2);
 print(sq);
 sq=add(sq,55,2);
 print(sq);
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码,经典。 CARD *myinsert(LCARD *head, LCARD *insert) { LCARD *temp = NULL; if (head==NULL)//链表为空 { head = insert; insert->next = insert; insert->prior = insert; } else//链表非空 { temp = head; if (head->cardnum>insert->cardnum)//插入到头前边,并且把自己当作头 { head->prior->next = insert; insert->prior = head->prior; insert->next = head; head->prior = insert; head = insert; } if (insert->cardnum0<50)//小于50正向插入 { while ((temp->cardnum<insert->cardnum)&&(temp->next!=head))//循环 { temp = temp->next; } if (temp->cardnum>insert->cardnum)//第一个条件终止的 { temp->prior->next = insert; insert->prior = temp->prior; insert->next = temp; temp->prior = insert; } else//第二个条件终止的 { head->prior->next = insert; insert->prior = head->prior; insert->next = head; head->prior = insert; } } else//大于50反向插入 { while ((temp->cardnum>insert->cardnum)&&(temp->prior!=head))//循环,第二个条件禁止跑飞 { temp = temp->prior; } if (temp->cardnum<insert->cardnum)//只有第一个条件可以终止的 { temp->next->prior = insert; insert->next = temp->next; insert->prior = temp; temp->next = insert; } } } //printf("%d\t%d\n", insert->id, insert->cardnum); return head; } void swap_id(SWID *sw) { LCARD *temp = sw->head; if (sw->head->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); sw->head->id = sw->inID; return ; } if ((sw->swapcardnum0)<50) { while ((temp->cardnum!=sw->swapcardnum)&&(temp->next!=sw->head)) { temp = temp->next; } if (temp->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); temp->id = sw->inID; } } else { while ((temp->cardnum!=sw->swapcardnum)&&(temp->prior!=sw->head)) { temp = temp->prior; } if (temp->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); temp->id = sw->inID; } } } LCARD *mydel(LCARD *head, LCARD *del) { LCARD *temp = NULL; if (head==NULL)//没有链表 { printf("there is no card\n"); } else//有链表 { if(head->next==head)//链表里就有一个节点并且为头结点 { if (head->cardnum==del->cardnum) { free(head); head = NULL; } else { printf("in mydel error\n"); } } else//链表里有超过一个的节点 { temp = head; if (del->cardnum0<50)//成立则正向删除 { while ((temp->cardnum!=del->cardnum)&&(temp->next!=head)) { temp = temp->next; } if (temp->cardnum==del->cardnum) { temp->prior->next = temp->next; temp->next->prior = temp->prior; free(temp); } } else//反向删除 { while ((temp->cardnum!=del->cardnum)&&(temp->prior!=head)) { temp = temp->prior; } if (temp->cardnum==del->cardnum) { temp->prior->next = temp->next; temp->next->prior = temp->prior; free(temp); } } } } return head; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值