Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views23 pages

Code

The document contains Java implementations for various linked list structures, including linear linked lists, circular linked lists, stacked linked lists, and queued linked lists. Each section provides a class definition with methods for common operations such as appending, inserting, deleting, and displaying elements. The main classes also include a user interface for interacting with the linked lists through a menu-driven approach.

Uploaded by

funin1419
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)
5 views23 pages

Code

The document contains Java implementations for various linked list structures, including linear linked lists, circular linked lists, stacked linked lists, and queued linked lists. Each section provides a class definition with methods for common operations such as appending, inserting, deleting, and displaying elements. The main classes also include a user interface for interacting with the linked lists through a menu-driven approach.

Uploaded by

funin1419
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/ 23

1.

​Linear linkedlist
import java.util.Scanner;

// Node class
class Node{
int data;
Node link;
}

// Linked List class


class LinkedList {
Node head,temp,prev;

// Append at end
public void append(int no) {
Node n = new Node();
n.data = no;

n.link = null;

if (head == null) {
head = n;
}
else {
temp=head;
while(temp.link!= null) {
temp=temp.link;
}
temp.link= n;
}
}

//add at he biginning
public void addatthebeginning(int no){

Node n = new Node();


n.data = no;

n.link = null;

if (head == null) {
head = n;
}

else{
n.link=head;
head=n;
}

//insert
public void insert (int no, int l){
int i;
temp=head;
Node n = new Node();
n.data = no;

n.link = null;

for(i=1;i<l;i++){
temp=temp.link;
}

n.link=temp.link;
temp.link=n;

// deleteatposition

public void deleteAtPosition(int val) {


Node temp = head;
Node prev = null;

if (head == null) {
System.out.println("Linked list is empty");
return;
}

while (temp != null) {


if (temp.data == val) {
if (temp == head) {
head = temp.link;
} else {
prev.link = temp.link;
}
System.out.println("Deleted " + val);
return;
} else {
prev = temp;
temp = temp.link;
}
}

System.out.println("Element " + val + " not found");


}
//display
public void display()
{
if (head == null)
{
System.out.println("Linked list is empty");
}
else {
temp = head;
System.out.println("Linked list contents:");
while (temp != null)
{
System.out.println(temp.data);
temp = temp.link;
}
}
}
}

// Main class
class linkedlinkedlist {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
Scanner sc = new Scanner(System.in);
int choice, num, loc, C;

do {
System.out.println("\n--- Linked List Menu ---");
System.out.println("press 1 to Append");
System.out.println("press 2 to Add at Beginning");
System.out.println("press 3 to Insert at Position");
System.out.println("press 4 to Delete at Position");
System.out.println("press 5 to Display");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter number to append: ");
num = sc.nextInt();
ll.append(num);
break;

case 2:
System.out.print("Enter number to add at the beginning: ");
num = sc.nextInt();
ll.addatthebeginning(num);
break;

case 3:
System.out.print("Enter number : ");
num = sc.nextInt();
System.out.print("Enter the location : ");
loc = sc.nextInt();
ll.insert(num,loc);
break;

case 4 :
System.out.print("Enter the number to be deleted: ");
num = sc.nextInt();
ll.deleteAtPosition(num);

break;

case 5:
ll.display();
break;
}
System.out.println("press 1 to continue and 0 to stop");
C=sc.nextInt();
}
while (C!=0);
}
}

Output
run:

--- Linked List Menu ---


press 1 to Append
press 2 to Add at Beginning
press 3 to Insert at Position
press 4 to Delete at Position
press 5 to Display
Enter your choice: 1
Enter number to append: 100
press 1 to continue and 0 to stop
1

--- Linked List Menu ---


press 1 to Append
press 2 to Add at Beginning
press 3 to Insert at Position
press 4 to Delete at Position
press 5 to Display
Enter your choice: 1
Enter number to append: 200
press 1 to continue and 0 to stop
1
--- Linked List Menu ---
press 1 to Append
press 2 to Add at Beginning
press 3 to Insert at Position
press 4 to Delete at Position
press 5 to Display
Enter your choice: 1
Enter number to append: 300
press 1 to continue and 0 to stop
1

--- Linked List Menu ---


press 1 to Append
press 2 to Add at Beginning
press 3 to Insert at Position
press 4 to Delete at Position
press 5 to Display
Enter your choice: 2
Enter number to add at the beginning: 500
press 1 to continue and 0 to stop
1

--- Linked List Menu ---


press 1 to Append
press 2 to Add at Beginning
press 3 to Insert at Position
press 4 to Delete at Position
press 5 to Display
Enter your choice: 3
Enter number : 600
Enter the location : 3
press 1 to continue and 0 to stop
1

