# 嵌入式 用链表实现直接选择排序和直接插入排序示例

496人阅读 评论(0)

mysort()为直接插入排序，mysort2()为直接选择排序；

1. #include
2. #include
3. typedef structnode
4. {
5. int data;
6. struct node*next;
7. }NODE;
8. NODE * mycreate()
9. {
10. NODE * head = NULL;
12. }
14. {
17. {
21. }
22. else
23. {
24. while(q->next !=NULL)
25. {
26. q = q->next;
27. }
28. NODE *newnode = (NODE *)malloc(sizeof(NODE));
29. newnode->data = data;
30. q->next = newnode;
31. newnode->next = NULL;
32. }
34. }
36. {
39. NODE * max;
40. NODE * tmp = NULL;
41. max = (NODE *)malloc(sizeof(NODE));
45. {
48. while(q->next !=NULL)
49. {
50. if(q->next->data >max->data)
51. {
52. max->data = q->next->data;
53. tmp = q->next;
54. maxpre = q;
55. }
56. q = q->next;
57. }
59. if(tmp !=NULL)
60. {
61. maxpre->next = tmp->next;
63. {
65. }
66. free(tmp);
67. tmp = NULL;
68. }
69. else
70. {
73. }
74. }
76. }
78. {
81. NODE *p = NULL;
82. NODE *max = NULL;
83. NODE *premax = NULL;
84. NODE *nextmax = NULL;
85. NODE *prelast = NULL;
86. NODE *lastnext = NULL;
87. int tmp;
88. while(last->next !=NULL)
89. {
90. p = last;
91. lastnext = last->next;
92. max = p;
93. while(p->next !=NULL)
94. {
95. if(p->next->data >max->data)
96. {
97. max = p->next;
98. premax = p;
99. }
100. p = p->next;
101. }
103. {
104. nextmax = max->next;
105. if(premax ==last)
106. {
107. last->next = nextmax;
108. max->next = last;
110. }
111. else
112. {
113. premax->next = last;
115. max->next = lastnext;
117. }
120. }
121. else
122. {
123. nextmax = max->next;
124. prelast->next = max;
125. if(premax ==last)
126. {
127. max->next = last;
128. last->next = nextmax;
129. }
130. else
131. {
132. premax->next = last;
133. last->next = nextmax;
134. max->next = lastnext;
135. }
136. last = prelast->next;
137. prelast = last;
138. last = last->next;
139. }
140. }
142. }
144. {
146. {
147. printf("Empty !\n");
148. }
149. else
150. {
152. {
155. }
157. }
158. }
159. intmain()
160. {
168. printf("排序前：");
171. printf("排序后：");
173. return 0;
174. }
#include
#include

typedef struct node
{
int data;
struct node *next;
}NODE;

NODE * mycreate()
{
}

{
{
}
else
{
while(q->next != NULL)
{
q = q->next;
}
NODE *newnode = (NODE *)malloc(sizeof(NODE));
newnode->data = data;
q->next = newnode;
newnode->next = NULL;
}
}

{
NODE * max;
NODE * tmp = NULL;
max = (NODE *)malloc(sizeof(NODE));
{
while(q->next != NULL)
{
if(q->next->data > max->data)
{
max->data = q->next->data;
tmp = q->next;
maxpre = q;
}
q = q->next;
}
if(tmp != NULL)
{
maxpre->next = tmp->next;
{
}
free(tmp);
tmp = NULL;
}
else
{
}
}
}

{
NODE *p = NULL;
NODE *max = NULL;
NODE *premax = NULL;
NODE *nextmax = NULL;
NODE *prelast = NULL;
NODE *lastnext = NULL;
int tmp;
while(last->next != NULL)
{
p = last;
lastnext = last->next;
max = p;
while(p->next != NULL)
{
if(p->next->data > max->data)
{
max = p->next;
premax = p;
}
p = p->next;
}
{
nextmax = max->next;
if(premax == last)
{
last->next = nextmax;
max->next = last;
}
else
{
premax->next = last;
max->next = lastnext;
}
}
else
{
nextmax = max->next;
prelast->next = max;
if(premax == last)
{
max->next = last;
last->next = nextmax;
}
else
{
premax->next = last;
last->next = nextmax;
max->next = lastnext;

}
last = prelast->next;
prelast = last;
last = last->next;
}

}
}

{
{
printf("Empty !\n");
}
else
{
{
}
}
}
int main()
{
printf("排序前：");
printf("排序后：");
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：979825次
• 积分：17634
• 等级：
• 排名：第540名
• 原创：741篇
• 转载：522篇
• 译文：0篇
• 评论：105条
阅读排行
最新评论