隊列編程題
#include<stdio.h>
#include<stdbool.h>
#include<malloc.h>
typedef
int
typedata;
struct
node
{
struct
node
*prev,
*next;
typedata
data;
};
typedef
struct
node
node;
typedef
struct
node*
link;
//
============init_head===============
//
//頭節點的初始化
link
init_head(void)
{
link
head
=
(link)malloc(sizeof(node));
if(head
!=
NULL)
{
head->prev
=
head->next
=
head;
}
return
head;
}
//
============newnode
================
//
//創建新節點
link
newnode(typedata
data)
{
link
new
=
(link)malloc(sizeof(node));
if(new
!=
NULL)
{
//前趨指針和後趨指針都指向自己
new->prev
=
new->next
=
new;
new->data
=
data;
}
return
new;
}
//
=================is_empty================
//
bool
is_empty(link
head)
{
//為空時,頭節點的前趨指針和後趨指針都指向head(頭節點)
if((head->next==head)
&&
(head->prev==head))
return
true;
return
false;
}
//
================insert_tail
==================
//
void
insert_tail(link
head,
link
new)
{
if(is_empty(head))
{
//第一個節點插入
head->next
=
head->prev
=
new;
new->next
=
new->prev
=
head;
return
;
}
//除了第一個節點插入
new->prev
=
head->prev;
new->next
=
head;
new->prev->next
=
new;
new->next->prev
=
new;
}
//
================show
====================
//
void
show(link
head)
{
//為空時,直接返回
if(is_empty(head))
return
;
//遍歷整個鏈
link
tmp
=
head->next;
while(tmp
!=
head)
{
printf("%d\t",
tmp->data);
tmp
=
tmp->next;
}
printf("\n");
}
//
==============insert_opint
===============
//
void
insert_opint(link
end_node,
link
p)
{
p->prev
=
end_node;
p->next
=
end_node->next;
end_node->next->prev
=
p;
end_node->next
=
p;
}
//
================insertion_sort===========
//
//順序排序
void
insertion_sort(link
head)
{
if(is_empty(head))
return;
//把隊列分拆,頭節點和第一個節點為一個已排序的隊列,
//其他的節點逐個比較已排序隊列插
link
p
=
head->next->next;
head->prev->next
=
NULL;
head->next->next
=
head;
head->next->prev
=
head;
head->prev
=
head->next;
while(p
!=
NULL)
{
link
end_node
=
head->prev;
if(p->data
>
end_node->data)
{
link
tmp
=
p;
p
=
p->next;
insert_tail(head,
tmp);
}
else
{
while(end_node!=head
&&
p->data<end_node->data)
end_node
=
end_node->prev;
link
tmp
=
p;
p
=
p->next;
insert_opint(end_node,
tmp);
}
}
}
int
main(void)
{
link
head
=
init_head();
if(head
==
NULL)
{
printf("falure\n");
return
0;
}
typedata
data;
while(1)
{
if(scanf("%d",
&data)
!=
1
)
break;
link
new
=
newnode(data);
if(new
==
NULL)
{
printf("falure\n");
return
0;
}
insert_tail(head,
new);
show(head);
}
printf("the
figure
is:\n");
show(head);
insertion_sort(head);
show(head);
return
0;
}
2. C語言編程 迷宮問題(隊列)
c#界面繪制的時候,底層重繪每次會清除畫布背景,然後再全部重新繪制,這才是導致閃爍最主要的原因。於是重載消息發送函數操作,禁掉這條消息。代碼如下:
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0014) // 禁掉清除背景消息
return;
base.WndProc(ref m);
}
