You are on page 1of 5

void addLast(String xSource, int xPrice, int xType) {

//You should write here appropriate statements to complete this function.


Node q = new Node(new Watermelon(xSource, xPrice, xType));
if (xSource.charAt(0) == 'D') {
return;
}
if (isEmpty()) {
head = tail = q;
} else {
tail.next = q;
tail = q;
}
//----------------------------------------------------------------------
}

void addFirst(String xSource, int xPrice, int xType) {


//You should write here appropriate statements to complete this function.
Node q = new Node(new Watermelon(xSource, xPrice, xType));
if (isEmpty()) {
head = tail = q;
} else {
q.next = head;
head = q;
}
//----------------------------------------------------------------------
}

Node takeNodeAtPosition(int pos) {


if (isEmpty()) {
return null;
}
int count = 0;
Node q = head;
while (q != null && count < pos - 1) {
q = q.next;
count++;
}
return q;
}

public void insert(Node y, int k) {


//You should write here appropriate statements to complete this function.
if (isEmpty()) {
return;
}
if (k < 0) {
return;
}
if (k == 0) {
y.next = head;
head = y;
} else {
if (takeNodeAtPosition(k) == null) {
return;
} else {
if (takeNodeAtPosition(k).next == null) {
tail.next = y;
tail = y;
} else {
y.next = takeNodeAtPosition(k).next;
takeNodeAtPosition(k).next = y;
}
}
}

//----------------------------------------------------------------------
}

public void deleteFirst() {


//You should write here appropriate statements to complete this function.
if (isEmpty()) {
return;
} else {
head = head.next;
}
//----------------------------------------------------------------------
}

public void deleteLast() {


//You should write here appropriate statements to complete this function.
if (isEmpty()) {
return;
}
Node cur = head;
while (cur.next != tail) {
cur = cur.next;
}
cur.next = null;
tail = cur;
//----------------------------------------------------------------------
}

public void removeNodeAtPosition(int position) {


//You should write here appropriate statements to complete this function.
if (isEmpty()) {
return;
}
if (head == tail) {
head = tail = null;
}
if (position == 0) {
head = head.next;
}
if (takeNodeAtPosition(position) == tail) {
deleteLast();
}
if (takeNodeAtPosition(position) == null) {
return;
} else {
takeNodeAtPosition(position).next =
takeNodeAtPosition(position).next.next;
}

//----------------------------------------------------------------------
}

int getSize() {
if (isEmpty()) {
return 0;
}
Node q = head;
int count = 0;
while (q != null) {
q = q.next;
count++;
}
return count;
}

void swap(Node q, Node p) {


if (isEmpty()) {
return;
}
Bike tmp = q.info;
q.info = p.info;
p.info = tmp;

void sortAsc() {
if (isEmpty()) {
return;
}
for (int i = 0; i <= getSize(); i++) {
Node q = takeNodeAtPosition(i);
for (int j = i + 1; j <= getSize(); j++) {
Node p = takeNodeAtPosition(j);
if (q.info.color > p.info.color) {
Bike temp = q.info;
q.info = p.info;
p.info = temp;
}
}
}
}

void sortDesc() {
if (isEmpty()) {
return;
}
for (int i = 0; i <= getSize(); i++) {
Node q = takeNodeAtPosition(i);
for (int j = i + 1; j <= getSize(); j++) {
Node p = takeNodeAtPosition(j);
if (q.info.color < p.info.color) {
Bike temp = q.info;
q.info = p.info;
p.info = temp;
}
}
}
}

Node NodeMax() {
if (isEmpty()) {
return null;
}
Node q = head;
Node max = head;
while (q != null) {
if (q.info.color > max.info.color) {
max = q;
}
q = q.next;
}
return max;
}

Node NodeNearMax(Node max) {


if (isEmpty()) {
return null;
}
Node current = head;
Node nearMax = head;
// Tìm node có giá trị thấp hơn node có giá trị lớn nhất
while (current != null) {
if (current.info.color < max.info.color && current.info.color >
nearMax.info.color) {
nearMax = current;
}
current = current.next;
}
return nearMax;
}

Node findFirstMax(Node max) {


if (head == null && tail == null) {
return null;
}
Node q = head;
while (q != null) {
if (q.getInfo().area == max.getInfo().area) {
return q;
}
q = q.next;
}
return null;
}

Node deleteNode(Node nearMax) {


if (isEmpty()) {
return null;
}
Node tmp = head;
while (tmp != null) {
if (tmp.next == nearMax) {
tmp.next = tmp.next.next;
}
tmp = tmp.next;
}
return nearMax;
}

Node findMinNode() {
if (isEmpty()) {
return null;
}
Node min = head;
Node q = head;
while (q != null) {
if (min.info.color > q.info.color) {
min = q;
}
q = q.next;
}
return min;
}

Node findsecondMinNode(Node secondMin) {


if (isEmpty()) {
return null;
}
Node q = head;
while (q != null) {
if (secondMin.info.color == q.info.color && q != secondMin) {
secondMin = q;
}
q = q.next;
}
return secondMin;
}

void deleteNode(Node secondMin) {


if (isEmpty()) {
return;
}
Node tmp = head;
while (tmp != null) {
if (tmp.next == secondMin) {
tmp.next = tmp.next.next;
}
tmp = tmp.next;
}
}

You might also like