//顺序队 #include#include #define MaxSize 1000 typedef struct elementtype { int tyint; char tychar; }ElementType; typedef struct queue { ElementType* base; int front; int rear; }Queue; Queue* CrateQueue() { Queue* newQueue=(Queue*)malloc(sizeof(Queue)); newQueue->base=(ElementType*)malloc(sizeof(ElementType)*MaxSize); newQueue->front=0; newQueue->rear=0; return newQueue; } int LengthQueue(Queue* myQueue) { return ((myQueue->rear-myQueue->front+MaxSize)%MaxSize); } void EnQueue(Queue* myQueue,ElementType data) { if((myQueue->rear+1)%MaxSize==myQueue->front) { exit(1); } myQueue->base[myQueue->rear]=data; myQueue->rear=(myQueue->rear+1)%MaxSize; } ElementType DeQueu(Queue* myQueue) { if(myQueue->front==myQueue->rear) { exit(2); } ElementType temp=myQueue->base[myQueue->front]; myQueue->front=(myQueue->front+1)%MaxSize; return temp; } ElementType GetHead(Queue* myQueue) { if(myQueue->front==myQueue->rear) { exit(3); } return myQueue->base[myQueue->front]; } int main() { Queue* test=CrateQueue(); ElementType data1={1,'a'},data2={2,'b',},temp; EnQueue(test,data1); EnQueue(test,data2); temp=DeQueu(test); printf("%d %cn",temp.tyint,temp.tychar); temp=GetHead(test); printf("%d %cn",temp.tyint,temp.tychar); } //链队 #include #include typedef struct elementtype { int tyint; char tychar; }ElementType; typedef struct node { ElementType data; struct node* next; }Node; typedef struct queue { Node* front; Node* rear; }Queue; Node* CreateNode() { Node* newNode=(Node*)malloc(sizeof(Node)); newNode->next=NULL; return newNode; } Queue* CreateQueue() { Node* newNode=CreateNode(); Queue* newQueue=(Queue*)malloc(sizeof(Queue)); newQueue->front=newNode; newQueue->rear=newQueue->front; return newQueue; } void EnQueue(Queue* myQueue,ElementType data) { Node* temp=CreateNode(); temp->data=data; myQueue->rear->next=temp; myQueue->rear=temp; } ElementType DeQueue(Queue* myQueue) { if(myQueue->front==myQueue->rear) { exit(1); } Node* temp=CreateNode(); ElementType x=myQueue->front->next->data; temp=myQueue->front; myQueue->front=myQueue->front->next; free(temp); return x; } ElementType GetHead(Queue* myQueue) { if(myQueue->front!=myQueue->rear) { return myQueue->front->next->data; } } int main() { Queue* test=CreateQueue(); ElementType data1={1,'a'},data2={2,'b'},temp; EnQueue(test,data1); EnQueue(test,data2); temp=DeQueue(test); printf("%d %cn",temp.tyint,temp.tychar); temp=GetHead(test); printf("%d %cn",temp.tyint,temp.tychar); temp=DeQueue(test); printf("%d %cn",temp.tyint,temp.tychar); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)