Skip to content

Commit bae0c11

Browse files
implement addAtIndex() to add element at any index
in the list.
1 parent 785e68c commit bae0c11

File tree

4 files changed

+134
-28
lines changed

4 files changed

+134
-28
lines changed

DoublyLinkedList.java

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,119 @@ private static class Node<E> {
33
private E element;
44
private Node<E> prev;
55
private Node<E> next;
6-
public Node(E e, Node<E> p, Node<E> n){
6+
7+
public Node(E e, Node<E> p, Node<E> n) {
78
element = e;
89
prev = p;
910
next = n;
1011
}
11-
public E getElement() { return element; }
12-
public Node<E> getPrev() { return prev; }
13-
public Node<E> getNext() { return next; }
14-
public void setPrev(Node<E> p) { prev = p;}
15-
public void setNext(Node<E> n) { next = n;}
12+
13+
public E getElement() {
14+
return element;
15+
}
16+
17+
public Node<E> getPrev() {
18+
return prev;
19+
}
20+
21+
public Node<E> getNext() {
22+
return next;
23+
}
24+
25+
public void setPrev(Node<E> p) {
26+
prev = p;
27+
}
28+
29+
public void setNext(Node<E> n) {
30+
next = n;
31+
}
1632
}
33+
1734
private Node<E> header;
1835
private Node<E> tailer;
1936
private int size = 0;
20-
//constructor a new empty list
21-
public DoublyLL(){
37+
38+
// constructor a new empty list
39+
public DoublyLL() {
2240
header = new Node<>(null, null, null);
2341
tailer = new Node<>(null, header, null);
2442
header.setNext(tailer);
2543
}
26-
public int size(){
44+
45+
public int size() {
2746
return size;
2847
}
29-
public boolean isEmpty(){
48+
49+
public boolean isEmpty() {
3050
return size == 0;
3151
}
32-
public E first(){
52+
53+
public E first() {
3354
if (isEmpty()) {
3455
return null;
3556
}
3657
return header.getNext().getElement();
3758
}
38-
public E last(){
59+
60+
public E last() {
3961
if (isEmpty()) {
4062
return null;
4163
}
4264
return tailer.getPrev().getElement();
4365
}
44-
//update methods
45-
public void addFirst(E e){
66+
67+
// update methods
68+
public void addFirst(E e) {
4669
addBetween(e, header, header.getNext());
4770
}
48-
public void addLast(E e){
71+
72+
public void addLast(E e) {
4973
addBetween(e, tailer.getPrev(), tailer);
5074
}
51-
public E removeFirst(){
75+
76+
public void addAtIndex(E e, int index) {
77+
if (index < 0 || index > size) {
78+
throw new IndexOutOfBoundsException("Index: " + index + " size: " + size);
79+
}
80+
if (isEmpty()) {
81+
addBetween(e, tailer, header.getNext());
82+
} else if (index == 0) {
83+
addBetween(e, header, header.getNext());
84+
} else if (index == size) {
85+
addBetween(e, tailer.getPrev(), tailer);
86+
}
87+
88+
else {
89+
Node<E> newNode = header;
90+
for (int i = 0; i < index; i++) {
91+
System.out.println("\nelemet at: " + i + " e: " + newNode.getElement());
92+
newNode = newNode.getNext();
93+
}
94+
addBetween(e, newNode, newNode.getNext());
95+
System.out.println("\nheader: " + newNode.getElement());
96+
}
97+
}
98+
99+
public E removeFirst() {
52100
if (isEmpty()) {
53101
return null;
54102
}
55103
return remove(header.getNext());
56104
}
57-
public E removeLast(){
58-
if (isEmpty()) return null;
105+
106+
public E removeLast() {
107+
if (isEmpty())
108+
return null;
59109
return remove(tailer.getPrev());
60110
}
61-
private void addBetween(E e, Node<E> predecessor, Node<E> successor ){
111+
112+
private void addBetween(E e, Node<E> predecessor, Node<E> successor) {
62113
Node<E> newest = new Node<>(e, predecessor, successor);
63114
predecessor.setNext(newest);
64115
successor.setPrev(newest);
65116
size++;
66117
}
118+
67119
private E remove(Node<E> node) {
68120
Node<E> predecessor = node.getPrev();
69121
Node<E> successor = node.getNext();
@@ -72,33 +124,39 @@ private E remove(Node<E> node) {
72124
size--;
73125
return node.getElement();
74126
}
75-
public void display(){
127+
128+
public void display() {
76129
if (isEmpty()) {
77130
System.out.println("List is empty!");
78131
return;
79132
}
80133
Node<E> current = header.getNext();
81-
while(current != null){
82-
System.out.print(current.getElement()+ " ");
134+
while (current != null) {
135+
System.out.print(current.getElement() + " ");
83136
current = current.getNext();
84-
if (current.getElement() ==null) {
137+
if (current.getElement() == null) {
85138
return;
86139
}
87140
}
88141
}
89142

90143
}
91-
public class DoublyLinkedList{
92-
public static void main(String[] args){
144+
145+
public class DoublyLinkedList {
146+
public static void main(String[] args) {
93147
DoublyLL<Integer> dll = new DoublyLL<>();
94148
dll.addFirst(3);
95149
dll.addFirst(2);
96150
dll.addFirst(1);
97151
dll.addLast(4);
98-
System.out.println("First element: "+dll.first());
99-
System.out.println("Last element: "+dll.last());
100-
System.out.println("Total element: "+dll.size());
152+
// dll.addAtIndex(33, 3);
153+
System.out.println("First element: " + dll.first());
154+
System.out.println("Last element: " + dll.last());
155+
System.out.println("Total element: " + dll.size());
101156
System.out.println("Doubly Linked List: ");
102157
dll.display();
158+
dll.addAtIndex(13, 3);
159+
System.out.println("\nAfter inserting at i");
160+
dll.display();
103161
}
104162
}

Hello.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
public class Hello {
3+
public static int myvar = 45;
4+
public static int var = 5;
5+
6+
public static void main(String[] args) {
7+
System.out.println("this is rajendra pancholi" + myvar);
8+
System.err.println("Error");
9+
10+
}
11+
}

HelloWorldGUI1.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import javax.swing.JOptionPane;
2+
public class HelloWorldGUI1 {
3+
public static void main(String[] args) {
4+
JOptionPane.showMessageDialog( null, "Hello Rajendra Pancholi!" );
5+
}
6+
}

HelloWorldGUI2.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
import javax.swing.*;
4+
public class HelloWorldGUI2 {
5+
private static class HelloWorldDisplay extends JPanel {
6+
public void paintComponent(Graphics g) {
7+
super.paintComponent(g);
8+
g.drawString( "Hello Rajendra Pacholi!", 20, 30 );
9+
}
10+
}
11+
private static class ButtonHandler implements ActionListener {
12+
public void actionPerformed(ActionEvent e) {
13+
System.exit(0);
14+
}
15+
}
16+
public static void main(String[] args) {
17+
HelloWorldDisplay displayPanel = new HelloWorldDisplay();
18+
JButton okButton = new JButton("OK");
19+
ButtonHandler listener = new ButtonHandler();
20+
okButton.addActionListener(listener);
21+
JPanel content = new JPanel();
22+
content.setLayout(new BorderLayout());
23+
content.add(displayPanel, BorderLayout.CENTER);
24+
content.add(okButton, BorderLayout.SOUTH);
25+
JFrame window = new JFrame("Raje");
26+
window.setContentPane(content);
27+
window.setSize(250,200);
28+
window.setLocation(400,100);
29+
window.setVisible(true);
30+
}
31+
}

0 commit comments

Comments
 (0)