資料結構基礎7_鏈隊的實現
阿新 • • 發佈:2019-01-29
typedef int ElemType;
typedef int Status;
#include<stdlib.h>
#include<stdio.h>
#define true 1
#define false 0
typedef struct Qnode{
ElemType data;
struct Qnode *next;
}Qnode,*queueptr;
typedef struct{
queueptr front;
queueptr rear;
}linkqueue;
Status Initqueue(linkqueue &Q)
{
Q.front=Q.rear=(queueptr)malloc(sizeof(Qnode));
if(!Q.front)
{
return false;
}
Q.front->next=NULL;
return true;
}
Status Destroyqueue(linkqueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return true;
}
Status Clearqueue(linkqueue &Q)
{
Q.front=Q.rear;
Q.front->next=NULL;
return true;
}
int queuelength(linkqueue Q)
{
int j=0;
Qnode *p=Q.front;
while(p!=NULL)
{
j++;
p=p->next;
}
return j;
}
Status Gethead(linkqueue Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return false;
}
e=Q.front->data;
return true;
}
Status Enqueue(linkqueue &Q,ElemType &e)
{
Qnode *p;
p=(queueptr)malloc(sizeof(Qnode));
if(!p)
{
return false;
}
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return true;
}
Status Dequeue(linkqueue &Q,ElemType &e)
{
if(Q.rear==Q.front)
{
return false;
}
Qnode *p;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return true;
}
Status queuetraverse(linkqueue Q)
{
Qnode *p=Q.front->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
return true;
}
typedef int Status;
#include<stdlib.h>
#include<stdio.h>
#define true 1
#define false 0
typedef struct Qnode{
ElemType data;
struct Qnode *next;
}Qnode,*queueptr;
typedef struct{
queueptr front;
queueptr rear;
}linkqueue;
Status Initqueue(linkqueue &Q)
{
Q.front=Q.rear=(queueptr)malloc(sizeof(Qnode));
if(!Q.front)
{
return false;
}
Q.front->next=NULL;
return true;
}
Status Destroyqueue(linkqueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return true;
}
Status Clearqueue(linkqueue &Q)
{
Q.front=Q.rear;
Q.front->next=NULL;
return true;
}
int queuelength(linkqueue Q)
{
int j=0;
Qnode *p=Q.front;
while(p!=NULL)
{
j++;
p=p->next;
}
return j;
}
Status Gethead(linkqueue Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return false;
}
e=Q.front->data;
return true;
}
Status Enqueue(linkqueue &Q,ElemType &e)
{
Qnode *p;
p=(queueptr)malloc(sizeof(Qnode));
if(!p)
{
return false;
}
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return true;
}
Status Dequeue(linkqueue &Q,ElemType &e)
{
if(Q.rear==Q.front)
{
return false;
}
Qnode *p;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return true;
}
Status queuetraverse(linkqueue Q)
{
Qnode *p=Q.front->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
return true;
}
#include<stdio.h> #include<linkqueue1.h> #include<stdlib.h> int main() { linkqueue s; Initqueue(s); int n=1; int e=2; int r=3; int q=4; Enqueue(s,n); Enqueue(s,e); Enqueue(s,r); Enqueue(s,q); int m; Dequeue(s,m); queuetraverse(s); system("pause"); }