0% found this document useful (0 votes)
14 views15 pages

CTDL 3

Uploaded by

reprep063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

CTDL 3

Uploaded by

reprep063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Bài tập Nâng Cao

#ifndef ELEMENT_H

#define ELEMENT_H

#include<bits/stdc++.h>

#pragma once

using namespace std;

class element

private:

int data;

element *pointer;

public:

element();

element(int);

virtual ~element();

int Getdata()

return data;

void Setdata(int val)

data = val;

element* Getpointer()

return pointer;

void Setpointer(element* val)

pointer = val;
}

protected:

};

#endif // ELEMENT_H

#include"element.h"

using namespace std;

element::element()

//ctor

this->data=0;

this->pointer= NULL;

element::element(int data)

//ctor

this->data=data;

this->pointer= NULL ;

element::~element()

//dtor

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

#include "element.h"

#pragma once

class linkedlist
{

private:

element* head;

element* tail;

int nNum;

public:

linkedlist();

virtual ~linkedlist();

element* Gethead()

return head;

void Sethead(element* val)

head = val;

element* Gettail()

return tail;

void Settail(element* val)

tail = val;

void InsertFirst(element*);

void InsertTail(element*);

bool DeleteFirst();

bool DeleteTail();

void Travel();

int SumList(); //tinh tong gia tri cac phan tu

int MaxList();// tim gia tri lon nhat


int CountPrime();//dem so nguyen to

bool InsertAfter(element*, int);//them phan tu moi sau phan tu chi dinh

bool DeleteElement(element*);

bool IsPrime(int num);

int CountEqualElements(int x);//dem so phan tu co gia tri trung

bool InsertUnique(int);//them phan tu vao danh sach neu khong co phan tu nao mang gia tri
giong nhau

bool RemoveAll();

protected:

};

#endif

#include "linkedlist.h"

#include <iostream>

using namespace std;

linkedlist::linkedlist()

//ctor

this->head=NULL;

this->tail=NULL;

this->nNum=0;

linkedlist::~linkedlist()

//dtor

void linkedlist::InsertFirst(element* e){

if(this->head==NULL)

this->head=this->tail=e;

else{
e->Setpointer(this->head); //step 1

this->head=e; // step 2

this->nNum++;

void linkedlist::InsertTail(element *e){

if(this->head==NULL)

this->head=this->tail=e;

else{

this->tail->Setpointer(e);// step 1

this->tail=e; // step 2

this->nNum++;

void linkedlist::Travel(){

element* p=this->head;

while(p!=NULL){

cout<<p->Getdata()<<"\t";

p=p->Getpointer();

bool linkedlist::DeleteFirst(){

if(this->head==NULL) return false;

else{

element* p=this->head;

this->head=this->head->Getpointer();

delete p;

return true;

}
bool linkedlist::DeleteTail() {

if (this->head == NULL) return false;

else {

element* current = this->head;

if (current->Getpointer() == NULL) { // Only one element

delete current;

this->head = this->tail = NULL;

} else {

while (current->Getpointer() != this->tail) {

current = current->Getpointer();

delete this->tail;

this->tail = current;

this->tail->Setpointer(NULL);

this->nNum--;

return true;

int linkedlist::SumList() {

int sum = 0;

element* p = this->head;

while (p != NULL) {

sum += p->Getdata();

p = p->Getpointer();

return sum;

int linkedlist::MaxList() {

if (this->head == NULL)

return 1; // Return minimum int value if list is empty


int max = this->head->Getdata();

element* p = this->head->Getpointer();

while (p != NULL) {

if (p->Getdata() > max) {

max = p->Getdata();

p = p->Getpointer();

return max;

bool isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return false;

return true;

int linkedlist::CountPrime() {

int count = 0;

element* p = this->head;

while (p != NULL) {

if (isPrime(p->Getdata())) {

count++;

p = p->Getpointer();

return count;

bool linkedlist::InsertAfter(element* p, int val) {

if (p == NULL) return false;

element* newElement = new element(val);


newElement->Setpointer(p->Getpointer());

p->Setpointer(newElement);

if (p == this->tail) {

this->tail = newElement;

this->nNum++;

return true;

bool linkedlist::DeleteElement(element* p) {

if (this->head == NULL || p == NULL) return false;

if (p == this->head) {

return this->DeleteFirst();

element* current = this->head;

while (current != NULL && current->Getpointer() != p) {

current = current->Getpointer();

if (current == NULL) return false;

current->Setpointer(p->Getpointer());

if (p == this->tail) {

this->tail = current;

delete p;

this->nNum--;

return true;

int linkedlist::CountEqualElements(int x) {

int count = 0;

element* p = this->head;

while (p != NULL) {
if (p->Getdata() == x) {

count++;

p = p->Getpointer();

return count;

bool linkedlist::InsertUnique(int val) {

element* p = this->head;

while (p != NULL) {

if (p->Getdata() == val) {

return false;

p = p->Getpointer();

this->InsertTail(new element(val));

return true;

bool linkedlist::RemoveAll()

while(this->head!=NULL)

linkedlist::DeleteFirst();

this->tail=NULL;

/*bool linkedlist::RemoveAll()

element *p=this->head;
element *q=this->tail;

if (p==NULL && q==NULL )

return true;

else

p->Getdata();

delete p;

}*/

#include <iostream>

#include "linkedlist.h"

#include"linkedlist.cpp"

#include"element.h"

#include"element.cpp"

using namespace std;

int main()

linkedlist *list_=new linkedlist();

element *e;

e=new element(9);

list_->InsertTail(e);

e=new element(10);

list_->InsertTail(e);

e=new element(8);

list_->InsertTail(e);

list_->Travel();

list_->DeleteFirst();

cout<<"\n";

list_->Travel();

list_->SumList();
cout<<list_->SumList();

cout<<endl;

list_->RemoveAll();

cout<<list_->RemoveAll()<<"\n Da xoa thanh cong!!"<<endl;

list_->Travel();

return 0;

Áp Dụng Bài 2:

#include <bits/stdc++.h>
#pragma once
using namespace std;

class Element {
private:
int data;
Element* pointer;
public:
Element();
Element(int data) ;
virtual ~Element() {}

int GetData() { return data; }


void SetData(int val) { data = val; }
Element* GetPointer() { return pointer; }
void SetPointer(Element* val) { pointer = val; }
};

class Book {
private:
string title;
string authors[5];
int authorCount;
string publisher;
int year;
Book* next;
public:
Book() {
// ctor
this->title = "";
this->authorCount = 0;
this->publisher = "";
this->year = 0;
this->next = NULL;
}

Book(string t, string a[], int ac, string p, int y) {


// ctor
this->title = t;
this->authorCount = ac;
this->publisher = p;
this->year = y;
this->next = NULL;
for (int i = 0; i < ac; ++i) {
this->authors[i] = a[i];
}
}

string GetTitle() { return title; }


string* GetAuthors() { return authors; }
int GetAuthorCount() { return authorCount; }
string GetPublisher() { return publisher; }
int GetYear() { return year; }
Book* GetNext() { return next; }
void SetNext(Book* nextBook) { next = nextBook; }
};
Element::Element()
{
//ctor
this->data=0;
this->pointer= NULL;
}
Element::Element(int data)
{
//ctor
this->data=data;
this->pointer= NULL ;
}
class Library {
private:
Book* head;
public:
Library() {
// ctor
this->head = NULL;
}
void AddBook(string title, string authors[], int authorCount, string publisher, int year) {
Book* newBook = new Book(title, authors, authorCount, publisher, year);
if (!head) {
head = newBook;
} else {
Book* current = head;
while (current->GetNext()) {
current = current->GetNext();
}
current->SetNext(newBook);

}
}

int CountBooksByAuthor(string author) {


int count = 0;
Book* current = head;
while (current) {
for (int i = 0; i < current->GetAuthorCount(); ++i) {
if (current->GetAuthors()[i] == author) {
count++;
break;
}
}
current = current->GetNext();
}
return count;
}

void ListBooksByPublisherAndYear(string publisher, int year) {


Book* current = head;
bool found = false;
while (current) {
if (current->GetPublisher() == publisher && current->GetYear() == year) {
cout << "Title: " << current->GetTitle() << endl;
found = true;
}
current = current->GetNext();
}
if (!found) {
cout << "No books found for publisher " << publisher << " in year " << year << "." << endl;
}
}
};

int main() {
Library library;
int n;
cout << "Nhap so luong sach: ";
cin >> n;
cin.ignore();

for (int i = 0; i < n; ++i) {


string title, publisher, author;
int year, authorCount;
string authors[5];

cout << "Nhap ten sach: ";


getline(cin, title);

cout << "Nhap so luong tac gia (toi da 5): ";


cin >> authorCount;
cin.ignore();
for (int j = 0; j < authorCount; ++j) {
cout << "Nhap ten tac gia " << j + 1 << ": ";
getline(cin, authors[j]);
}

cout << "Nhap nha xuat ban: ";


getline(cin, publisher);

cout << "Nhap nam xuat ban: ";


cin >> year;
cin.ignore();

library.AddBook(title, authors, authorCount, publisher, year);


}

string searchAuthor;
cout << "Nhap ten tac gia can tim: ";
getline(cin, searchAuthor);
int count = library.CountBooksByAuthor(searchAuthor);
cout << "So luong sach cua tac gia " << searchAuthor << ": " << count << endl;

string searchPublisher;
int searchYear;
cout << "Nhap nha xuat ban can tim: ";
getline(cin, searchPublisher);
cout << "Nhap nam xuat ban can tim: ";
cin >> searchYear;

cout << "Cac sach duoc xuat ban boi " << searchPublisher << " trong nam " << searchYear << ":" <<
endl;
library.ListBooksByPublisherAndYear(searchPublisher, searchYear);
return 0;
}

You might also like