C++ 借助给定数据从单链表中删除特定节点
我正在尝试删除特定数据的节点。为此,我正在使用deleteNode函数,但无法删除。请参阅代码:C++ 借助给定数据从单链表中删除特定节点,c++,linked-list,singly-linked-list,C++,Linked List,Singly Linked List,我正在尝试删除特定数据的节点。为此,我正在使用deleteNode函数,但无法删除。请参阅代码: #include<iostream> using namespace std; class node { public: int data; node* next; ///constructor for initializing the data- node(int d) { data=d; next=
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
///constructor for initializing the data-
node(int d)
{
data=d;
next=NULL;
}
};
void addAtEnd(node* &head,int data)
{
if(head==NULL)
{
head=new node(data);
return ;
}
node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
node* n =new node(data);
n->data=data;
temp->next=n;
n->next=NULL;
return;
}
AddAtTail(node* head,int d)
{
node* ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
node *n=new node(d);
ptr->next=n;
n->next=NULL;
}
AddAtPosition(node* head,int p,int d)
{
///2 3 4 6 7 8 -1
///4th Position-
node*ptr=head;
int jump=1;
while(jump<=p-1)
{
jump++;
ptr=ptr->next;
}
node*n=new node(d);
n->next=ptr->next;
ptr->next=n;
}
/// Delete First node
void deleteFirst(node *&head)
{
head=head->next;
}
///Delete last node;
void deleteLast(node* head)
{
node* ptr=head;
while(ptr->next->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=NULL;
}
**///Delete Specific Node :-**
但节点没有删除
ptr=ptr->next->next代码>
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{ /* pointer->next points to the node you want to delete.
point it instead to the one beyond,
freeing the memory of the deleted node */
node * pNodeToDelete = ptr->next;
ptr->next=ptr->next->next;
free(pNodeToDelete)
return;
}
ptr=ptr->next;
}
}
您的
delete…
函数实际上并没有删除任何内容。您只是在操作指针,但正在泄漏实际的节点
对象。并且您没有考虑被删除的节点是头部
节点的可能性,这需要更新头部
以指向下一个节点
此外,函数将在空列表上崩溃,deleteLast
将在节点数少于2个的列表上崩溃
并且deleteData
未正确枚举节点
请尝试类似以下内容:
#include <iostream>
using namespace std;
class node {
public:
int data;
node* next;
///constructor for initializing the data-
node(int d) {
data=d;
next=NULL;
}
};
node* findLast(node *head, node **before) {
if (before) *before = NULL;
node *last = head;
if (last) {
while (last->next) {
if (before) *before = last;
last = last->next;
}
}
return last;
}
node* addAtFront(node* &head, int data) {
node* n = new node(data);
n->next = head;
head = n;
return n;
}
node* addAtEnd(node* &head, int data) {
node *last = findLast(head, NULL);
node* n = new node(data);
if (last) {
last->next = n;
} else {
head = n;
}
return n;
}
node* addAtPosition(node* &head, int p, int d) {
if ((!head) || (p <= 0)) {
return addAtFront(head, d);
}
node *ptr = head;
node *temp;
do {
temp = ptr;
ptr = ptr->next;
}
while ((ptr) && (--p > 0));
node *n = new node(d);
n->next = temp->next;
temp->next = n;
return n;
}
/// Delete First node
void deleteFirst(node* &head) {
if (head) {
node *ptr = head;
head = head->next;
delete ptr;
}
}
///Delete last node
void deleteLast(node* &head) {
node *beforeLast;
node *last = findLast(head, &beforeLast);
if (last) {
if (beforeLast) {
beforeLast->next = NULL;
}
if (head == last) {
head = NULL;
}
delete last;
}
}
///Delete Specific Node
void deleteData(node* &head, int d) {
node *before = NULL;
node *ptr = head;
while (ptr) {
if (ptr->data == d) {
if (before) {
before->next = ptr->next;
}
if (head == ptr) {
head = head->next;
}
delete ptr;
return;
}
before = ptr;
ptr = ptr->next;
}
}
void takeInput(node* &head) {
int d;
if (!((cin >> d) && (d != -1))) return;
node *last = findLast(head, NULL);
node *n = new node(d);
if (last) {
last->next = n;
} else {
head = n;
}
last = n;
while ((cin >> d) && (d != -1)) {
n = new node(d);
last->next = n;
last = n;
}
}
void print(node* head) {
while (head) {
cout << head->data << "=>";
head = head->next;
}
}
int main() {
node* head = NULL;
takeInput(head);
print(head);
cout << endl;
cout << endl << endl;
cout << "---------- Here The Insertion Process starts at different Positions -----------" << endl << endl;
cout << "Adding at End" << endl;
addToEnd(head, 9);
print(head);
cout << endl;
cout << "Adding at Position p" << endl;
int p, d;
cout << "Enter Position and data :" << endl;
if (cin >> p >> d) {
addAtPosition(head, p, d);
print(head);
cout << endl;
}
cout << "Adding at Front" << endl;
cout << "Enter data to add at front : " << endl;
if (cin >> d) {
addAtFront(head, d);
print(head);
cout << endl;
}
cout << endl << endl << endl;
cout << "-------------------- NOW LETS PERFORM DELETION ------------------" << endl << endl;
cout << "Deleting first node :" << endl;
deleteFirst(head);
print(head);
cout << endl;
cout << endl << "Deleting Last node :" << endl;
deleteLast(head);
print(head);
cout << endl;
cout << "deleting specific node" << endl;
cout << "Enter data to delete" << endl;
if (cin >> d) {
deleteData(head, d);
print(head);
cout << endl;
}
cout << "deleting remaining nodes" << endl;
node *ptr = head;
while (ptr) {
node *temp = ptr;
ptr = ptr->next;
delete temp;
}
return 0;
}
#包括
使用名称空间std;
类节点{
公众:
int数据;
节点*下一步;
///用于初始化数据的构造函数-
节点(int d){
数据=d;
next=NULL;
}
};
节点*findLast(节点*头部,节点**之前){
if(before)*before=NULL;
节点*最后一个=头部;
如果(最后){
while(上一次->下一次){
如果(之前)*之前=最后一次;
最后一个=最后一个->下一个;
}
}
最后返回;
}
节点*添加前端(节点*&头部,内部数据){
节点*n=新节点(数据);
n->next=头部;
水头=n;
返回n;
}
节点*添加结束(节点*&头,整数数据){
node*last=findLast(head,NULL);
节点*n=新节点(数据);
如果(最后){
last->next=n;
}否则{
水头=n;
}
返回n;
}
节点*添加位置(节点*&头部,内部p,内部d){
如果((!head)| |(p下一步;
}
而((ptr)&(-p>0));
节点*n=新节点(d);
n->next=temp->next;
温度->下一步=n;
返回n;
}
///删除第一个节点
void deleteFirst(节点*&头){
若有(总目){
节点*ptr=头部;
头部=头部->下一步;
删除ptr;
}
}
///删除最后一个节点
void deleteLast(节点*&头){
节点*在最后一个节点之前;
node*last=findLast(head和beforeLast);
如果(最后){
如果(上次之前){
beforeLast->next=NULL;
}
如果(头==最后一个){
head=NULL;
}
最后删除;
}
}
///删除特定节点
void deleteData(节点*&头部,int d){
node*before=NULL;
节点*ptr=头部;
while(ptr){
如果(ptr->data==d){
如果(之前){
before->next=ptr->next;
}
如果(水头=ptr){
头部=头部->下一步;
}
删除ptr;
返回;
}
前=ptr;
ptr=ptr->next;
}
}
无效输入(节点*&头部){
int d;
如果(!((cin>>d)&(d!=-1))返回;
node*last=findLast(head,NULL);
节点*n=新节点(d);
如果(最后){
last->next=n;
}否则{
水头=n;
}
last=n;
而((cin>>d)&(d!=-1)){
n=新节点(d);
last->next=n;
last=n;
}
}
无效打印(节点*头){
while(head){
下一步是收集数据;
}
}
int main(){
node*head=NULL;
输入(头);
打印头;
cout Off-topic ish:你应该new
你也应该delete
。建议修复缺少返回类型的函数。在我复制你的问题之前,程序由于列表中的一个循环而挂起。你有多个bug。使用mark-1眼球,ptr=ptr->next->next;
修改一个临时本地函数,并生成no更改列表。deleteData
:1)从不检查head
是否为空2)从不检查head
3)ptr=ptr->next->next;
保存的数据是错误的。可能ptr->next=ptr->next->next;
4)从不调用delete
如果有人知道如何在代码块中获取我的最后一个大括号,请编辑它,或者更好的是,告诉我如何操作。是的,我在按下时用代码块的其余部分选择了它我删除了{}按钮。我也尝试过删除它并在代码块中重新插入它……谢谢!ptr->next=ptr->next->next;。但是当我将它存储在指针中时,程序没有给出正确的输出。所以我没有释放内存。我不明白你最后的评论——你的意思是什么,“但当我把它存储在指针中时,程序就没有给出正确的输出。”?你期望它不会给出什么输出?你说的“当我把它存储在指针中时”是什么意思?@kindacoder——它现在对你有用吗?我不明白你在我上一个问题上面的评论中的最后一个问题。
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{ /* pointer->next points to the node you want to delete.
point it instead to the one beyond */
ptr->next=ptr->next->next;
return;
}
ptr=ptr->next;
}
}
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{ /* pointer->next points to the node you want to delete.
point it instead to the one beyond,
freeing the memory of the deleted node */
node * pNodeToDelete = ptr->next;
ptr->next=ptr->next->next;
free(pNodeToDelete)
return;
}
ptr=ptr->next;
}
}
#include <iostream>
using namespace std;
class node {
public:
int data;
node* next;
///constructor for initializing the data-
node(int d) {
data=d;
next=NULL;
}
};
node* findLast(node *head, node **before) {
if (before) *before = NULL;
node *last = head;
if (last) {
while (last->next) {
if (before) *before = last;
last = last->next;
}
}
return last;
}
node* addAtFront(node* &head, int data) {
node* n = new node(data);
n->next = head;
head = n;
return n;
}
node* addAtEnd(node* &head, int data) {
node *last = findLast(head, NULL);
node* n = new node(data);
if (last) {
last->next = n;
} else {
head = n;
}
return n;
}
node* addAtPosition(node* &head, int p, int d) {
if ((!head) || (p <= 0)) {
return addAtFront(head, d);
}
node *ptr = head;
node *temp;
do {
temp = ptr;
ptr = ptr->next;
}
while ((ptr) && (--p > 0));
node *n = new node(d);
n->next = temp->next;
temp->next = n;
return n;
}
/// Delete First node
void deleteFirst(node* &head) {
if (head) {
node *ptr = head;
head = head->next;
delete ptr;
}
}
///Delete last node
void deleteLast(node* &head) {
node *beforeLast;
node *last = findLast(head, &beforeLast);
if (last) {
if (beforeLast) {
beforeLast->next = NULL;
}
if (head == last) {
head = NULL;
}
delete last;
}
}
///Delete Specific Node
void deleteData(node* &head, int d) {
node *before = NULL;
node *ptr = head;
while (ptr) {
if (ptr->data == d) {
if (before) {
before->next = ptr->next;
}
if (head == ptr) {
head = head->next;
}
delete ptr;
return;
}
before = ptr;
ptr = ptr->next;
}
}
void takeInput(node* &head) {
int d;
if (!((cin >> d) && (d != -1))) return;
node *last = findLast(head, NULL);
node *n = new node(d);
if (last) {
last->next = n;
} else {
head = n;
}
last = n;
while ((cin >> d) && (d != -1)) {
n = new node(d);
last->next = n;
last = n;
}
}
void print(node* head) {
while (head) {
cout << head->data << "=>";
head = head->next;
}
}
int main() {
node* head = NULL;
takeInput(head);
print(head);
cout << endl;
cout << endl << endl;
cout << "---------- Here The Insertion Process starts at different Positions -----------" << endl << endl;
cout << "Adding at End" << endl;
addToEnd(head, 9);
print(head);
cout << endl;
cout << "Adding at Position p" << endl;
int p, d;
cout << "Enter Position and data :" << endl;
if (cin >> p >> d) {
addAtPosition(head, p, d);
print(head);
cout << endl;
}
cout << "Adding at Front" << endl;
cout << "Enter data to add at front : " << endl;
if (cin >> d) {
addAtFront(head, d);
print(head);
cout << endl;
}
cout << endl << endl << endl;
cout << "-------------------- NOW LETS PERFORM DELETION ------------------" << endl << endl;
cout << "Deleting first node :" << endl;
deleteFirst(head);
print(head);
cout << endl;
cout << endl << "Deleting Last node :" << endl;
deleteLast(head);
print(head);
cout << endl;
cout << "deleting specific node" << endl;
cout << "Enter data to delete" << endl;
if (cin >> d) {
deleteData(head, d);
print(head);
cout << endl;
}
cout << "deleting remaining nodes" << endl;
node *ptr = head;
while (ptr) {
node *temp = ptr;
ptr = ptr->next;
delete temp;
}
return 0;
}