--- Linked List Menu ---


press 1 to Append
press 2 to Add at Beginning
press 3 to Insert at Position
press 4 to Delete at Position
press 5 to Display
Enter your choice: 4
Enter the number to be deleted: 500
Deleted 500
press 1 to continue and 0 to stop
1

--- Linked List Menu ---


press 1 to Append
press 2 to Add at Beginning
press 3 to Insert at Position
press 4 to Delete at Position
press 5 to Display
Enter your choice: 5
Linked list contents:
100
200
600
300
press 1 to continue and 0 to stop

2.​circular linked list


import java.util.*;

// Node class

class NodeC {
int data;
NodeC link;
}

// Circular Linked List class

class CircularList {
NodeC head, temp,prev;

void append(int num) {


NodeC n = new NodeC();
n.data = num;
// System.out.println("In Circular Append");
if(head == null) {
n.link = n; // only when the list is empty
head = n;
} else {
temp = head;
while (temp.link != head) {
temp = temp.link;
}
temp.link = n;
n.link = head;
// prints data backwards
// head=n;
}
}
void display() {
if (head == null) {
System.out.println("Circular Linked list is empty:");
} else {
temp = head;
while (temp.link != head) {
System.out.println(temp.data);
temp = temp.link; // FIXED: now it moves to the next NodeC
}
System.out.println(temp.data); // Print the last data in last NodeC
}
}

void addAtBegining(int no){


NodeC n = new NodeC();
n.data=no;
temp =head;
while(temp.link!= head)
{
temp =temp.link;
}
temp.link =n;
n.link=head;
head =n;
}

void insert(int num, int pos) {


NodeC n = new NodeC();
n.data = num;

// If inserting at position 1, same as add at beginning


if (pos == 1) {
temp = head;
while (temp.link != head) {
temp = temp.link;
}
temp.link = n;
n.link = head;
head = n;
} else {
temp = head;
int i = 1;

// Traverse to the (pos-1)th node


while (i < pos - 1 && temp.link != head) {
temp = temp.link;
i++;
}

// Insert the new node


n.link = temp.link;
temp.link = n;
}
}
void deleteAtPosition(int no){
temp=head;
prev=head;
while(temp.link!=head)
{
if (no==temp.data)
{
if (temp==head)
{
while(temp.link!=head)
{
temp=temp.link;
}
temp.link=head.link;
head=head.link;
}
else {
prev.link=temp.link;
temp=prev.link;
}
}
else {
prev=temp;
temp=temp.link;
}
}
}
}

// Main class
public class CircularLinkedList {
public static void main(String[] args) {
CircularList LL = new CircularList();
Scanner sc = new Scanner(System.in);
int choice, num, C, loc;

do {
System.out.println("\n--- Circular Linked List Menu ---");
System.out.println("1 to Append");
System.out.println("2 to Add at Beginning");
System.out.println("3 to Insert at Position");
System.out.println("4 to Delete by Value");
System.out.println("5 to Display");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch (choice) {
case 1 : {
System.out.print("Enter number to append: ");
num = sc.nextInt();
LL.append(num);
break;
}
case 2 : {
System.out.print("Enter number to add at the beginning: ");
num = sc.nextInt();
LL.addAtBegining(num);
break;
}
case 3 : {
System.out.print("Enter number: ");
num = sc.nextInt();
System.out.print("Enter the position: ");
loc = sc.nextInt();
LL.insert(num, loc);
break;
}
case 4 : {
System.out.print("Enter the number to be deleted: ");
num = sc.nextInt();
LL.deleteAtPosition(num);
break;
}
case 5 : LL.display();
break;
default : System.out.println("Invalid choice");
}

System.out.println("Press 1 to continue and 0 to stop:");


C = sc.nextInt();
} while (C != 0);
sc.close();
}
}

Output
run:

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 1
Enter number to append: 100
Press 1 to continue and 0 to stop:
1

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 1
Enter number to append: 20
Press 1 to continue and 0 to stop:
1

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 1
Enter number to append: 300
Press 1 to continue and 0 to stop:
1

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 2
Enter number to add at the beginning: 56
Press 1 to continue and 0 to stop:
1

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 3
Enter number: 88
Enter the position: 2
Press 1 to continue and 0 to stop:
1

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 5
56
88
100
20
300
Press 1 to continue and 0 to stop:
1

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 4
Enter the number to be deleted: 20
Press 1 to continue and 0 to stop:
1

--- Circular Linked List Menu ---


