数据结构---链表队

来源:互联网 发布:sql 2008 sa 密码 编辑:程序博客网 时间:2024/06/06 09:37

队结构体定义:

typedef int datatype;typedef struct node{datatype data;struct node *next;}Queue;//链队结点类型 

主函数内队伍的定义:

LQueue *head;head=InitQueue();


队伍头尾封装:

typedef struct {Queue *rear;Queue *front;}LQueue;//头尾指针封装在一起的链队
空队的初始化及建立:

LQueue *InitQueue()//创建一个带头结点的空队 {LQueue *q;Queue *p; q=(LQueue *)malloc(sizeof(LQueue));//申请头尾指针结点p=(Queue *)malloc(sizeof(Queue));//申请链队头结点p->next=NULL,q->front=q->rear=p;return q;}
入队:

void in_LQueue(LQueue *q,datatype x)//入队 {Queue *p;p=(Queue *)malloc(sizeof(Queue));printf("%d入队\n",x);p->data=x;p->next=NULL;q->rear->next=p;q->rear=p;}

判断队伍空则真,否则为假:

bool empty_LQueue(LQueue *q){if(q->rear==q->front){printf("队空!\n"); return true ;}return false;}

出队:

int out_LQueue(LQueue *q,datatype *x){Queue *p;if(empty_LQueue(q))return 0;p=q->front->next;q->front->next=p->next;*x=p->data;free(p);if(q->front->next==NULL)q->rear=q->front;//只有一个元素时,出队后队空,修改队尾指针return true; }




0 0