Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

UNIVERSITETI POLITEKNIK I TIRANES

FAKULTETI I TEKNOLOGJISE DHE INFORMACIONIT


DEPARTAMENTI I INXHINIERISË INFORMATIKE

Punë Laboratori nr. 1

Lenda:Strukture te dhenash
Grupi:2-C
Viti akademik:2023-2024

Punoi:Mite Hajdari
Pranoi:Msc.Alba Haveriku
package lab1;

public class lab1 {

public static void main(String[] args) {


// TODO Auto-generated method stub
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();

list1.insert(1);
list1.insert(2);
list1.insert(6);
list1.insert(3);
list1.insert(5);
list2.insert(7);
list2.insert(8);
list2.insert(4);

list1.alternimi(list1, list2);

System.out.println("Lista pas bashkimit


alternativ:");
list1.show();
}
}

class LinkedList {
Node head;

public void insert(int element) {


Node node = new Node(element);
if (head == null) {
head = node;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = node;
}
}

public void show() {


Node current = head;
while (current != null) {
System.out.println(current.element);
current = current.next;
}
}

public void alternimi(LinkedList l1,


LinkedList l2) {
Node n1 = l1.head;
Node n2 = l2.head;
Node temp1, temp2;

while (n1 != null && n2 != null) {


temp1 = n1.next;
temp2 = n2.next;

n1.next = n2;
if (temp1 == null) {
break;
}
n2.next = temp1;

n1 = temp1;
n2 = temp2;
}
}
}

class Node {
int element;
Node next;

public Node(int element) {


this.element = element;
this.next = null;
}

Rezultati:

You might also like