Q1_CSD
Q1_CSD
|| ||______//add first
|| ||______//add many
|| ||______//add if age > 4
|| ||______//add element have max age
|| ||______//add a node with value x after the node p
|| ||______//add a node after a node require
||
||___INSERT____//insert index k
|| ||____//insert after index k
|| ||____//insert before index k
|| ||____//insert x to sorted link list
||
||___SEARCH____//search node with Object
|| ||____//search node with string
|| ||____//search node with integer/double
|| ||____//search node with index
|| ||____//search node after index
|| ||____//search node before index
|| ||____//search first node have integer/double/string(VD:first node have
age=5)
|| ||____//search second node have integer/double/string(VD:second node have
age=5)
||
||___DELETE____//delete node
|| ||____//delete all
|| ||____//delete 2 node last have age > 5
|| ||____// Delete from head
|| ||____// Delete from tail
|| ||____// Delete after a given node p
|| ||____// Delete with price
||___SORT______
|| ||______//sort by for assending (integer)
|| ||______//sort by for assending (string)
|| ||______//sort 3 first element assending (integer)
|| ||______//sort 3 last element assending (integer)
|| ||______//sort between index 2 to 6 element assending (integer)
|| ||______//check sorted accending?
|| ||______//check sorted x elementFirst
|| ||______//check sorted x elementLast
||
||___SWAP______//swap min max
|| ||______//swap node max second with node min first
|| ||______//swap lon thu n
||
||___GET_______//get index at Node
|| ||_______//get Object max
|| ||_______//get Object max second
|| ||_______//get size of list
||
||___ORTHER____//traverse
|| ||____//replace a node
|| ||____//count number of node
|| ||____//reverse list
|| ||____//append another list
|| ||____//change name first
|| ||____//convert to array[]
|| ||____//Check 2 link list equal
//add last
public void addLast(Person x){
Node q = new Node(x);
if(isEmpty()){head=tail=q;return;}
tail.next=q;tail=q;
}
//add first
public void addFirst(Person x){
Node p = new Node(x);
if(isEmpty()) {head = tail = p;}
else {p.next = head; head = p;}
}
//add many
public void addMany(String [] a, int [] b){
int n,i; n=a.length;
for(i=0;i<n;i++) addLast(new Person(a[i],b[i]));
}
//insert index k
public void insertAtIndexK( Ball c,int k) {
//Index = POSITION - 1
Node newNode = new Node(c);
if(k==size()+1){
return;
}
if (k == 0) {
newNode.next = head;
head = newNode;
return;
}
int count = 0;
Node p = head;
if (count == k - 1) {
newNode.next = p.next;
p.next = newNode;
} else {
return;
}
}
int count = 0;
Node p = head;
if (count == k) {
newNode.next = p.next;
p.next = newNode;
} else {
return;
}
}
if (k == 0) {
newNode.next = head;
head = newNode;
return;
}
int count = 0;
Node p = head;
Node previous = null;
if (count == k) {
previous.next = newNode;
newNode.next = p;
} else {
return;
}
}
Node p = head;
int count = 0;
while (p != null) {
if (count == index) {
// Tìm thấy nút ở vị trí index
return p;
}
p = p.next;
count++;
}
// Nếu vị trí index vượt quá độ dài của danh sách, trả về null
return null;
}
Node p = head;
int count = 0;
while (p != null) {
if (count == k) {
return p.next;
}
p = p.next;
count++;
}
return null;
}
return null;
}
Node p = head;
int count = 0;
while (p != null) {
if (count == k - 1) {
// Tìm thấy nút ở vị trí k - 1
return p;
}
p = p.next;
count++;
}
return null;
}
while (p != null) {
if (p.info.age == 5) {
return p;
}
p = p.next;
}
return null;
}
//delete node
public 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;
if(f==null){
head=head.next;
if(head==null) tail=null;
return;
}
f.next=p.next;
if(f.next==null) tail=f;
}
//delete all
public void deleAll(int xAge){
Node q;
while(true){
q = searchByAge(xAge);
if(q==null) break;
dele(q);
}
}
int index = 0;
Node p = head;
while (p != null) {
if (p == nodeToFind) {
return index;
}
p = p.next;
index++;
}
// Nếu không tìm thấy nút trong danh sách, trả về -1 hoặc giá trị khác thích
hợp
return -1;
}
//traverse
public void traverse(){
MyList h=new MyList();
Node p=head;Person x;
while(p!=null)
{x=p.info;h.addFirst(x);
p=p.next;
}
head=h.head;tail=h.tail;
}
//replace a node
public void replace{
Node p = head;
while(p != null){
if(p.info.name.equals("xName")){// xName was given
break;
}
p = p.next;
}
if(p != null){
p.info.name = yName;// yName was given
}
}
//reverse list
public void reverse() {
int n = size(), i, j;
for (i = 0, j = n - 1; i < j; i++, j--) {
Node pi = getNode(i);// create a node = getnode index i
Node pj = getNode(j);// create a node = getnode index j
Person temp = pi.info;// Note: change value of node, not change node
pi.info = pj.info;
pj.info = temp;
}
}
//convert to array[]
Person[] toArray() {
int n = size();
Person[] arr = new Person[n];
Node p = head;
int i = 0;
while (p != null) {
arr[i++] = p.info;
p = p.next;
}
return arr;
}