首页 \ 问答 \ 单个圆形链表排序(Sorting singly circular linked list)

单个圆形链表排序(Sorting singly circular linked list)

我想在每次编辑后对单个循环链表进行排序。 但我的代码不起作用。 我基于选择排序算法。 我已经这样做了好几个小时但似乎无法获得正确的代码。

void editList(node *head, int value, int newValue)
{
    node *traverser = head;
    do {
        traverser = traverser -> next;
    }while(traverser -> data != value);

    traverser -> data = newValue;

    node *index;
    node *selection;
    node *temp = new node;

    for(index = head; index -> next != head; index = index -> next) {

        for(selection = head; selection -> next != head; selection = selection -> next) {
            if(index -> data > selection -> data) {
                temp -> data = index-> data;
                index -> data = selection -> data;
                selection -> data = temp -> data;

            }
        }//End of outer loop

    }//End of sorting

    return;
}//End of editList()

I'm trying to sort the singly circular linked list after each edit. But my code doesn't work. I based it on the selection sort algorithm. I've been doing this for hours but can't seem to get the correct code.

void editList(node *head, int value, int newValue)
{
    node *traverser = head;
    do {
        traverser = traverser -> next;
    }while(traverser -> data != value);

    traverser -> data = newValue;

    node *index;
    node *selection;
    node *temp = new node;

    for(index = head; index -> next != head; index = index -> next) {

        for(selection = head; selection -> next != head; selection = selection -> next) {
            if(index -> data > selection -> data) {
                temp -> data = index-> data;
                index -> data = selection -> data;
                selection -> data = temp -> data;

            }
        }//End of outer loop

    }//End of sorting

    return;
}//End of editList()

原文:https://stackoverflow.com/questions/41854509
更新时间:2021-09-13 21:09

最满意答案

似乎你过早地调用generatePost()函数(当文档加载时,但同时,这也是当你加载jQuery时...为什么你得到了“)”缺少错误..)。

所以我将HTML更改为:

<head>
</head>
<body>
    <main>
        <div class="container">
            <div id="postcontainer">

            </div>
        </div>
    </main>
</body>

而jQuery:

$(document).ready(function() {
        function generatePost(title, time, text) {
        var postCount = 0;
        $('#postcontainer').append('<div id=' + "post_" + postCount +  '></div>').attr('class', 'content');
        $('#post_' + postCount).append('<h3>' + title + '</h3>').attr('id', 'post_h3_' + postCount);
        $('#post_h3_' + postCount).append('<span>' + time + '</span>');
        var paragraphs = text.split("||");

        for (var i = 0; i < paragraphs.length; i++) {
            var paragraphCount = 0;
            $('#post_' + postCount).append('<p>' + paragraphs[i] + '</p>').attr('id', 'post_p_' + postCount + '_' + paragraphCount);
            paragraphCount++;
        }
        postCount++;
    }  
         generatePost("Testing Title", "I don't know", "This is || a paragraph"); 
});

只需在document.ready结束时调用该函数。 此外,正如您所看到的,我还遇到了一些关于如何分配帖子ID的问题,所以最后也将其切换为一个简单的字符串插值。

干杯,

PG


Seems you were calling the generatePost() function too early (when document loads, but at the same time, thats also when you load your jQuery... why you were getting the ")" Missing error..).

So I changed the HTML to:

<head>
</head>
<body>
    <main>
        <div class="container">
            <div id="postcontainer">

            </div>
        </div>
    </main>
</body>

And the jQuery to:

$(document).ready(function() {
        function generatePost(title, time, text) {
        var postCount = 0;
        $('#postcontainer').append('<div id=' + "post_" + postCount +  '></div>').attr('class', 'content');
        $('#post_' + postCount).append('<h3>' + title + '</h3>').attr('id', 'post_h3_' + postCount);
        $('#post_h3_' + postCount).append('<span>' + time + '</span>');
        var paragraphs = text.split("||");

        for (var i = 0; i < paragraphs.length; i++) {
            var paragraphCount = 0;
            $('#post_' + postCount).append('<p>' + paragraphs[i] + '</p>').attr('id', 'post_p_' + postCount + '_' + paragraphCount);
            paragraphCount++;
        }
        postCount++;
    }  
         generatePost("Testing Title", "I don't know", "This is || a paragraph"); 
});

