0% found this document useful (0 votes)
3 views3 pages

Untitled LL

The document contains Java methods for manipulating a linked list, including adding a node at a specific position, removing a node from a position or the last node, swapping nodes at given positions, sorting nodes based on specific criteria, and sorting the entire list. Each method includes checks for invalid positions and handles node connections appropriately. The linked list operations are implemented using a Node class that holds information and a reference to the next node.

Uploaded by

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

Untitled LL

The document contains Java methods for manipulating a linked list, including adding a node at a specific position, removing a node from a position or the last node, swapping nodes at given positions, sorting nodes based on specific criteria, and sorting the entire list. Each method includes checks for invalid positions and handles node connections appropriately. The linked list operations are implemented using a Node class that holds information and a reference to the next node.

Uploaded by

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

void addPos(int pos, Node x){

Node p = head;
while(pos-- > 1){
if (p.next == null){
System.out.println("position invalid");
return;
}
p = p.next;
}
Node node = new Node(x.info,p.next);
p.next = node;
void RemovePos(int pos){
if (pos == 1){
head = head.next;
return;
}
Node p = head;
while(pos-- > 2){
if (p.next == null){
System.out.println("position invalid");//dont need
return;
}
p = p.next;
}
p.next = p.next.next;

void RemoveLast(){
Node p = head;
while(p.next != tail){
p = p.next;
}
p.next = null;

void Swap(int xPos, int yPos){


Node x = head;
while(xPos-- > 1){
if (x.next == null){
System.out.println("position invalid");
return;
}
x = x.next;
}
Node y = head;
while(yPos-- > 1){
if (y.next == null){
System.out.println("position invalid");
return;
}
y = y.next;
}
Node a = new Node(x.info,x.next);
x.info = y.info;
y.info = a.info;

link list
void Sort(int pos,int quantity){
Node p=head ;
while(pos-- >1){
p=p.next;
}
while(quantity >1){
Node temp =p;
for (int i=0;i <quantity-1;i++){
temp=temp.next;
if(p.info.type>temp.info.type){
Node a =new Node(p.info,p.next);
p.info =temp.info;
temp.info=a.info;
}

}
p=p.next;
quantity--;
}
}
void sortFull() {
Node i =head ;
Node j=null ;
Watermelon tmp ;
while(j !=null){
j=i.next ;
while (j!=null){
if (i.info.price > j.info.price){
tmp =i.info;
i.info=j.info ;
j.info=tmp ;
}
j=j.next;
}
i=i.next;
}
}

You might also like