[英]Inserting a element in a singly circular linked list in sorted manner
I want to create a program which inserts data in a singly circular linked list in sorted manner (given the pointer to last element). 我想创建一个程序,以排序方式(给定指向最后一个元素的指针)将数据插入单个循环链接列表中。 I have written the code, tried to debug it, but not able to find what is going wrong actually. 我已经编写了代码,尝试对其进行调试,但实际上无法找到问题所在。 The output I am getting is 5 6 6 7 7 7 9 9 9 我得到的输出是5 6 6 7 7 7 9 9 9
#include <iostream>
using namespace std;
//structure for list
class Node{
public:
int val;
Node* next;
};
//function to add new node and data
Node* insertnode(Node** l,int data){
Node *new_node, *temp;
new_node = new Node;
new_node->val = data;
//if list is empty
if(*l == nullptr){
new_node->next = new_node;
*l = new_node;
return *l;
}
//if new element value is greater than last
if(((*l)->val)<(new_node->val)){
new_node->next = (*l)->next;
(*l)->next = new_node;
*l = new_node;
return *l;
}
temp = (*l)->next;
//if new element value is low than last
if(((*l)->val)>(new_node->val)){
while(temp!=*l){
if(temp->val>=new_node->val){
break;
}
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
return (*l);
}
void displaylist(Node *l){
Node* last = l->next;
do{
cout<<l->val<<" ";
last = last->next;
}while(last->next != l);
cout<<endl;
}
int main()
{
Node* last = nullptr;
last = insertnode(&last, 5);
displaylist(last);
last = insertnode(&last, 6);
displaylist(last);
last = insertnode(&last, 7);
displaylist(last);
last = insertnode(&last, 5);
displaylist(last);
last = insertnode(&last, 9);
displaylist(last);
}
Let's take a walk through displaylist
让我们通过散步displaylist
void displaylist(Node *l){
Node* last = l->next; // points at first node. Sounds OK
do{
cout<<l->val<<" "; // always print last node. Doesn't seem like a good idea
last = last->next; //advance one node. Sounds OK
}while(last->next != l); // loop until the node after the next node is back to the last node
// Looks past at least one node
cout<<endl;
}
First we change some of the identifiers around so they are more descriptive of what they really represent 首先,我们更改一些标识符,以便它们更能描述其真正代表的内容
void displaylist(Node *last){ // l is the last node
Node* current = last->next; //current is a better name for the item we're looking at
do{
cout<<last->val<<" ";
current = current->next;
}while(current->next != last);
cout<<endl;
}
now we start fixing things by printing out the item we're iterating rather than the first item over and over again. 现在,我们开始打印要迭代的项目,而不是一遍又一遍地打印第一个项目来修复问题。
void displaylist(Node *last){
Node* current = last->next;
do{
cout<<current->val<<" "; // print out the item we're iterating
current = current->next;
}while(current->next != last);
cout<<endl;
}
Now what we want to do is stop when the next item to print is the first one again, that's last->next
现在我们要做的是在下一个要打印的项目再次是第一个项目时停止,即last->next
void displaylist(Node *last){
Node* current = last->next;
do{
cout<<current->val<<" ";
current = current->next;
}while(current != last->next);
cout<<endl;
}
That should about do it. 那应该做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.