iT邦幫忙

0

c++串列問題 求大大解惑

我不知道為何程式中的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;
}

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-01-22 17:24:35
最佳解答
    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

找到了,把

Node* deleteLastNode(struct Node* head)
{
if(head == NULL){
	   return NULL;
}
	
	   
if(head->next=NULL){
		delete head;
		return NULL;
	}

改成

Node* deleteLastNode(struct Node* head)
{
if(head == NULL){
	   return NULL;
}
	
	   
if(head->next==NULL){  //這列你少了一個 = (等號)
		delete head;
		return NULL;
	}

即可

謝謝你 我終於可以了

/images/emoticon/emoticon41.gif

我要發表回答

立即登入回答