Q1 CSD
Q1 CSD
int MaxAgeN(int n) {
Node p = head;
int max = -1;
if (n == 1) {
max = p.info.type;
while (p.next != null) {
if (p.next.info.type > max) {
max = p.next.info.type;
}
p = p.next;
}
}
else {
p = head;
int maxN = MaxAgeN(n-1);
while(p!=null) {
if (p.info.type<maxN) {
max = p.info.type;
}
p=p.next;
}
p=head;
while (p.next != null) {
if (p.next.info.type > max && p.next.info.type<maxN) {
max = p.next.info.type;
}
p = p.next;
}
}
return max;
}
void max1(){
int max = MaxAgeN(2);//edit here
Node p = head;
while(p!= null){
if(max == p.info.type){
p.info.place ="YY"; //edit here
break;
}
p = p.next;
}
}
---------------------------------
----------Q1
void addLast(Castor x) {//You should write here appropriate statements to
complete this function.
Node q = new Node(x);
if (isEmpty()) {
head = tail = q;
} else {
tail.next = q;
tail = q;
}
}
---------------------
void addFirst(Castor x){
head = new Node(x,head);
if(tail==null) tail=head;
}
//---------------
--them phan tu dau tien
void addFirst(Castor x) {
Node p = new Node(x);
if (isEmpty()) {
head = tail = p;
return;
}
p.next = head;
head = p;
}
---------------------
void addAfter(Node p, Castor x) {
Node p1 = new Node(x);
if (isEmpty()) {
return;
}
p1.next = p.next;
p.next = p1;
if (p == tail) {
tail = p1;
}
}
------------------
void insert(Castor x, int index) {
int count = 0;
Node p = head;
while (p.next != null) {
if (index == 0) {
this.addFirst(x);
break;
}
if (count == index - 1) {
this.addAfter(p, x);
break;
}
count++;
p = p.next;
}
}
------------------
int max()
{Node p=head;
int max=head.info.depth;
while(p!=null){
if(max<p.info.depth) max=p.info.depth;
p=p.next;
}
return max;
}
-----------------------------
//sort
---------------------------------
void dele(Node q) {
Node f, p;
f = null;
p = head;
while (p != null) {
if (p == q) {
break;
}
f = p;
p = p.next;
}
if (p == null) {
return;//q is not found
}
if (f == null) {
head = head.next;
if (head == null) {
tail = null;
}
return;
}
f.next = p.next;
if (f.next == null) {
tail = f;
}
}
---------------------
// sort full day
void sortByPrice() {
Node pi, pj;
pi = head;
while (pi != null) {
pj = pi.next;
while (pj != null) {
if (pi.info.price > pj.info.price) {
Car temp = pi.info;
pi.info = pj.info;
pj.info = temp;
}
pj = pj.next;
}
pi = pi.next;
}
}
//xoa tu duoi len theo gia tri va so luong xoa
int size() {
int size = 0;
Node p = head;
while (p.next != null) {
size++;
p = p.next;
}
return size;
}
void dele(Node q) {
Node f, p;
f = null;
p = head;
while (p != null) {
if (p == q) {
break;
}
f = p;
p = p.next;
}
if (p == null) {
return;//q is not found
}
if (f == null) {
head = head.next;
if (head == null) {
tail = null;
}
return;
}
f.next = p.next;
if (f.next == null) {
tail = f;
}
}
-----------------------------//repalce
-- doi gia tri tai vi tri do
void Replace() {
int count = 0;
Node p = head;
while (p != null) {
if (p.info.wing < 6) {
count++;
if (count == 2) {
p.info.rate = 99;
}
}
p = p.next;
}
}
--------------------//get tail (index last) lay ra vi tri cuoi cung
int indexLast() {
int index1 = 0;
Node p = head;
while (p.next != null) {
index1++;
p = p.next;
}
return index1;
}
----------------------------//lay ra index Max
int MaxAgeN() {
Node p = head;
int index = 0;
return index;
}