C++ 在列表末尾插入整数并在第n个位置删除
因此,在我的链表程序中,我想让它做的是询问用户要输入多少个数字,然后输入这些数字,并将这些数字添加到列表的末尾。然后,它将打印列表。之后,用户将选择要删除的列表中元素的位置,并再次打印列表C++ 在列表末尾插入整数并在第n个位置删除,c++,linked-list,C++,Linked List,因此,在我的链表程序中,我想让它做的是询问用户要输入多少个数字,然后输入这些数字,并将这些数字添加到列表的末尾。然后,它将打印列表。之后,用户将选择要删除的列表中元素的位置,并再次打印列表 #include <iostream> using namespace std; struct Node{ int data; Node* link; }; Node* head; void Insert(int data){ //insert an integer at the en
#include <iostream>
using namespace std;
struct Node{
int data;
Node* link;
};
Node* head;
void Insert(int data){ //insert an integer at the end of the list
Node* temp = new Node();
Node* temp2 = new Node();
temp->data = data;
temp->link = NULL;
if(head = NULL){
head = temp;
return;
}
temp2 = head;
while(temp2->link != NULL){
temp2 = temp2->link;
}
temp2->link = temp;
}
void Delete(int n){ //delete an integer at nth position
Node* temp1 = new Node();
temp1 = head;
if(n == 1){ //if the first node is to be deleted
head = temp1->link; //now head points to second node
delete temp1; //delete first node
return;
}
for(int i = 0; i < n-2; i++){
temp1 = temp1->link; //temp1 points to (n-1)th node
}
Node* temp2 = temp1->link; //temp2 points to nth node
temp1->link = temp2->link; // pointing to (n+1)th node
delete temp2; //deleting nth node
}
void Print(){ //print out the list
Node* printNode = head;
cout << "List: ";
while(printNode != NULL){
cout << printNode->data;
cout << " ";
printNode = printNode->link;
}
cout << "\n";
}
int main(){
int x, count, n;
head = NULL; //start with an empty list
cout << "How many numbers? " << endl;
cin >> count;
for(int i = 0; i < count; i++){
cout << "Enter number: ";
cin >> x;
Insert(x);
}
Print();
cout << "Enter position to delete: ";
cin >> n;
Delete(n);
Print();
return 0;
}
#包括
使用名称空间std;
结构节点{
int数据;
节点*链接;
};
节点*头;
void Insert(int data){//在列表末尾插入一个整数
Node*temp=新节点();
Node*temp2=新节点();
温度->数据=数据;
temp->link=NULL;
if(head=NULL){
压头=温度;
返回;
}
temp2=头部;
while(temp2->link!=NULL){
temp2=temp2->link;
}
temp2->link=temp;
}
void Delete(int n){//删除第n个位置的整数
Node*temp1=新节点();
temp1=头部;
如果(n==1){//如果要删除第一个节点
head=temp1->link;//现在head指向第二个节点
delete temp1;//删除第一个节点
返回;
}
对于(int i=0;ilink;//temp1指向第(n-1)个节点
}
Node*temp2=temp1->link;//temp2指向第n个节点
temp1->link=temp2->link;//指向第(n+1)个节点
delete temp2;//删除第n个节点
}
void Print(){//打印列表
节点*打印节点=头部;
cout n;
删除(n);
打印();
返回0;
}
接受第一个数字后,程序停止工作。我能知道我哪里把代码弄错了吗?我能做些什么来提高代码的效率?提前感谢。您可能需要重新考虑插入功能。代码崩溃的部分是在while循环插入期间。如果您希望
temp2
保存数据,那么您需要动态地为它分配空间,正如您所做的那样。但是,您只是将其用作位置指示器(遍历列表)——那么,为什么需要分配空间来指向列表中的头部或任何其他节点位置呢
下面是我将如何插入列表(当然在后面):
void Insert(int data){//在列表末尾插入一个整数
Node*temp=新节点();
//这是为了确保创建了temp->也称为防御性编程。
如果(!temp)
{
就我而言,这只是一个小错误。代码已被更正
if(head == NULL){
head = temp;
return;
}
当前只浏览了代码。if(head=NULL)
应该是if(head=NULL)
。不知道这是否解决了整个问题,但这是一个问题start@tomasbasham对我来说,这是一个很大的facepalm。它现在已经修复了。谢谢,伙计。没问题。我可以指出,保存对链接列表尾部的引用,而不是每次插入number@tomasbasham另一个gr吃提示,谢谢!这是一个很大的帮助,也教了很多东西。对于我不好的命名法,我应该更改哪一部分?在使用Pos_指示符后,is说Pos_指示符没有在函数中声明。严格按照你的说明,所以我不确定出了什么问题。你是如何声明的?我能够在VS 2013中编译并运行,没有问题。它是decla红色使用Node*Pos_指示符对吗?或者我缺少一个标题吗?我使用dev cpp,我将尝试使用VS.@nisyedlah运行它-没错,我不熟悉dev cpp-我只是在虚拟linux机器上使用g++编译器运行它,它在那里也工作得很好。
if(head == NULL){
head = temp;
return;
}