生活随笔
收集整理的這篇文章主要介紹了
数据结构之链式队列
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
鏈?zhǔn)疥犃?/h3>- 思維導(dǎo)圖:
- 鏈隊列基本操作的實現(xiàn):
- 定義鏈隊列:
- 帶頭節(jié)點的初始化:
- 帶頭結(jié)點的判空:
- 帶頭結(jié)點的入隊:
- 帶頭節(jié)點的出隊:
- 不帶頭節(jié)點的初始化:
- 不帶頭節(jié)點的判空:
- 不帶頭結(jié)點的入隊:
- 不帶頭結(jié)點的出隊:
思維導(dǎo)圖:
鏈隊列基本操作的實現(xiàn):
定義鏈隊列:
typedef struct LinkNode
{ int data
;struct LinkNode
*next
;
}LinkNode
;
typedef struct{ LinkNode
*front
,*rear
;
}LinkQueue
;
帶頭節(jié)點的初始化:
void InitQueue(LinkQueue
&Q
){Q
.front
= Q
.rear
= (LinkNode
*)malloc(sizeof(LinkNode
));Q
.front
->next
= NULL;
}
帶頭結(jié)點的判空:
bool
IsEmpty(LinkQueue Q
){if(Q
.front
== Q
.rear
)return true
;elsereturn false
;
}
帶頭結(jié)點的入隊:
void EnQueue(LinkQueue
&Q
,int x
){LinkNode
*s
= (LinkNode
*)malloc(sizeof(LinkNode
));s
->data
= x
;s
->next
= NULL;Q
.rear
->next
= s
;Q
.rear
= s
;
}
帶頭節(jié)點的出隊:
bool
DeQueue(LinkQueue
&Q
,int &x
){if(Q
.front
== Q
.rear
)return false
;LinkNode
*p
= Q
.front
->next
;x
= p
->data
;if(Q
.rear
== p
)Q
.rear
= Q
.front
;elseQ
.front
->next
= p
->next
;free(p
);return true
;
}
不帶頭節(jié)點的初始化:
void InitQueue(LinkQueue
&Q
){Q
.rear
= NULL;Q
.front
= NULL;
}
不帶頭節(jié)點的判空:
bool
IsEmpty(LinkQueue Q
){if(Q
.front
== NULL)return true
;elsereturn false
;
}
不帶頭結(jié)點的入隊:
void EnQueue(LinkQueue
&Q
,int x
){LinkNode
*s
= (LinkNode
*)malloc(sizeof(LinkNode
));s
->data
= x
;s
->next
= NULL; if(Q
.front
== NULL){Q
.front
= s
;Q
.rear
= s
;}else{Q
.rear
->next
= s
;Q
.rear
= s
;}
}
不帶頭結(jié)點的出隊:
bool
DeQueue(LinkQueue
&Q
,int &x
){if(Q
.front
== NULL)return false
;LinkNode
*p
= Q
.front
;x
= p
->data
;if(p
== Q
.rear
){Q
.front
= NULL;Q
.rear
= NULL; }elseQ
.front
= p
->next
;free(p
);return true
;
}
總結(jié)
以上是生活随笔為你收集整理的数据结构之链式队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。