[英]Sorted insert using Linked List
我在链表中为排序插入实现一个迭代函数时遇到一些困难。 我以前用递归做了这个,这在调用insert()
函数时要容易得多,但在这里我对如何实现(l->data < data)
条件有点困惑:
typedef struct E_Type * List;
struct E_Type
{
int data;
struct E_Type* next;
};
和功能:
insert(List & l, int data){
if (l == 0 || l->data > data){
List new_list = new E_Type;
new_list->data = data;
new_list->next = l;
l = new_list;
}
else if (l->data < data){
List new_list = new E_Type;
new_list->data = data;
new_list->next = l; //i am shooting in the dark with this one
l = new_list;
}
}
我不会为你编写代码,但会提供一些提示。
从根本上说,有两种情况:
l
。 如果我是你,我会首先使用笔和纸来处理这两种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.