0% found this document useful (0 votes)
10 views

Linkedlist Java

Uploaded by

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

Linkedlist Java

Uploaded by

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

public class LinkedList {

Node head;

LinkedList() {
head = null;
}

class Node {
int data;
Node next;

Node(int val) {
data = val;
next = null;
}
}

public void insertAtBeginning(int val) {


Node node = new Node(val);

if (head != null)
node.next = head;

head = node;

public void displayList() {


Node temp = head;

while (temp != null) {


System.out.print(temp.data + " ");
temp = temp.next;
}
}

public void insertAtPosition(int pos, int val) {


if (pos == 0) {
insertAtBeginning(val);
return;
}

Node node = new Node(val);

Node temp = head;


for (int i = 1; i < pos; i++) {
temp = temp.next;
if (temp == null) {
throw new IllegalArgumentException("Invalid Position");
}

node.next = temp.next;
temp.next = node;
}
public void deleteAtPosition(int pos) {
if (head == null)
throw new IndexOutOfBoundsException("List is Empty");

if (pos == 0) {
head = head.next;
return;
}

Node temp = head;


Node prev = null;
for (int i = 1; i <= pos; i++) {
prev = temp;
temp = temp.next;
}

prev.next = temp.next;
}

public void getAtPosition(int pos) {


Node temp = head;

for (int i = 0; i < pos; i++) {


temp = temp.next;
}

System.out.println(temp.data);

public void updateAtPosition(int pos, int val) {


Node temp = head;
Node prev = head;
Node node = new Node(val);
for (int i = 0; i < pos; i++) {
prev = temp;
temp = temp.next;
}

System.out.println(temp.data);
node.next = temp.next;
temp = node;
prev.next = temp;
}

}
public class LinkedList<T> {
Node head;

LinkedList() {
head = null;
}

class Node {
T data;
Node next;

Node(T val) {
data = val;
next = null;
}
}

public void insertAtBeginning( T val) {


Node node = new Node(val);

if (head != null)
node.next = head;

head = node;

public void displayList() {


Node temp = head;

while (temp != null) {


System.out.print(temp.data + " ");
temp = temp.next;
}
}

public void insertAtPosition(int pos, T val) {


if (pos == 0) {
insertAtBeginning(val);
return;
}

Node node = new Node(val);

Node temp = head;


for (int i = 1; i < pos; i++) {
temp = temp.next;
if (temp == null) {
throw new IllegalArgumentException("Invalid Position");
}

node.next = temp.next;
temp.next = node;
}

public void deleteAtPosition(int pos) {


if (head == null)
throw new IndexOutOfBoundsException("List is Empty");
if (pos == 0) {
head = head.next;
return;
}

Node temp = head;


Node prev = null;
for (int i = 1; i <= pos; i++) {
prev = temp;
temp = temp.next;
}

prev.next = temp.next;
}

public void getAtPosition(int pos) {


Node temp = head;

for (int i = 0; i < pos; i++) {


temp = temp.next;
}

System.out.println(temp.data);

public void updateAtPosition(int pos, T val) {


Node temp = head;
Node prev = head;
Node node = new Node(val);
for (int i = 0; i < pos; i++) {
prev = temp;
temp = temp.next;
}

System.out.println(temp.data);
node.next = temp.next;
temp = node;
prev.next = temp;
}

}
import java.util.Iterator;

public class LinkedList<T> implements Iterable<T> {


Node head;

LinkedList() {
head = null;
}

class Node {
T data;
Node next;

Node(T val) {
data = val;
next = null;
}
}

public void insertAtBeginning(T val) {


Node node = new Node(val);

if (head != null)
node.next = head;

head = node;

public void displayList() {


Node temp = head;

while (temp != null) {


System.out.print(temp.data + " ");
temp = temp.next;
}
}

public void insertAtPosition(int pos, T val) {


if (pos == 0) {
insertAtBeginning(val);
return;
}

Node node = new Node(val);

Node temp = head;


for (int i = 1; i < pos; i++) {
temp = temp.next;
if (temp == null) {
throw new IllegalArgumentException("Invalid Position");
}

node.next = temp.next;
temp.next = node;
}

public void deleteAtPosition(int pos) {


if (head == null)
throw new IndexOutOfBoundsException("List is Empty");
if (pos == 0) {
head = head.next;
return;
}

Node temp = head;


Node prev = null;
for (int i = 1; i <= pos; i++) {
prev = temp;
temp = temp.next;
}

prev.next = temp.next;
}

public void getAtPosition(int pos) {


Node temp = head;

for (int i = 0; i < pos; i++) {


temp = temp.next;
}

System.out.println(temp.data);

public void updateAtPosition(int pos, T val) {


Node temp = head;
Node prev = head;
Node node = new Node(val);
for (int i = 0; i < pos; i++) {
prev = temp;
temp = temp.next;
}

System.out.println(temp.data);
node.next = temp.next;
temp = node;
prev.next = temp;
}

@Override
public Iterator<T> iterator() {

return new Iterator<T>() {


Node temp = head;

@Override
public boolean hasNext() {

return temp!=null;
}

@Override
public T next() {
T val = temp.data;
temp = temp.next;
return val;
}
};
}

}
import java.util.Iterator;

public class MainClass {


public static void main(String[] args) {
LinkedList<Integer> list=new LinkedList<Integer>();

list.insertAtBeginning(5);
list.insertAtBeginning(9);
list.insertAtBeginning(20);
list.insertAtBeginning(6);

list.insertAtPosition(2,11);
list.displayList();
System.out.println();

// list.deleteAtPosition(0);
// list.displayList();

// list.getAtPosition(3);

// list.updateAtPosition(4,33);
// list.displayList();

Iterator<Integer> li=list.iterator();
while(li.hasNext()) {
System.out.print(li.next());
}
for(int val:list) {
System.out.println(val);
}

}
}

You might also like