mark下双向队列,比起循环队列,遍历更加轻松愉快,
复杂度还是O(n):
#include
#include
typedef struct Node {
int Data;
struct Node *prior, *next;
} DuNode, *DuLinklist;
DuLinklist Create_Linklist() {
DuLinklist Head = (DuLinklist) malloc(sizeof(DuNode));
Head->next = NULL;
Head->prior = NULL;
return Head;
}
void Insert_Linklist(DuLinklist Head, int num) {
DuLinklist temp = (DuLinklist) malloc(sizeof(DuNode));
if (Head->next == NULL) {
temp->Data = num;
temp->next = Head->next;
temp->prior = Head;
Head->next = temp;
}
else {
temp->Data = num;
temp->next = Head->next;
Head->next->prior = temp;
temp->prior = Head;
Head->next = temp;
}
}
//遍历到尾再往回遍历
void Foreach_Linklist(DuLinklist Head) {
DuLinklist temp = Head->next;
while (temp->next) {
printf("%d\n", temp->Data);
temp = temp->next;
}
while(temp){
printf("%d\n", temp->Data);
temp = temp->prior;
}
}
int main(void) {
DuLinklist Head = Create_Linklist();
Insert_Linklist(Head, 3);
Insert_Linklist(Head, 4);
Insert_Linklist(Head, 5);
Foreach_Linklist(Head);
}
转载请标注