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

Linked List

Uploaded by

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

Linked List

Uploaded by

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

import java.util.

Scanner;

public class LinkedList {

Node head;

static class Node {

int data;

Node next;

Node(int d) {

data = d;

next = null;

public static LinkedList insert_end(LinkedList list, int data) {

Node nn = new Node(data);

nn.next = null;

if (list.head == null) {

list.head = nn;

} else {

Node p = list.head;

while (p.next != null) {

p = p.next;

p.next = nn;

return list;

public static LinkedList insert_beg(LinkedList list, int data) {

Node nn = new Node(data);


nn.next = null;

if (list.head == null) {

list.head = nn;

} else {

nn.next = list.head;

list.head = nn;

return list;

public static LinkedList insert_mid(LinkedList list, int data, int pos) {

Node nn = new Node(data);

nn.next = null;

if (list.head == null) {

list.head = nn;

} else {

Node p = list.head;

int count = 0;

while (count < pos - 1 && p.next != null) {

p = p.next;

count++;

nn.next = p.next;

p.next = nn;

return list;

public static void printList(LinkedList list) {

Node p = list.head;

while (p != null) {
System.out.print(p.data + "->");

p = p.next;

System.out.println("null");

public static LinkedList Delete_end(LinkedList list){

Node p=list.head;

if(list.head==null){

System.out.println("List Is Empty");

else if(list.head.next==null){

list.head=null;

else{

while(p.next.next!=null){

p=p.next;

p.next=null;

return list;

public static LinkedList Delete_beg(LinkedList list){

Node p=list.head;

if(list.head==null){

System.out.println("List Is Empty");

else if(list.head.next==null){

list.head=null;

else{

p=list.head.next;
list.head.next=null;

list.head=p;

return list;

public static LinkedList Delete_mid(LinkedList list,int pos){

Node p=list.head;

Node pprev=p;

if(list.head==null){

System.out.println("List Is Empty");

else{

int count=0;

while(count==pos-1 && p!=null){

p=p.next;

pprev=p;

count++;

pprev=p.next.next;

p.next=pprev;

return list;

public static void Search(LinkedList list,int key){

Node p=list.head;

boolean found=false;

if(list.head==null){

System.out.println("List Is Empty");

else{

for(int i=0;p!=null;i++){
if(p.data==key){

System.out.println("The key is found at "+i);

found=true;

break;

p=p.next;

if(found==false){

System.out.println("The key is not found at list");

public static void max_min(LinkedList list){

Node p=list.head;

int max=Integer.MIN_VALUE;

int min=Integer.MAX_VALUE;

while(p!=null){

max=Math.max(p.data,max);

min=Math.min(p.data,min);

p=p.next;

System.out.println("Max:"+max+" "+"Min:"+min);

public static int sum(LinkedList list){

Node p=list.head;

int sum=0;

while(p!=null){

sum+=p.data;
p=p.next;

return sum;

public static Node reverse(Node head) {

Node p = head;

Node prev = null;

while (p != null) {

Node front = p.next;

p.next = prev;

prev = p;

p = front;

return prev;

static void swap(Node p1,Node p2){

int temp=p1.data;

p1.data=p2.data;

p2.data=temp;

public static void sort(Node head){

if(head==null){

return;

boolean swapped;

Node p;

do{

swapped=false;

p=head;
while(p.next!=null){

if(p.data >p.next.data){

swap(p,p.next);

swapped=true;

p=p.next;

}while(swapped);

public static void main(String[] args) {

Scanner get = new Scanner(System.in);

LinkedList list = new LinkedList();

while (true) {

System.out.println("Linked List");

System.out.println("Enter the choice:");

System.out.println("1. InsertAtEnd");

System.out.println("2. InsertAtBeg");

System.out.println("3. InsertAtMiddle");

System.out.println("4. PrintList");

System.out.println("5. DeleteAtEnd");

System.out.println("6. DeleteAtBeg");

System.out.println("7. DeleteAtMid");

System.out.println("8. Search");

System.out.println("9. Max and Min");

System.out.println("10.Sum of elements");

System.out.println("11.Reverse");

System.out.println("12. Sort");
System.out.println("13. Exit");

int choice = get.nextInt();

switch (choice) {

case 1:

System.out.println("Enter the Value:");

int valEnd = get.nextInt();

insert_end(list, valEnd);

break;

case 2:

System.out.println("Enter the Value:");

int valBeg = get.nextInt();

insert_beg(list, valBeg);

break;

case 3:

System.out.println("Enter the Value and Position:");

int valMid = get.nextInt();

int pos = get.nextInt();

insert_mid(list, valMid, pos);

break;

case 4:

printList(list);

break;

case 5:

Delete_end(list);

break;

case 6:

Delete_beg(list);

break;

case 7:

System.out.println("Enter the posistion of node to be deleted");


pos=get.nextInt();

Delete_mid(list,pos);

break;

case 8:

System.out.println("Enter the element to be searched");

int key=get.nextInt();

Search(list,key);

break;

case 9:

max_min(list);

break;

case 10:

int total=sum(list);

System.out.println("Total:"+total);

break;

case 11:

list.head=reverse(list.head);

break;

case 12:

sort(list.head);

break;

case 13:

System.out.println("Exiting...");

get.close();

return;

default:

System.out.println("Invalid choice. Try again.");

}
Output:
Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Enter the Value:

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min


10.Sum of elements

11.Reverse

12. Sort

13. Exit

Enter the Value:

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Enter the Value:

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList
5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

2->6->4->null

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

12

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg
3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

2->4->6->null

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Enter the Value:

3
Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

3->2->4->6->null

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort
13. Exit

Enter the Value and Position:

43

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

3->2->4->4->6->null

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search
9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid
8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Enter the Value and Position:

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Enter the posistion of node to be deleted

Linked List

Enter the choice:

1. InsertAtEnd
2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

2->4->3->null

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Enter the element to be searched


4

The key is found at 1

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

Max:4 Min:2

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements
11.Reverse

12. Sort

13. Exit

10

Total:9

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

11

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search
9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

3->4->2->null

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

12

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg
7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

2->3->4->null

Linked List

Enter the choice:

1. InsertAtEnd

2. InsertAtBeg

3. InsertAtMiddle

4. PrintList

5. DeleteAtEnd

6. DeleteAtBeg

7. DeleteAtMid

8. Search

9. Max and Min

10.Sum of elements

11.Reverse

12. Sort

13. Exit

13

Exiting...

You might also like