链对的节点,指针,顺序 | |
链队的定义节点定义 指针定义 | //节点定义 typedef struct queuenode { int data; queuenode* next; }queuenode,*queueptr; // 链(指针)定义 typedef struct linkqueue { queueptr front,rear; }linkqueue; |
链队的push声明 主要行为 行为代码 | status queuepush (linkqueue* pq,int e) 创建节点 node的data和next赋值 加入链表,rear指向调整 queueptr p=(queueptr)malloc(sizeof(queuenode));
p->data=e; p->next=NULL;
pq->rear->next=p; pq->rear=p; |
链队的pop声明 主要行为 行为代码 补充行为 总体行为代码 |
status queuepop (linkqueue * pq,int*pe) 创建p,并使p指向node1 p的data给pe传出来 头node指向node2 销毁node1 queueptr p=pq->front->next;
*pe=p->data;
pq->front->next=p->next;
free (p); 空队报错 假如rear指向的node就是被销毁的node,rear指向头node if(pq->front==pq->rear) return ERROR; queueptr p=pq->front->next;
*pe=p->data;
pq->front->next=p->next;
if(p->next==NULL) pq->rear=pq->front;
free (p); |
代码汇总:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include <string.h>
//-----------------------------
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int status;
#define MAXSIZE 20
//-----------------------------
//节点定义
typedef struct queuenode
{
int data;
queuenode* next;
}queuenode,*queueptr;
// 链(指针)定义
typedef struct linkqueue
{
queueptr front,rear;
}linkqueue;
status queuepush (linkqueue* pq,int e)
{
queueptr p=(queueptr)malloc(sizeof(queuenode));
p->data=e;
p->next=NULL;
pq->rear->next=p;
pq->rear=p;
}
status queuepop (linkqueue * pq,int*pe)
{
if(pq->front==pq->rear)
return ERROR;
queueptr p=pq->front->next;
*pe=p->data;
pq->front->next=p->next;
if(p->next==NULL)
pq->rear=pq->front;
free (p);
}
void test()
{
}
int main()
{
test();
system("pause");
return 0;
}