我正在尝试删除特定数据的节点。为此,我使用deleteNode函数但无法删除。请参阅代码:
#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 :-**
此处启动删除节点的功能。我试图删除有数据3的节点我正在从主函数输入数据。
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{
ptr=ptr->next->next;
return;
}
ptr=ptr->next;
}
}
void takeInput(node*& head)
{
int d;
cin>>d;
while(d!=-1)
{
addAtEnd(head,d);
cin>>d;
}
}
void print(node* head)
{
while(head!=NULL)
{
cout<<head->data<<"=>";
head=head->next;
}
}
AddAtFront(node* &head,int d)
{
///create new node;
node*n=new node(d);
n->next=head;
head=n;
}
int main()
{
node* head(NULL);
takeInput(head);
print(head);
cout<<endl<<endl<<endl<<"---------- Here The Insertion Process starts at different Positions -----------"<<endl;
cout<<endl<<"Adding at Tail"<<endl;
AddAtTail(head,9);
print(head);
cout<<endl<<"Adding at Position p"<<endl;
int p,d;
cout<<"Enter Position and data :"<<endl;
cin>>p>>d;
AddAtPosition(head,p,d);
print(head);
cout<<endl<<"Adding at Front"<<endl;
cout<<"Enter data to add at front : "<<endl;
cin>>d;
AddAtFront(head,d);
print(head);
cout<<endl<<endl<<endl;
cout<<endl<<"-------------------- NOW LETS PERFORM DELETION ------------------"<<endl;
cout<<endl<<"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;
cin>>d;
deleteData(head,d);
print(head);
cout<<endl;
return 0;
}
请参阅我尝试删除节点的DeleteNode函数。 为什么节点没有删除?这是功能:
**///Delete Specific Node i.e- data :-**
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{
ptr=ptr->next->next;
return;
}
ptr=ptr->next;
}
}
但是节点没有删除。
答案 0 :(得分:1)
您的delete...
函数实际上并没有删除任何内容。您只是在操纵指针,但正在泄漏实际的node
对象。并且您没有考虑被删除的节点是head
节点的可能性,这需要更新head
以指向下一个节点。
此外,您的函数将在空列表中崩溃,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::list
(双重链接)或std::forward_list
(单链接)。让STL为你努力工作。
答案 1 :(得分:0)
ptr=ptr->next->next;
您需要更改其&#34; next&#34;值。类似的东西(我没有编译和测试过这个;这是一个指向正确方向的建议。)
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;
}
}