대기 열의 기본 조작 - 데이터 구조

31775 단어 데이터 구조대열
데이터 구조 - 대열 의 기본 조작
#include 
#include 
#define queuesize 10  //         
#define null 0
#define ok 1
#define overflow -2
typedef int elemtype;
typedef int status;

typedef struct
{
     
  elemtype * data;
  int front;
	  int rear;   //    ,    ;
}sqqueue;


status  initsqqueue( sqqueue &q)
{
     
 
  q.data=new elemtype[queuesize];
  if(!q.data) exit(overflow);
  else{
     
  q.front=q.rear=0;   //   
 
  }
  return ok;
}

int queuelength(sqqueue q) //       
{
     
  return (q.rear-q.front+queuesize)%queuesize;
}



status createquque(sqqueue &q,int a[],int n)  //        ;
{
     
  int i;
  q.data=new elemtype[queuesize];
  q.front=0;
  q.rear=0;
  for(i=0;i<n;i++)
  {
     
	  q.data[q.rear]=a[i-1];
	  q.rear=(q.rear+1)%queuesize;  //                ;
  }
  return ok;
}


int enqueue(sqqueue &q,elemtype x)  //    
{
     
  if((q.rear+1)%queuesize==q.front)  //   
	 {
      printf("  ,    
"
); return null;} else{ q.data[q.rear]=x; q.rear=(q.rear+1)%queuesize; printf("
"
); return ok; } } int dequeue(sqqueue &q,elemtype &x) // { if(q.rear==q.front){ printf(" , :
"
); return null; } else{ x=q.data[q.front]; q.front=(q.front+1)%queuesize; printf(" , %d
"
,x); return ok; } } int gethead(sqqueue q) // { if(q.front!=q.rear) return q.data[q.front]; } int queuefull( sqqueue q) { if(q.front=q.rear) return ok; else return null; } void display(sqqueue q) // { sqqueue r; r.front=q.front; if(q.front==q.rear) { printf("SqQueue empty!
"
); return;} else { printf("in order SqQueue:"); while(q.rear!=r.front) { printf("%d,q.data[r.front]); r.rear=r.front; r.front=(r.front+1)%queuesize; } printf("
"
); printf("the position of tail :rear=%d
"
,q.data[r.rear]); printf("the position of head :front=%d
"
,q.data[q.front]); } } int main() { int select; sqqueue head; do { printf("
frist be use must be initiated !
"
); printf("please choose (0--7):
"
); printf("====================menu==========
"
); printf(" 1 initate SqQueue
"
); printf(" 2 create SqQueue queue is null
"
); printf(" 3 qppend SqQueue element
"
); printf(" 4 delete SQueue element
"
); printf(" 5 is empty SqQueue !
"
); printf(" 6 gethead element
"
); printf(" 7 display SqQueue
"
); printf(" 8 is full SqQueue
"
); printf(" 0 exit
"
); printf("
======================================
"
); scanf("%d",&select); switch(select) { case 1: { system("cls"); initsqqueue(head); printf("Sqqueue have been initiated!
"
); display(head); break; } case 2: { system("cls"); int n=10,a[10]; // printf(" please input n ,this is length:
");
//scanf("%d",&n); for(int i=1;i<=n;i++) a[i-1]=i; createquque(head,a,n); printf("sqqueue have been created!
"
); display(head); break; } case 3: { system("cls"); int x; printf("input sqqueue value:
"
); scanf("%d",&x); enqueue(head,x); display(head); break; } case 4: { system("cls"); int e,z; z=dequeue(head,e); printf("sqqueue head element %d be deleted!
"
,z); display(head); break; } case 5: { system("cls"); //if(ququeempty(head)) printf("sqqueue is empty
");
printf("sququeu is not empty
"
); display(head); break; } case 6: { system("cls"); int y; y=gethead(head); printf(" sqqueue head element :%d
"
,y); display(head); break; } case 7: { system("cls"); display(head); break; } case 8: { system("cls"); if(queuefull(head)) { printf("sqqueue is full
"
); } else printf(" sqqueue is not full
"
); display(head); break; } case 0: exit(0); } }while(select<=8); }

좋은 웹페이지 즐겨찾기