Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b14095

Browse files
Add example for add element to the linkedlist
1 parent 15f92a1 commit 4b14095

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.gaurav.ExProject.LinkedList;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* A Java program to add an element to the LinkedList.
7+
*
8+
* The add(E e) method will add an element to
9+
* the end of the linkedList.
10+
*
11+
* @author coderolls.com
12+
*
13+
*/
14+
public class LinkedListAddExample {
15+
16+
public static void main(String[] args) {
17+
18+
LinkedList<String> linkedList = new LinkedList<String>();
19+
20+
//adding elements to the linkedList
21+
linkedList.add("John");
22+
linkedList.add("Peter");
23+
24+
System.out.println(linkedList);// [John, Peter]
25+
26+
// adding an element to the linkedList
27+
// element will be added to the end of the linkedList
28+
linkedList.add("Noah");
29+
30+
System.out.println(linkedList); // [John, Peter, Noah]
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.gaurav.ExProject.LinkedList;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* A Java program to add an element to the linkedlist
7+
* at the specific index.
8+
*
9+
* The add(int index, E element) method will add
10+
* an element at the specified index in LinkedList.
11+
*
12+
* @author coderolls.com
13+
*
14+
*/
15+
public class LinkedListAddExample2 {
16+
17+
public static void main(String[] args) {
18+
19+
LinkedList<String> linkedList = new LinkedList<String>();
20+
21+
//adding elements to the linkedList using normal add method
22+
linkedList.add("John");
23+
linkedList.add("Peter");
24+
linkedList.add("Karan");
25+
26+
System.out.println(linkedList);// [John, Peter, Karan]
27+
28+
// adding an element to the LinkedList at index 1
29+
linkedList.add(1, "Noah");
30+
31+
System.out.println(linkedList); // [John, Noah, Peter, Karan]
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /