Skip to content

Commit 0bb9f27

Browse files
author
Ram swaroop
committed
linkedlist boilerplate code done
1 parent efc36c1 commit 0bb9f27

File tree

6 files changed

+322
-16
lines changed

6 files changed

+322
-16
lines changed
+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package me.ramswaroop.common;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
*
6+
* @author: ramswaroop
7+
* @date: 6/16/15
8+
* @time: 12:53 PM
9+
*/
10+
public interface LinkedList<E> {
11+
12+
/**
13+
* Appends the specified element to the end of this list.
14+
*
15+
* @param item
16+
* @return
17+
*/
18+
boolean add(E item);
19+
20+
/**
21+
* Inserts the specified element at the specified position in this list.
22+
*
23+
* @param index
24+
* @param item
25+
* @return
26+
* @throws IndexOutOfBoundsException {@inheritDoc}
27+
*/
28+
boolean add(int index, E item);
29+
30+
/**
31+
* Inserts the specified element at the beginning of this list.
32+
*
33+
* @param item
34+
*/
35+
void addFirst(E item);
36+
37+
/**
38+
* Appends the specified element to the end of this list.
39+
*
40+
* @param item
41+
*/
42+
void addLast(E item);
43+
44+
/**
45+
* Removes all of the elements from this list.
46+
*/
47+
void clear();
48+
49+
/**
50+
* Returns a shallow copy of this LinkedList.
51+
*
52+
* @return
53+
*/
54+
LinkedList<E> clone();
55+
56+
/**
57+
* Returns true if this list contains the specified element.
58+
*
59+
* @param item
60+
* @return
61+
*/
62+
boolean contains(E item);
63+
64+
/**
65+
* Returns the element at the specified position in this list.
66+
*
67+
* @param index
68+
* @return
69+
* @throws IndexOutOfBoundsException {@inheritDoc}
70+
*/
71+
E get(int index);
72+
73+
/**
74+
* Returns the first element in this list.
75+
*
76+
* @return
77+
* @throws java.util.NoSuchElementException if this list is empty
78+
*/
79+
E getFirst();
80+
81+
/**
82+
* Returns the last element in this list.
83+
*
84+
* @return
85+
* @throws java.util.NoSuchElementException if this list is empty
86+
*/
87+
E getLast();
88+
89+
/**
90+
* Retrieves and removes the head (first element) of this list.
91+
*
92+
* @return
93+
*/
94+
E remove();
95+
96+
/**
97+
* Removes the element at the specified position in this list.
98+
*
99+
* @param index
100+
* @return
101+
* @throws IndexOutOfBoundsException {@inheritDoc}
102+
*/
103+
E remove(int index);
104+
105+
/**
106+
* Removes the first occurrence of the specified element from this list, if it is present.
107+
*
108+
* @param item
109+
* @return {@code true} if this list contained the specified element
110+
*/
111+
boolean remove(E item);
112+
113+
/**
114+
* Replaces the element at the specified position in this list with the specified element.
115+
*
116+
* @param index
117+
* @param item
118+
* @return
119+
* @throws IndexOutOfBoundsException {@inheritDoc}
120+
*/
121+
E set(int index, E item);
122+
123+
/**
124+
* Returns the number of elements in this list.
125+
*
126+
* @return
127+
*/
128+
int size();
129+
130+
}

src/me/ramswaroop/common/LinkedQueue.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public E element() {
4545
if (front == null) {
4646
throw new NoSuchElementException();
4747
}
48-
return front.value;
48+
return front.item;
4949
}
5050

5151
@Override
@@ -72,17 +72,17 @@ public void print() {
7272
return;
7373
}
7474
for (node = front; node != rear; node = node.next) {
75-
System.out.print(node.value + ",");
75+
System.out.print(node.item + ",");
7676
}
77-
System.out.println(node.value + "]");
77+
System.out.println(node.item + "]");
7878
}
7979

8080
private class Node<E> {
81-
E value;
81+
E item;
8282
Node<E> next;
8383

84-
public Node(E value, Node<E> next) {
85-
this.value = value;
84+
public Node(E item, Node<E> next) {
85+
this.item = item;
8686
this.next = next;
8787
}
8888
}

src/me/ramswaroop/common/LinkedStack.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public E push(E item) {
3737
}
3838

3939
/**
40-
* Removes the object at the top of this stack and returns that object as the value of this function.
40+
* Removes the object at the top of this stack and
41+
* returns it.
4142
*
4243
* @return
4344
*/
@@ -58,7 +59,7 @@ public E peek() {
5859
if (top == null) {
5960
throw new EmptyStackException();
6061
}
61-
return top.value;
62+
return top.item;
6263
}
6364

6465
/**
@@ -87,9 +88,9 @@ public void print() {
8788
return;
8889
}
8990
for (node = top; node.next != null; node = node.next) {
90-
System.out.print(node.value + ",");
91+
System.out.print(node.item + ",");
9192
}
92-
System.out.println(node.value + "]");
93+
System.out.println(node.item + "]");
9394
}
9495

9596
/**
@@ -103,11 +104,11 @@ public boolean isEmpty() {
103104
}
104105

105106
private class Node<E> {
106-
E value;
107+
E item;
107108
Node<E> next;
108109

109-
Node(E value, Node next) {
110-
this.value = value;
110+
Node(E item, Node next) {
111+
this.item = item;
111112
this.next = next;
112113
}
113114
}

src/me/ramswaroop/common/Stack.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ public interface Stack<E> {
1616
public E push(E item);
1717

1818
/**
19-
* Removes the object at the top of this stack and returns
20-
* that object as the value of this function. This method
21-
* throws an exception if this queue is empty.
19+
* Removes the object at the top of this stack and returns it.
20+
* This method throws an exception if this queue is empty.
2221
*
2322
* @return
2423
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package me.ramswaroop.linkedlists;
2+
3+
import me.ramswaroop.common.LinkedList;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
*
8+
* @author: ramswaroop
9+
* @date: 6/16/15
10+
* @time: 1:00 PM
11+
*/
12+
public class DoubleLinkedList<E> implements LinkedList<E> {
13+
14+
@Override
15+
public boolean add(E item) {
16+
return false;
17+
}
18+
19+
@Override
20+
public boolean add(int index, E item) {
21+
return false;
22+
}
23+
24+
@Override
25+
public void addFirst(E item) {
26+
27+
}
28+
29+
@Override
30+
public void addLast(E item) {
31+
32+
}
33+
34+
@Override
35+
public void clear() {
36+
37+
}
38+
39+
@Override
40+
public LinkedList<E> clone() {
41+
return null;
42+
}
43+
44+
@Override
45+
public boolean contains(E item) {
46+
return false;
47+
}
48+
49+
@Override
50+
public E get(int index) {
51+
return null;
52+
}
53+
54+
@Override
55+
public E getFirst() {
56+
return null;
57+
}
58+
59+
@Override
60+
public E getLast() {
61+
return null;
62+
}
63+
64+
@Override
65+
public E remove() {
66+
return null;
67+
}
68+
69+
@Override
70+
public E remove(int index) {
71+
return null;
72+
}
73+
74+
@Override
75+
public boolean remove(E item) {
76+
return false;
77+
}
78+
79+
@Override
80+
public E set(int index, E item) {
81+
return null;
82+
}
83+
84+
@Override
85+
public int size() {
86+
return 0;
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package me.ramswaroop.linkedlists;
2+
3+
import me.ramswaroop.common.LinkedList;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
*
8+
* @author: ramswaroop
9+
* @date: 6/16/15
10+
* @time: 1:00 PM
11+
*/
12+
public class SingleLinkedList<E> implements LinkedList<E> {
13+
14+
@Override
15+
public boolean add(E item) {
16+
return false;
17+
}
18+
19+
@Override
20+
public boolean add(int index, E item) {
21+
return false;
22+
}
23+
24+
@Override
25+
public void addFirst(E item) {
26+
27+
}
28+
29+
@Override
30+
public void addLast(E item) {
31+
32+
}
33+
34+
@Override
35+
public void clear() {
36+
37+
}
38+
39+
@Override
40+
public LinkedList<E> clone() {
41+
return null;
42+
}
43+
44+
@Override
45+
public boolean contains(E item) {
46+
return false;
47+
}
48+
49+
@Override
50+
public E get(int index) {
51+
return null;
52+
}
53+
54+
@Override
55+
public E getFirst() {
56+
return null;
57+
}
58+
59+
@Override
60+
public E getLast() {
61+
return null;
62+
}
63+
64+
@Override
65+
public E remove() {
66+
return null;
67+
}
68+
69+
@Override
70+
public E remove(int index) {
71+
return null;
72+
}
73+
74+
@Override
75+
public boolean remove(E item) {
76+
return false;
77+
}
78+
79+
@Override
80+
public E set(int index, E item) {
81+
return null;
82+
}
83+
84+
@Override
85+
public int size() {
86+
return 0;
87+
}
88+
}

0 commit comments

Comments
 (0)