我不知道為何程式中的deleteLastNode和deleteMiddleNode為何叫不出來
應該的結果是
10-->null
20-->10-->null
30-->20-->10-->null
30-->20-->10-->40-->null
30-->20-->50-->10-->40-->null
20-->50-->10-->null
20-->10-->null
但我得結果是
10-->null
20-->10-->null
30-->20-->10-->null
30-->20-->10-->40-->null
30-->20-->50-->10-->40-->null
20-->50-->10-->40-->null
求大大解惑
我的程式碼(C++)
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
Node* push(struct Node* head,int node_data)
{
struct Node* newNode = new Node;
newNode->data =node_data;
newNode->next=head;
head = newNode;
return head;
}
Node* append(struct Node* head,int node_data){
Node*newNode = new Node;
Node *current = head;
newNode->data=node_data;
newNode->next = NULL;
if(head==NULL){
head = newNode;
return head;
}
while (current->next != NULL)
current = current->next;
current->next = newNode;
return head;
}
Node* insert(struct Node* head,int num,int node_data){
int listlen =0;
if(head ==NULL){
cout<<"the given previous node is required,cannot be NULL"<<endl;
return NULL;
}
if (num == 0){
cout<<"input number can't be rero"<<endl;
return head;
}
struct Node* newNode=new Node;
struct Node* count_list = head;
struct Node*current = head;
while (count_list != NULL){
listlen++;
count_list = count_list->next;
}
newNode->data=node_data;
if(num>=listlen){
cout<<"input number over list length"<<endl;
}
else{
for(int i=1;i<num;i++)
current = current->next;
newNode->next = current->next;
current->next = newNode;
}
return head;
}
Node* deleteFirstNode(struct Node* head)
{
if(head == NULL)
return NULL;
Node* tempNode=head;
head = head->next;
delete tempNode;
return head;
}
Node* deleteLastNode(struct Node* head)
{
if(head == NULL){
return NULL;
}
if(head->next=NULL){
delete head;
return NULL;
}
Node* second_last = head;
while (second_last->next->next != NULL){
second_last = second_last->next;
}
delete (second_last->next);
second_last->next=NULL;
return head;
}
Node* deleteMiddleNode(struct Node* head,int num){
if(head ==NULL){
cout<<"the given previous node is required,cannot be NULL"<<endl;
return NULL;
}
int listlen=0;
struct Node*remove_pre = head;
struct Node*count_list = head;
struct Node*remove;
while(count_list != NULL){
listlen++;
count_list = count_list->next;
}
if(num==0,num >= listlen){
cout<<"input number over list length"<<endl;
}
else{
for(int i=1;i<num;i++)
remove_pre=remove_pre->next;
remove=remove_pre->next;
remove_pre->next = remove->next;
delete remove;
}
return head;
}
void displayList(struct Node *node)
{
while(node != NULL)
{
cout<<node->data<<"-->";
node = node->next;
}
if(node == NULL)
cout<<"null";
}
int main(){
Node*head=NULL;
head = append(head,10);
cout<<" "<<endl;
displayList (head);
head = push(head,20);
cout<<" "<<endl;
displayList (head);
head = push(head,30);
cout<<" "<<endl;
displayList (head);
head = append(head,40);
cout<<" "<<endl;
displayList (head);
head = insert(head,2,50);
cout<<" "<<endl;
displayList (head);
head = deleteFirstNode(head);
cout<<" "<<endl;
displayList (head);
head =deleteLastNode(head);
cout<<" "<<endl;
displayList (head);
head =deleteMiddleNode(head,1);
cout<<" "<<endl;
displayList (head);
return 0;
}
head = deleteFirstNode(head);
cout<<" "<<endl;
displayList (head);
head =deleteLastNode(head);
cout<<" "<<endl;
displayList (head);
head =deleteMiddleNode(head,1);
cout<<" "<<endl;
displayList (head);
改成
head = deleteFirstNode(head);
/*
cout<<" "<<endl;
displayList (head);
*/
head =deleteLastNode(head);
cout<<" "<<endl;
displayList (head);
head =deleteMiddleNode(head,1);
cout<<" "<<endl;
displayList (head);
我用了但還是不行 改的是最後一段嗎
1.是,改的是最後一段
2.我再測了一下,你的 deleteLastNode function 有錯,其他 deleteFirstNode, deleteMiddleNode 都沒錯,只要修正 deleteLastNode 就可以正確了
我可以問一下如何修改嗎?? 順帶一提 我是看這個影片做的https://youtu.be/VtrrLX4rif8