Just calling the function at the end of the document.ready. Also, as you can see, I also ran into some issues about how the post's id was being assigned, so ended up switching that up as well, assigning it with some simple string interpolation.

Cheers,

PG

相关问答

更多
  • 解决:我通过使用纯Javascript而不是使用jQuery来解决这个问题。 我试着加载jquery.min.js,正常的jquery.js,并确保它与我运行wkhtmltopdf时位于同一主机上。 没有工作。 当我使用普通的JavaScript时,它运行良好。 SOLVED: I solved this issue by using pure Javascript instead of using jQuery. I tried loading jquery.min.js, normal jquery.j ...
  • 终于解决了。 我仍然不知道为什么它不起作用,但根据我对这些html-to-pdf库的经验, 总有些事情会在某些时候出错。 我设法生成(并下载)我的PDF成功使用原始工具所有这些库建立在: wkhtmltopdf 。 这是我的代码,它可能会帮助一些人: Symfony控制器: /** * @Route("/report") */ public function report(Request $request) { $idTown = 1;//$request->request->get("idTo ...
  • 好的,我会和孩子一起坐在角落里吃酱! 是的,问题是我没有加载jQuery API。 关于为什么我在脚本标签内有评论的问题。 显然,当您将XSL名称空间定义为XHTML时,它不喜欢空脚本标记。 它迫使你实际在标签内放置一些东西。 一旦我删除了XHTML定义,问题也就消失了。 OK I'll go sit in the corner with the kids that eat paste now!! Yes the issue was that I didn't have the jQuery API loa ...
  • 有没有理由不只是使用.empty() ? 而且,顺便提一下,如果你的问题是关于'效率',只要效率与速度大致相当,那么我可以建议JS Perf进行自我测试吗? 顺便说一句,在JS Perf比较中 ,使用Chromium 18 / Ubuntu 11.04时, .empty()似乎始终是更快的方法。 参考文献: 除此之外; 如果你不介意使用普通的原生JavaScript,那么它更快 (在我的Samsung II上,DOM运行速度为〜82k ops / sec,在我的桌面上(没什么特别)在Chromium 18 ...
  • 使用#mydiv过滤器在文档上绑定click事件,以便在包含id mydiv添加事件的元素的html动态绑定时。 jQuery(document).on('click', "#mydiv", function(event) { alert('clicked'); }); 或者,如果将响应添加到“#container”,则可以将事件绑定到“#container”, jQuery('#container').on('click', "#mydiv", function(event) { ale ...
  • 屏幕截图中的内容并不完全是一个微调器 - 它只是一个< ...
  • 你在本地运行这个项目吗? 如果是这样,请尝试在WAMP,MAMP或其他本地Web服务器上运行它。 Are you running this project locally? If so try running it on WAMP, or MAMP, or some other local web server.
  • 切换功能需要参数或什么都不需要。 当没有传递参数时,参数本身可以直接从dom获得。 在html中,toggle函数获取this关键字。 元素本身已通过。 但是如果没有传递参数,则参数是未定义的,因此可以动态计算: 在切换功能中尝试更改: function toggle(source = false) { if(!source) { var s ...
  • 似乎你过早地调用generatePost()函数(当文档加载时,但同时,这也是当你加载jQuery时...为什么你得到了“)”缺少错误..)。 所以我将HTML更改为:
    而jQuery: $(do ...
  • 如果代码: $(".alert-btn").click(function(){ alert("Hey there!"); }); 在文件test.js您应该在加载DOM元素后加载此文件: