LinkedList high level implementation [i]
Problem statement: LinkedList high level implementation in java !
- class Node4 {
- Node4 next;
- int data;
- Node4(int data) {
- this.data = data;
- }
- }
- public class LinkedListHighLevelExe1 {
- Node4 head;
- public static void main(String[] args) {
- LinkedListHighLevelExe1 l = new LinkedListHighLevelExe1();
- l.add(3);
- l.add(5);
- l.add(8);
- l.add(9);
- l.print(l.head);
- }
- // add the element in LinkedList
- public void add(int data) {
- Node4 next = new Node4(data);
- if (head == null) {
- head = next;
- return;
- }
- Node4 last = head;
- while (last.next != null) {
- last = last.next;
- }
- last.next = next;
- }
- // print the LinkedList
- void print(Node4 next) {
- Node4 temp = next;
- while (temp != null) {
- System.out.print(temp.data + "-->");
- temp = temp.next;
- }
- }
- }
Output:
3-->5-->8-->9-->
Comments
Post a Comment