- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个代码,我必须插入一个带有随机数的节点并按顺序删除每个数字。我已经完成了其他功能,我正在尝试编写 void InsertNode(int a)
和 void DeleteNode(int a)
代码。当我运行它时,我得到以下结果。
50 30 20 10 70 80 60 90 100 40
50 is inserted
30 is inserted
20 is inserted
...
100 is inserted
50 is inserted
30 is inserted
20 is inserted
...
100 is inserted 50 is inserted
30 is inserted
20 is inserted
...
100 is inserted 50 is inserted
30 is inserted
20 is inserted
...
100 is inserted
Good Job!
但是结果应该是这样的:
50 30 20 10 70 80 60 90 100 40
50 is inserted
30 is inserted
20 is inserted
...
100 is inserted
10 20 30 40 50 60 70 80 90 100
Good Job!
50 is deleted
10 20 30 40 60 70 80 90 100
30 is deleted
10 20 40 60 70 80 90 100
20 is deleted
10 40 60 70 80 90 100
10 is deleted
40 60 70 80 90 100
70 is deleted
40 60 80 90 100
80 is deleted
40 60 90 100
60 is deleted
40 90 100
90 is deleted
40 100
100 is deleted
40
40 is deleted
这是全部代码。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Node{
public:
int value;
Node *next;
};
class LinkedList{ //Singly Linked List
public:
LinkedList(){
head=NULL;
}
void SubMain(){
bool result;
UniqueRandomData(10);
CallInsertNode();
PrintNode();
result=CheckNode();
CallDeleteNode();
}
void UniqueRandomData(int n){
int i, j, k, temp;
this->n=n;
x=new int[n];
for(i=0; i<n; i++)x[i]=(i+1)*10;
for(i=0; i<n; i++){
j=rand()%n;
k=rand()%n;
temp=x[j];
x[j]=x[k];
x[k]=temp;
}
for(i=0; i<n; i++)
cout<<x[i]<<" ";
cout<<endl;
}
void InsertNode(int a){
if (head == NULL) {
head = new Node();
}
else{
head->next = new Node();
head = head->next;
}
for(int i=0; i<n; i++){
cout<<x[i]<<" is inserted."<<endl;
}
}
void PrintNode(){
Node *cur=head;
while(cur!=0){
cout<<cur->value<<" ";
cur=cur->next;
}
cout<<endl;
}
void CallInsertNode(){
int i;
for(i=0; i<n; i++)
InsertNode(x[i]);
}
bool CheckNode(){
Node *cur=head;
while(cur !=NULL && cur->next !=NULL)
if(cur->value > cur->next->value){
cout<<"Error!"<<endl;
cout<<cur->value<<", "<<cur->next->value<<endl;
return false;
}
else
cur=cur->next;
cout<<"Good job!"<<endl;
return true;
}
void CallDeleteNode(){
int i;
for(i=0; i<n; i++){
DeleteNode(x[i]);
this->PrintNode();
}
}
void DeleteNode(int a){
Node* prev = this->head;
Node* current = this->head->next;
while(current->value != a){
current = current->next;
prev = prev->next;
}
if(current->value == a){
prev->next = current->next;
delete current;
}
for(int i=0; i<n; i++){
cout<<x[i]<<" is deleted.";
}
}
private:
Node *head;
int *x;
int n;
};
int main(){
LinkedList x;
x.SubMain();
return 0;
//system("pause");
}
我应该如何处理 void InsertNode(int a)
和 void DeleteNode(int a)
函数?
最佳答案
head->next = new Node();
head = head->next;
您在 head 之后添加了一个节点(因此丢失了曾经存在的所有节点)。然后你将头向下移动一个位置。
Before Insert is called:
********* ********* *********
head->* 10 *-->* 20 *-->* 50 *--|
********* ********* *********
After Insert(15) is called:
head
|
\/
********* ********* ********* *********
* 10 *-->* 15 *--| * 20 *-->* 50 *--|
********* ********* ********* *********
Before DeleteNode is called:
********* ********* *********
head->* 10 *-->* 20 *-->* 50 *--|
********* ********* *********
然后调用 DeleteNode(10) 将不会删除头项。
您的类(class)没有实现三原则。
以下成员确实与列表无关。
void SubMain(){
void UniqueRandomData(int n){
void CallInsertNode(){
void CallDeleteNode(){
int *x;
int n;
你应该把他们带出类。独立功能很好。但如果您愿意,可以将它们放在测试类中。
关于c++ - 为链表创建 "void InsertNode(int a)"和 "void DeleteNode(int a)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141231/
我有这个代码。选择文本时,将出现一个包含颜色的 div。单击其中一种颜色时,所选文本将包含该突出显示。但是,如何检查突出显示是否在另一个突出显示内部?如果突出显示位于另一个突出显示内部,我希望出现一个
我正致力于将纯 JS 文本突出显示脚本转换为 jQuery,但我试图找出与 insertNode 等效的 jQuery 是什么: var selection = window.getSelection
我正在创建一个 chrome 扩展。基本上,如果用户单击内容菜单选项,我想在所选文本前后插入 HTML 格式标签。 标签被添加到一个事件页面中,在添加格式标签之后,将新值触发到消息对象的“粘贴”键中的
我有以下方法在插入符号位置粘贴一些 HTML,使用 `range.insertNode(),源自 this SO answer function insertAtCursor(html) {
这是一个旨在在基本链表末尾插入新节点的函数。 node *insert(node *head, int data) { if (head == NULL) return create
有谁知道如何将浏览器选择设置为新/独立创建的范围?我了解如何从浏览器中获取文本选择,我了解如何创建范围,但我不知道如何告诉浏览器将选择更改为我创建的范围。我原以为它会是类似“setSelection”
为了兼容性,我使用 Rangy 在 WYSIWYG 编辑器 (uEditor) 中插入节点。 它可以工作,但是在 IE Some text //Javascript
如何使用 Range.insertNode() 将 IMG 元素动态插入到 DOM 中? 我的 HTML: 我的JS: var button = docu
我创建了自己的指令,如果用户未登录则隐藏内容:*onlyUser。当我尝试在使用 ng/animation 的组件中使用它时,有时会出现错误: Cannot read property 'insert
当使用 insertNodes 时,将为节点创建一个唯一的 ID。 insertNodes(addSelected, data, before, anchor) 我们如何为新节点指定一个特定的名称/文
我正在尝试在当前选择的正下方添加一个新行,然后将选择放到新行中。 let current_path = props.selection.anchor.path[0] Transforms.insert
我正在开发一个 contenteditable div 来制作一个简单的 RichText 编辑器。我的要求之一是能够在按钮事件的光标位置插入 html block 。 通过使用 range、sele
我正在制作一个代码,我必须插入一个带有随机数的节点并按顺序删除每个数字。我已经完成了其他功能,我正在尝试编写 void InsertNode(int a) 和 void DeleteNode(int
我是一名优秀的程序员,十分优秀!