Tìm số lần xuất hiện của phần tử trong linked list

Xin chào mọi người ạ, em đang học về phần này, nhưng thường hay gặp lỗi cú pháp này nọ.
Phần em code là hàm count, mọi người xem giúp em xem phần đấy em thực hiện sai chỗ nào và chỉnh sửa giúp em ạ. Em cảm ơn nhiều lắm.

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
class SinglyLinkedListNode {
public:
	int data;
	SinglyLinkedListNode *next;

	SinglyLinkedListNode(int node_data) {
		this->data = node_data;
		this->next = nullptr;
	}
};

class SinglyLinkedList {
public:
	SinglyLinkedListNode *head;
	SinglyLinkedListNode *tail;

	SinglyLinkedList() {
		this->head = nullptr;
		this->tail = nullptr;
	}

	void insert_node(int node_data) {
		SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);

		if (!this->head) {
			this->head = node;
		}
		else {
			this->tail->next = node;
		}

		this->tail = node;
	}
};

void print_singly_linked_list(SinglyLinkedListNode* node, string sep) {
	while (node) {
		cout << node->data;
		node = node->next;
		if (node) {
			cout << sep;
		}
	}
}

void free_singly_linked_list(SinglyLinkedListNode* node) {
	while (node) {
		SinglyLinkedListNode* temp = node;
		node = node->next;

		free(temp);
	}
}

int count(SinglyLinkedList* root, int search_for)
{
	SinglyLinkedListNode *temp = root->head;
	if (temp->data == NULL) return 0;
	else if (temp->data == search_for)
	{
		root->head = root->head->next;
		delete[] temp;
		return count(root, search_for) + 1;
	}
	else
	{
		root->head = root->head->next;
		delete[] temp;
		return count(root, search_for);
	}

}


int main()
{
	SinglyLinkedList *llist = NULL;
	llist->insert_node(1);
	llist->insert_node(1);
	llist->insert_node(3);
	llist->insert_node(1);
	llist->insert_node(5);
	cout << count(llist, 1);
	return 0;
}

Đang NULL, tức là chưa có danh sách nào cả :slight_smile:

4 Likes

new free delete bạn dùng loạn xạ quá nên mình cũng bó tay

int main()
{
	SinglyLinkedList *llist = NULL;**
	SinglyLinkedList *llist = new SinglyLinkedList();

trong hàm count của bạn thay đổi giá trị của head rồi thì kiểu gì dùng xong hàm count list của bạn cũng NULL, mình chả biết sửa kiểu gì

if (temp->data == NULL) return 0;

cái này cũng sai nè, nếu temp == NULL nó vẫn cố tìm temp->data

...
int count(SinglyLinkedList* root, int search_for)
{
	SinglyLinkedListNode *temp = root->head;
	int result = 0;
	while(temp){
	    if(temp->data == search_for)result +=1;
	    temp = temp->next;
	}
	return result;
}
3 Likes

Em cảm ơn 2 anh đã chỉ cho em ạ.

Em ko đặt tên biến theo qui tắc chung nên loạn giữa pointer và non-pointer. temp->data == NULL là sai, vì data là int. Thường anh sẽ đặt thêm chữ p để phân biệt pointer và non-pointer.
Ví dụ của em: SingleLinkedListNode* pNext;
Với cái dòng đầu tiên trong int main() phải là : SinglyLinkedList* pList = new SinglyLinkedList(); để khởi tạo linked list của em
Tặng em thêm trang này để đọc thêm:
https://google.github.io/styleguide/cppguide.html

4 Likes

Em rất vui vì mọi người đều tốt bụng, nhẹ nhàng và có tâm, em đã học được nhiều điều từ sự tốt bụng của mọi người mặc dù em toàn hỏi những cái ngớ ngẩn :)))))

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?