-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueNode.c
More file actions
64 lines (64 loc) · 1.17 KB
/
QueueNode.c
File metadata and controls
64 lines (64 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
#define MAXSIZE 100
typedef struct queuenode
{
ElemType data;
struct queuenode *next;
}QueueNode;
typedef struct
{
QueueNode *front;
QueueNode *rear;
}q;
QueueNode *QueueNode_Init(void)
{
QueueNode *p=(QueueNode*)malloc(sizeof(QueueNode));
p->data=0;
p->next=NULL;
q*qu=(q*)malloc(sizeof(q));
qu->front=p;
qu->rear=p;
return p;
}
//把头节点当成队头,进行入队(尾插法)
void Que_In(q *qu,ElemType e)
{
QueueNode *p=(QueueNode *)malloc(sizeof(QueueNode));
p->data=e;
p->next=NULL;
qu->rear->next=p;
qu->rear=p;
}
//出队(删除头节点后的第一个节点)
void Que_Out(q*qu,ElemType *e)
{
QueueNode *p=qu->front->next;
*e=p->data;
qu->front->next=p->next;
if(qu->rear==p)
{
qu->rear=qu->front;
}
free(p);
}
//测试
int main(void)
{
q *qu=(q*)malloc(sizeof(q));
qu->front=QueueNode_Init();
qu->rear=qu->front;
ElemType e;
Que_In(qu,1);
Que_In(qu,2);
Que_In(qu,3);
Que_Out(qu,&e);
printf("出队元素:%d\n",e);
QueueNode *p=qu->front->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}