向环状链表插入元素
来源:互联网 发布:禾田软件价格 编辑:程序博客网 时间:2024/04/28 23:11
Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the list.
EDIT:
Thanks to dear readers Saurabh andreader who pointed out my mistake. When the list has only one value, inserting a different value would be handled by case 3), not case 1). Besides, I believe I did not explain “What is a cyclic sorted list” nicely, as this had caused some confusion. Imagine you have a sorted list, but its tail points back to its head. In other words, the list must have a minimum node, continue in a non-descending order, and eventually points back to this minimum node itself. And the only way to access the list is via aNode, which can point to any node in the list anddoes not necessarily point to the minimum node.
First, it is important that you understand what a cyclic linked list is. A cyclic linked list differs from a normal linked list in that its tail node points back to its head node instead of NULL.
This problem seems a little tricky because the given node is not necessarily the list’s head (ie, the node that has the smallest element). It shouldn’t take you too long to come up with an idea, but beware. There are hidden traps around the corner and you are bound to make some mistakes if you are not careful in your thoughts.
A cyclic sorted linked list. Note that the tail is pointing back to its head. The only reference to the list is a given node which can be any node in the list. Let’s say that you need to insert 4 into the list.
This is how the cyclic list becomes after inserting 4. Note that the cyclic linked list remained in sorted order.
Hints:
It is best to list all kinds of cases first before you jump into coding. Then, it is much easier to reduce the number of cases your code need to handle by combining some of them into a more generic case. Try to also list down all possible edge cases if you have time. You might discover a bug before you even start coding!
Solution:
Basically, you would have a loop that traverse the cyclic sorted list and find the point where you insert the value (Let’s assume the value being inserted calledx). You would only need to consider the following three cases:
- prev→val ≤ x ≤ current→val:
- Insert between prev and current.
- x is the maximum or minimum value in the list:
- Insert before the head. (ie, the head has the smallest value and its prev→val > head→val.
- Traverses back to the starting point:
- Insert before the starting point.
Most people have no problem getting case 1) working, while case 2) is easy to miss or being handled incorrectly. Case 3), on the other hand is more subtle and is not immediately clear what kind of test cases would hit this condition. It seemed that case 1) and 2) should take care of all kinds of cases and case 3) is not needed. Think again… How can you be sure of that? Could you come up with one case where it hits case 3)?
- Q: What if the list has only one value?
- A:
Handled by case 1). Handled by case 3). - Q: What if the list is passed in as NULL?
- A: Then handle this special case by creating a new node pointing back to itself and return.
- Q: What if the list contains all duplicates?
- A: Then it has been handled by case 3).
Below is the code. You could combine both negation of case 1) and case 2) in the while loop’s condition, but I prefer to use break statements here to illustrate the above idea clearer.
void insert(Node *& aNode, int x) { if (!aNode) { aNode = new Node(x); aNode->next = aNode; return; } Node *p = aNode; Node *prev = NULL; do { prev = p; p = p->next; if (x <= p->data && x >= prev->data) break; // For case 1) if ((prev->data > p->data) && (x < p->data || x > prev->data)) break; // For case 2) } while (p != aNode); // when back to starting point, then stop. For case 3) Node *newNode = new Node(x); newNode->next = p; prev->next = newNode;}
- 向环状链表插入元素
- 删除环状单向链表中的重复元素的操作
- 环状链表unique
- Insert into a sorted circular linked list 环状链表 插入节点
- JAVA环状单向链表去除连续的重复元素(求代码)
- 向数组中插入元素
- 向数组中插入元素
- 【c++】向顺序线性表中插入元素
- 链表环状检测主要有三种方法
- 数据结构—环状链表瞎搞
- 双向链表-插入元素
- 利用Javascript向页面中插入元素
- 向数组指定位置插入元素
- jQuery向匹配元素中插入新元素
- 5、向数组中插入一个元素
- java向数组中插入元素
- 向数组中插入元素排序
- 编码实现环状单向链表(尾指针直接指向头指针,中间没有空节点),去除连续的重复元素的操作
- 函数atof,atoi,atol,strtod,strtol,strtoul
- paip.提升安全性---WEB程序安全检测与防范
- JVM内存结构系列:HotSpot内存结构最清晰
- 每个程序员都应该知道的8个LINUX命令
- JAVA SE重点知识点串讲总结_panfanglin
- 向环状链表插入元素
- 常用的Web控件
- 生男生女的面试题
- 从今天起记录见过的Manager
- File.mkdirs和File.mkdir的区别
- amazon的几道笔试题
- python成三棋源代码
- 设计模式——模板方法
- ora-12545错误解决方法