我正在编写一个程序,该程序接受8个用户输入的整数,并从中得出一个链表。我让程序打印链接列表,然后删除最后一个节点,然后反向打印列表。在测试过程中,我一直在进行编程以确保每个部分都工作正常,并且可以完成打印原始链接列表的工作。
当我完成修改的代码编写并打印部分时,我遇到了一个问题-在打印出原始列表后,该程序将不会输出任何内容。因此,例如,如果我输入1,2,3,4,5,6,7,8,它将输出: 1个 2 3 4 5 6 7 8 就是这样。我已经尝试过将cout <<“ testing”;在不同的位置查看我的代码停止输出的位置,以及成功输出的最新位置就在while循环之前。
我不确定为什么while循环会导致程序停止输出任何内容,甚至是与while循环本身无关的任意cout语句,因此我想在这里问一下。如果有帮助,我正在使用Visual Studio 2017。感谢您提供的所有帮助!
#include <iostream>
using namespace std;
void getdata(int & info); //function that assigns a user inputted value to each node
const int nil = 0;
class node_type // declaration of class
{
public:
int info;
node_type *next;
};
int main()
{
node_type *first, *p, *q, *r, *newnode;
first = new node_type;
newnode = new node_type;
int info;
getdata(info); //first node
(*first).info = info;
(*first).next = nil;
getdata(info); //second node
(*newnode).info = info;
(*first).next = newnode;
(*newnode).next = nil;
p = newnode;
for (int i = 2; i < 8; i++) //nodes 3-8
{
newnode = new node_type;
getdata(info);
(*newnode).info = info;
(*p).next = newnode;
p = newnode;
(*newnode).next = nil;
}
q = first;
while (q != nil) // printing linked list
{
cout << (*q).info << "\n";
q = (*q).next;
}
//deletes last node then reverses list
p = first;
q = (*p).next;
r = (*q).next;
if (first == nil) //if list is empty
cout << "Empty list";
else if ((*first).next == nil) //if list has one node
first = nil;
else if (r == nil) //if list has two nodes
q = nil;
else //general case
{
(*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
while ((*r).next != nil)
{
(*q).next = p;
(*r).next = q;
p = q;
q = r;
r = (*r).next;
}
(*q).next = p;
first = q;
}
q = first;
while (q != nil) // printing newly modified list.
{
cout << (*q).info << "\n";
q = (*q).next;
}
return 0;
}
void getdata(int & info)
{
cout << "Enter number: \n";
cin >> info;
}
答案 0 :(得分:0)
class HTMLBaseElement extends HTMLElement {
constructor(...args) {
const self = super(...args)
self.parsed = false // guard to make it easy to do certain stuff only once
self.parentNodes = []
return self
}
setup() {
// collect the parentNodes
let el = this;
while (el.parentNode) {
el = el.parentNode
this.parentNodes.push(el)
}
// check if the parser has already passed the end tag of the component
// in which case this element, or one of its parents, should have a nextSibling
// if not (no whitespace at all between tags and no nextElementSiblings either)
// resort to DOMContentLoaded or load having triggered
if ([this, ...this.parentNodes].some(el=> el.nextSibling) || document.readyState !== 'loading') {
this.childrenAvailableCallback();
} else {
this.mutationObserver = new MutationObserver(() => {
if ([this, ...this.parentNodes].some(el=> el.nextSibling) || document.readyState !== 'loading') {
this.childrenAvailableCallback()
this.mutationObserver.disconnect()
}
});
this.mutationObserver.observe(this, {childList: true});
}
}
}
在反转列表时会陷入无限循环。这就是为什么它什么都不输出的原因。
将元素2链接到元素3后,下一步将元素3链接到元素2。
在遍历列表的同时反转它会导致无限循环。
在一般情况下将其反转的正确方法如下:
class MyComponent extends HTMLBaseElement {
constructor(...args) {
const self = super(...args)
return self
}
connectedCallback() {
// when connectedCallback has fired, call super.setup()
// which will determine when it is safe to call childrenAvailableCallback()
super.setup()
}
childrenAvailableCallback() {
// this is where you do your setup that relies on child access
console.log(this.innerHTML)
// when setup is done, make this information accessible to the element
this.parsed = true
// this is useful e.g. to only ever attach event listeners once
// to child element nodes using this as a guard
}
}
在旁注中,应使用else //general case
{
(*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
while ((*r).next != nil)
{
(*q).next = p;
(*r).next = q;
p = q;
q = r;
r = (*r).next;
}
(*q).next = p;
first = q;
}
而不是else //general case
{
node_type* current = first;
node_type* next = first->next;
(*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
while (next)
{
node_type* temp = next->next;
next->next = current;
current = next;
next = temp;
}
first = current;
}
取消引用。