求数据结构(c++)写一段程序,链表中找到含x的那一个节点并删除它。求完整的程序可运行的

2025-02-24 18:12:27
推荐回答(2个)
回答1:

temp=p;
p=p->next;
temp->next=NULL;
这三句存在问题,temp=p,让temp指向p所指向的节点,p=p->next,p指向后移
temp->next=NULL,让temp的后继为空,这里出了问题,链表从temp指向的节点断开,相当于删除p之后的所有节点。

应该先判断p是不是最后节点
if(p->next==NULL)
如果是,只好去找p的前趋pre,让pre->next=NULL,free(p)
如果不是最后节点,将p的后继节点数值域复制给p,然后将p的后继节点删除,等同与删除p
p->data=p->next->data;
p->next=p->next->next;
free(p);

回答2:

/*
* modification time 2019-7-30
*
* author zyb
*
* description This is a dynamic linked list demo,
* dynamic allocation memory and delete memory
* */

#include
using namespace std;

struct Student{
char *name;
Student *next;
};
Student *createDynamicLinkedList();
void showNode(Student *head);//show linked list
void showConversely(Student *head);
bool insertNodeAfter(Student *head,Student *insertNode,int nodePosition);
bool deleteNode(Student *head,int deletePosition);
bool deleteNode(Student *head,string deletePosition);
int size(Student *head);
bool find(Student *node,string str);
int main()
{
Student *head=createDynamicLinkedList();
showNode(head);
#if 0
int x=0;
cout<<"plead enter you soon delete node number:"<cin>>x;
deleteNode(head,x);
#else
string x;

cout<<"plead enter you soon delete name:"<cin>>x;
deleteNode(head,x);
showNode(head);
//head=head++;
//Student *temp=head++;
//cout<name;
//showNode(head);

#endif
cout<<"count :"<return 0;
}

bool deleteNode(Student *head,string deletePosition){
Student *temp=head;
Student *temp2=temp->next;

while(temp2!=NULL){
if(find(temp2,deletePosition)){
Student *n=temp2;
temp2=temp2->next;
temp->next=temp2;
delete n;
continue;
}

temp2=temp2->next;
temp=temp->next;
}
return false;

}

bool find(Student *node,string str){
if(node!=NULL){
string temp=node->name;
string::size_type idx;
idx=temp.find(str);
if(idx!=string::npos)
return true;
else
return false;
}
return false;
}
bool deleteNode(Student *head,int deletePosition){
int count=0;
Student *temp=head;
while(temp!=NULL){
count++;
if(count==deletePosition){
if(temp->next!=NULL){
if(temp->next->next!=NULL){
Student *dd=temp->next->next;
delete temp->next;
temp->next=dd;
}else{

delete temp->next;
}
return true;
}
return false;
}

temp=temp->next;
}
return false;

}
Student *createDynamicLinkedList(){

cout << "Please input data (0 is exit)" << endl;
char *data = new char[40];
cin >> data;
if(*data=='0')
return NULL;

Student *head, *s1, *s2;
head = new Student;
head->name="head";
s1 = s2 = head;

while (*data != '0')
{

s1 = new Student;
s1->name = data;
s2->next = s1;
s2 = s1;
cout << "Please input data (0 is exit)" << endl;
data = new char[40];
cin >> data;
}
s2->next = NULL;
return head;
}
int size(Student *head){
int count=0;
while(head!=NULL){
head=head->next;
count++;
}
return count-1;
}
bool insertNodeAfter(Student *head,Student *insertNode,int nodePosition){
//if node is null ,return false
if(insertNode==NULL)
return false;
int count=0;
while(head->next!=NULL){
count++;

if(count==nodePosition){
//Student *temp=head;
Student *temp2=head->next;
head->next=insertNode;
insertNode->next=temp2;
return true;
}
head=head->next;

}
head->next=insertNode;
insertNode->next=NULL;

return true;
}
/**
* discription: This is output function,order of the output is first in,
* first out.Be carefu! The memory of the application is not
* release.
* parament:
* Student *head:"Student" type pointer.
*
*/

void showNode(Student *head){
cout<<"showNode() Function run..."<while (head != NULL){
if(head->name!="head")
cout << head->name << endl;
head = head->next;
}
}
/**
* discription: This is a output and release memory function,order of the * output and release is last in first out.
* parament:
* Student *head: "Student" type pointer.
*/
void showConversely(Student *head){
if (head == NULL)
return;
if (head->next != NULL){
showConversely(head->next);
}
cout << "delete " << head->name << endl;
delete head;
head=NULL;
}
//不知道x是什么,就当字符串处理了,输入数字也行