1 to Append
2 to Add at Beginning
3 to Insert at Position
4 to Delete by Value
5 to Display
Enter your choice: 5
56
88
100
300
Press 1 to continue and 0 to stop:

3.​Stacked Linked List


import java.util.*;

// Node class
class NodeS {
int data;
NodeS link;
}

// Stacked Linked List class (LIFO)


class Stack {
NodeS top, temp;

// Push operation
public void push(int no) {
NodeS n = new NodeS();
n.data = no;
n.link = null;

if (top == null) {
top = n;
} else {
n.link = top;
top = n;
}
System.out.println("Pushed: " + no);
}

// Pop operation
public void pop() {
if (top == null) {
System.out.println("Stack is empty.");
} else {
System.out.println("Popped out data: " + top.data);
top = top.link;
}
}

// Display operation
public void display() {
if (top == null) {
System.out.println("Stack is empty.");
} else {
temp = top;
System.out.println("Stack contents (top to bottom):");
while (temp != null) {
System.out.println(temp.data);
temp = temp.link;
}
}
}
}

public class StackedLinkedList {


public static void main(String[] args) {
Stack st = new Stack();
Scanner sc = new Scanner(System.in);
int choice, num, C;

do {
System.out.println("\n--- Stacked Linked List Menu ---");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter number to push: ");
num = sc.nextInt();
st.push(num);
break;

case 2:
st.pop();
break;

case 3:
st.display();
break;

default:
System.out.println("Invalid choice. Please enter 1, 2, or 3.");
}

System.out.print("Press 1 to continue or 0 to exit: ");


C = sc.nextInt();
} while (C != 0);

sc.close();
}
}

output
run:

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 1
Enter number to push: 100
Pushed: 100
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 1
Enter number to push: 200
Pushed: 200
Press 1 to continue or 0 to exit: 1
--- Stacked Linked List Menu ---
1. Push
2. Pop
3. Display
Enter your choice: 1
Enter number to push: 300
Pushed: 300
Press 1 to continue or 0 to exit: 11

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 2
Popped out data: 300
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 3
Stack contents (top to bottom):
200
100
Press 1 to continue or 0 to exit:

4.​Queued link list


import java.util.Scanner;

// Node class

class NodeQ {
int data;
NodeQ link;
}

// Queue Linked List class (FIFO)

class Queues {
NodeQ Front,temp,rear;

public void push( int no){


NodeQ n = new NodeQ();
n.data=no;
n.link=null;
if (Front==null){
Front=n;
rear=n;
}
else{
rear.link=n;
rear=n;
}
}

public void pop(){


if (Front==null){
System.out.println("Queue link is empty..");
}
else{
System.out.println("Popped out data is "+ Front.data);
Front=Front.link;
}
}

public void display(){


if (Front==null){
System.out.println("Queue link is empty..");
}
else{
temp = Front;
while(temp!=null){
System.out.println(temp.data);
temp=temp.link;
}
}
}
}

public class QueueLinkedList {


public static void main(String[] args) {
Queues Q = new Queues();
Scanner sc = new Scanner(System.in);
int choice, num,C;

do {
System.out.println("\n--- Queued Linked List Menu ---");
System.out.println("1 to Push");
System.out.println("2 to Pop");
System.out.println("3 to Display");
System.out.println("Enter your choice");
choice = sc.nextInt();

switch (choice) {
case 1 : {
System.out.print("Enter number to push: ");
num = sc.nextInt();
Q.push(num);
break;
}
case 2 : {
Q.pop();
break;
}
case 3 : Q.display();
break;
default : System.out.println("Invalid choice");
}

System.out.println("Press 1 to continue and 0 to stop:");


C = sc.nextInt();
} while (C != 0);
sc.close();
}
}

Output
run:

--- Queued Linked List Menu ---


1 to Push
2 to Pop
3 to Display
Enter your choice
1
Enter number to push: 100
Press 1 to continue and 0 to stop:
1

--- Queued Linked List Menu ---


1 to Push
2 to Pop
3 to Display
Enter your choice
1
Enter number to push: 200
Press 1 to continue and 0 to stop:
1

--- Queued Linked List Menu ---


1 to Push
2 to Pop
3 to Display
Enter your choice
1
Enter number to push: 300
Press 1 to continue and 0 to stop:
1
--- Queued Linked List Menu ---
1 to Push
2 to Pop
3 to Display
Enter your choice
2
Popped out data is 100
Press 1 to continue and 0 to stop:
1

--- Queued Linked List Menu ---


1 to Push
2 to Pop
3 to Display
Enter your choice
3
200
300
Press 1 to continue and 0 to stop:

