473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,399 developers and data experts.

How to implement singly linked list in Java

package com.ds.algorithms.linkedlist.SLLcustomImplement;
/** * @Author pankaj * @create 4/11/21 6:19 PM */
public class Node {
int data;
Node next;
}
— — — — — — — — — — — — — — — — — — — — — — — — — — — —
package com.ds.algorithms.linkedlist.SLLcustomImplement;
/**
* @Author pankaj
* @create 4/11/21 6:25 PM
*/
public class LinkedList {
Node head;// first Node
public void add(int data) {
// create Node before add it to LL
Node node=new Node();
node.data=data;
node.next=null;
// if you are adding first node, List is empty
if (head == null)
{
head=node;
}
// if head(first node is already added
else {
Node travers=head;// to traverse Node, to hold temp data
while (travers.next!=null)
{
travers=travers.next;// going to next node
}
// after reaching to last node
travers.next=node;// newly created Node is pointing
}
}
// add at first
public void addAtFirst(int data)
{
Node node=new Node();
node.data=data;
node.next=null;
node.next=head; // newly created next value will be previous head node
// change newly created Node to Head
head=node;
}
// addAt index
public void addAtIndex(int index,int data)
{
Node node=new Node();
node.data=data;
node.next=null;
// add Node to 0th index — .> add it to first Location
if(index==0)
{
addAtFirst(data);
}else {
Node n = head;
for (int i = 0; i < index — 1; i++) {
n = n.next;
}
node.next = n.next;// change ref, index-1 Node to newly created Node
n.next = node;
}
}
// delete value
public void deleteAt(int index)
{
if(index==0)
{
head=head.next;
} else {
Node n=head;
Node nRef=null;
for (int i=0;i<index-1;i++) // reach to that specific index
{
n=n.next;
}
// store ref of next Node of index element
nRef=n.next;
n.next=nRef.next;
System.out.println(“ Deleted : INDEXED “+ index+” DATA “+nRef.data);
}
}
// display method
public void display()
{
Node node=head;
while (node.next !=null)
{
System.out.print(node.data+” -> “);
node=node.next;// going to next node
}
System.out.print(node.data); // print last Node
}
}
— — — — — — — — — — — — — — — — — — — — — — — — — -
package com.ds.algorithms.linkedlist.SLLcustomImplement;
/**
* @Author pankaj
* @create 4/11/21 6:21 PM
*/
public class RunLinkedList {
public static void main(String[] args) {
LinkedList linkedList=new LinkedList();
linkedList.add(10);
linkedList.add(45);
linkedList.add(78);
linkedList.addAtFirst(520);
linkedList.addAtIndex(3,300);
linkedList.addAtIndex(0,100);
linkedList.display();
linkedList.deleteAt(1);
linkedList.display();
}
}
Apr 12 '21 #1
0 4558

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
4
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
2
by: Justin Crites | last post by:
I have an object which I want to be serializable. I have marked with with . The object only has a single data member, which is a LinkedList<int>. This linked list is a private member and cannot...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
10
by: ac.c.2k7 | last post by:
Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. 1. But if the node to be deleted is the last node. Then what should we do ?...
6
by: Phillip.Ross.Taylor | last post by:
When I designed my application I created an object called "Orderable" which exposes a public property "sequence". Then a few objects inherit from this. I'll just call them ObjectX for the sake...
3
by: huiling25 | last post by:
I don't know why the customer records cannot be inserted into the linked list and the head of the linked list keep pointing to null... //ListNode.java public class ListNode{ private Object...
2
by: monkey0525 | last post by:
Hi, I'm writing a program that reverses the lines of a String using the LinkedList structure, which includes an iterator. I have my code written out, but every time my program runs, it takes a very...
2
by: raamay | last post by:
i have been given an assignment to create a singly linked list which maintains order. but i am not sure whether to use a sorting method or to take care of the sorting while adding the values in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.