选择是一种奇异的链表
Selection sort of a singular linked list
我需要开发一种算法来进行奇异链表的选择,它不会分配或释放任何内存,下面的代码能做到吗?如何修改此代码以使用for循环而不是while循环?
/************************** SortList ************************************
Description Arranges the singly linked list pointed to by List in
natural order. It is assumed that the list has a dummy
head node.
The algorithm used is a linked variation of the selection
sort and works like this:
Start with EndSorted aimed at first node of list
repeat
Find smallest char between EndSorted and end of list
Swap smallest element with char in EndSorted
Change EndSorted to next node
until we get to end of list
None of the pointers in linked list are changed
Parameters
IN, List A pointer to a singly linked list with a dummy head node
-----------------------------------------------------------------------*/
typedef Node* NodePtr;
void SortList(NodePtr List)
{
NodePtr SmallNode; //points to smallest char
NodePtr SearchNode; //used to search each node in list
NodePtr EndSorted; //points to list to sort
char TempCh;
if (List->Link != NULL) //List is not empty
EndSorted = List->Link; //make EndSorted point to the beginning of List
else //List is empty
EndSorted = List; //EndSorted points to dummy head Node and the following loop
//will never execute
while (EndSorted->Link != NULL) //make sure EndSorted is not at the end of List
{
SmallNode = EndSorted; //give SmallNode a starting value
SearchNode = EndSorted->Link; //make SearchNode point to the Node after EndSorted
while (SearchNode != NULL) //make sure SearchNode is not at the end of List
{
if (SearchNode->Ch < SmallNode->Ch) //check the Ch value of the two Nodes
SmallNode = SearchNode; //if SearchNode -> Ch is smaller then SmallNode -> Ch
//make SmallNode point to SearchNode
SearchNode = SearchNode->Link; //advance SearchNode to the next Node
}
TempCh = EndSorted->Ch; //place the Ch value in EndSorted in TempCh
EndSorted->Ch = SmallNode->Ch; //swap SmallNode -> Ch with EndSorted -> Ch
//This places the smallest unsorted value in List at the beginning
SmallNode->Ch = TempCh;
EndSorted = EndSorted->Link; //advance EndSorted to the next Node
}
}
所以它应该是这样的?
void SortList(NodePtr List)
{
NodePtr SmallNode; //points to smallest char
NodePtr SearchNode; //used to search each node in list
NodePtr EndSorted; //points to list to sort
char TempCh;
if (List->Link != NULL) //Makes sure the list is not empty
{
/* (Points EndSorted at the first non-dummy node node; While EndSorted is not at the end of the list;
Advance EndSorted to the next node) */
for (EndSorted = List->Link; EndSorted->Link != NULL; EndSorted = EndSorted->Link)
{
SmallNode = EndSorted; //Start SmallNode with the data of the first (Non-Dummy) Node
/*Points SearchNode at the Node after the Current EndSorted location; While Search Node is not at the end of the list;
Advance SearchNode to The next node*/
for (SearchNode = EndSorted->Link; SearchNode != NULL; SearchNode = SearchNode->Link)
{
if (SearchNode->Ch < SmallNode->Ch) //compares the values of the two nodes
{
SmallNode = SearchNode; //if search node is smaller, swap them
} //to update the smallest node on this pass
//once all values have been checked, and the smallest is found
//it will be moved to the front of the list, or, after the node
//it is slightly larger than
} //smallest node has been found, begin swap and end the inner while loop
TempCh = EndSorted->Ch; //TempCh holds the value of the Ch held by EndSorted
EndSorted->Ch = SmallNode->Ch; //EndSorted now holds the smallest unsorted node's value
SmallNode->Ch = TempCh; //SmallNode now holds the value EndSorted originally held
}
}
}
选择排序的算法是:
int min;// min element is declared
for (int i = 0; i < size; ++i)
{
min=i;
for (int j = i + 1; j < size+1; ++j)
{
if (ar[j] < ar[min])
{
min = j;
}
}
swap (ar[i],ar[min]);
}
我已经为数组做了但对于链表,所应用的概念也将是相同的,只是有任何变量将用于遍历,ar[i]将被返回节点值的函数取代。列表将被遍历,直到结束节点表示node.next()=无效的对于算法,u可以访问链接:http://www.sanfoundry.com/cplusplus-program-implement-selection-sort/
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 尝试连接两种不同类型的结构来创建链表
- 链表一副牌,只出现51张牌,没有出现52张
- 是否有一种模式可以使用协议缓冲区而不针对LibProtobuf链接
- 关于链表,这两种方法有什么不同?C++中的1个自变量与2个自变量
- 在 C 中创建一种查找表
- 一种方式链接列表和不需要的第一个元素AFRTER填充
- 链表的几种实现 - C++
- 查找表是哈希表的一种形式吗
- 选择是一种奇异的链表
- 有没有一种方法可以在不重建任何项目的情况下重新链接解决方案
- C++将节点添加到链表的开头一次后失败.其他方法也有问题
- C++将原始表强制转换为另一种表类型
- 有没有一种方法可以检查堆叠的协同程序是否在给定链的上下文中
- Qt中有没有一种方法可以获取表单上的所有小部件
- 为已经存在的注册表类使用包装类是一种好方法吗?
- 最后一个人站着,使用了圆形链表
- 是否有一种方法将我的GCL Lisp文件与Windows上的单独c++程序链接
- 链表vs.动态数组实现一副牌?c++
- 是他们编译一个项目的一种方式,这样你的所有函数类都可以.进入一个可以链接的文件