5.​Stacked Linked List using array


import java.util.*;

// Node class
class NodeS {
int data;
NodeS link;
}

// Stacked Linked List class (LIFO)


class Stack {
NodeS top, temp;

// Push operation
public void push(int no) {
NodeS n = new NodeS();
n.data = no;
n.link = null;

if (top == null) {
top = n;
} else {
n.link = top;
top = n;
}
System.out.println("Pushed: " + no);
}
// Pop operation
public void pop() {
if (top == null) {
System.out.println("Stack is empty.");
} else {
System.out.println("Popped out data: " + top.data);
top = top.link;
}
}

// Display operation
public void display() {
if (top == null) {
System.out.println("Stack is empty.");
} else {
temp = top;
System.out.println("Stack contents (top to bottom):");
while (temp != null) {
System.out.println(temp.data);
temp = temp.link;
}
}
}
}

public class StackedLinkedList {


public static void main(String[] args) {
Stack st = new Stack();
Scanner sc = new Scanner(System.in);
int choice, num, C;

do {
System.out.println("\n--- Stacked Linked List Menu ---");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter number to push: ");
num = sc.nextInt();
st.push(num);
break;

case 2:
st.pop();
break;
case 3:
st.display();
break;

default:
System.out.println("Invalid choice. Please enter 1, 2, or 3.");
}

System.out.print("Press 1 to continue or 0 to exit: ");


C = sc.nextInt();
} while (C != 0);

sc.close();
}
}
output
run:

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 1
Enter number to push: 10
Pushed: 10
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 20
Invalid choice. Please enter 1, 2, or 3.
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 1
Enter number to push: 20
Pushed: 20
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 1
Enter number to push: 30
Pushed: 30
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 3
Stack contents (top to bottom):
30
20
10
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 2
Popped out data: 30
Press 1 to continue or 0 to exit: 1

--- Stacked Linked List Menu ---


1. Push
2. Pop
3. Display
Enter your choice: 3
Stack contents (top to bottom):
20
10
Press 1 to continue or 0 to exit:

6.​Queue Linked List using array


import java.util.Scanner;

class Queue {
private int[]QU = new int[5];
private int front, rear;

// Constructor
public Queue() {
front = 0;
rear = 0;
}

// push element
public void push(int num) {
if (rear == 5) {
System.out.println("Queue is full.");
} else {
QU[rear] = num;
rear++;
System.out.println("push: " + num);
}
}

// Display elements in the Queue


public void display() {
if (front == rear) {
System.out.println("Queue is empty.");
} else {
System.out.println("Queue contents display:");
for (int i = front; i<rear; i++){
System.out.println(QU[i]);
}
}
}

// Pop element from the Queue


public void pop() {
if (front == rear) {
System.out.println("Queue is empty.");
} else {
System.out.println("pop " + QU[front]);
for (int i = 0; i < rear - 1; i++) {
QU[i] = QU[i + 1]; // Shift elements
}
rear--;
}
}

// Main class with menu


public class QueueArray {
public static void main(String[] args){
Queue q = new Queue();
Scanner sc = new Scanner(System.in);
int choice, num, C;

do {
System.out.println("\n--- Queue Menu (Array-based) ---");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. Display");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter number to push ");
num = sc.nextInt();
q.push(num);
break;

case 2:
q.pop();
break;

case 3:
q.display();
break;
}

System.out.print("Press 1 to continue or 0 to exit: ");


C = sc.nextInt();
} while (C != 0);

sc.close();
}
}
Output
run:

--- Queue Menu (Array-based) ---


1. push
2. pop
3. Display
Enter your choice: 1
Enter number to push 100
push: 100
Press 1 to continue or 0 to exit: 1

--- Queue Menu (Array-based) ---


1. push
2. pop
3. Display
Enter your choice: 1
Enter number to push 200
push: 200
Press 1 to continue or 0 to exit: 1
--- Queue Menu (Array-based) ---
1. push
2. pop
3. Display
Enter your choice: 1
Enter number to push 300
push: 300
Press 1 to continue or 0 to exit: 1

--- Queue Menu (Array-based) ---


1. push
2. pop
3. Display
Enter your choice: 3
Queue contents display:
100
200
300
Press 1 to continue or 0 to exit: 1

--- Queue Menu (Array-based) ---


1. push
2. pop
3. Display
Enter your choice: 2
pop 100
Press 1 to continue or 0 to exit:
1

--- Queue Menu (Array-based) ---


1. push
2. pop
3. Display
Enter your choice: 3
Queue contents display:
200
300
Press 1 to continue or 0 to exit:

You might also like