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;
}