Q1 CSD
Q1 CSD
void addFirst(Book b) {
Node newNode = new Node(b);
if (isEmpty()) {
head = tail = newNode;
return;
}
newNode.next = head;
head = newNode;
}
int count = 0;
while (temp != null) {
if (index - count == 1) {
newNode.next = temp.next;
temp.next = newNode;
if (temp == tail) {
tail = newNode;
}
return;
}
count++;
temp = temp.next;
}
}
---------------------------------
//replace thang lon thu n
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;
}
}
---------------------------------
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;
}
//---------------
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;
}
}
---------------------
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;
}
}
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;
}
}
-----------------------------//replace
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)
int indexLast() {
int index1 = 0;
Node p = head;
while (p.next != null) {
index1++;
p = p.next;
}
return index1;
}
----------------------------
int MaxAgeN() {
Node p = head;
int index = 0;
return index;
}
----------------------------
//xoa tai vi tri thu may
void dele(int position) {
if (position <= 0) {
return;
}
if (position == 1) {
if (head == null) {
return;
}
head = head.next;
if (head == null) {
tail = null;
}
return;
}
int count = 1;
Node p = head;
Node prev = null;
while (p != null && count < position) {
prev = p;
p = p.next;
count++;
}
if (p == null) {
return;
}
prev.next = p.next;
if (prev.next == null) {
tail = prev;
}
}
----------------------------
//reverse 4 phan tu cuoi
void reverseLast4Elements() {
int length = 0;
Node current = head;
while (current != null) {
length++;
current = current.next;
}
int startIndex = length - 4;
if (startIndex < 0) {
// Not enough elements to reverse
return;
}
current = head;
for (int i = 0; i < startIndex; i++) {
current = current.next;
}
// Update pointers
if (startIndex == 0) {
head = prev;
} else {
Node prevStart = head;
for (int i = 0; i < startIndex - 1; i++) {
prevStart = prevStart.next;
}
prevStart.next = prev;
}
}
----------------------------
//reverse 4 phan tu dau
void reverseFirst4Elements() {
if (head == null) {
return;
}
head = prev;
}