繁体   English   中英

以排序方式将元素插入到单个循环链表中

[英]Inserting a element in a singly circular linked list in sorted manner

我想创建一个程序,以排序方式(给定指向最后一个元素的指针)将数据插入单个循环链接列表中。 我已经编写了代码,尝试对其进行调试,但实际上无法找到问题所在。 我得到的输出是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);
}

让我们通过散步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;
}

首先,我们更改一些标识符,以便它们更能描述其真正代表的内容

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;
}

现在,我们开始打印要迭代的项目,而不是一遍又一遍地打印第一个项目来修复问题。

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;
}

现在我们要做的是在下一个要打印的项目再次是第一个项目时停止,即last->next

void displaylist(Node *last){
    Node* current = last->next;
    do{
        cout<<current->val<<" "; 
        current = current->next; 
    }while(current != last->next); 
    cout<<endl;
}

那应该做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM