We were given an assignment to create a LinkedList from scratch, and there are absolutely no readings given to guide us on this migrane-causing task. Also everything online
class Node
{
int data;
Node link;
public Node()
{
data=0;
link=null;
}
Node ptr,start,temp;
void create()throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter first data");
this.data=Integer.parseInt(br.readLine());
ptr=this;
start=ptr;
char ins ='y';
do
{
System.out.println("Wanna Insert another node???");
ins=(char)br.read();
br.read();
if(ins=='y')
{
temp=new Node();
System.out.println("Enter next data");
temp.data=Integer.parseInt(br.readLine());
temp.link=null;
ptr.link=temp;
temp=null;
ptr=ptr.link;
}
}while(ins=='y');
}
public static void main(String args[])throws IOException
{
Node first= new Node();
first.create();
}
}
Linked list to demonstrate Insert Front, Delete Front, Insert Rear and Delete Rear operations in Java:
import java.io.DataInputStream;
import java.io.IOException;
public class LinkedListTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Node root = null;
DataInputStream reader = new DataInputStream(System.in);
int op = 0;
while(op != 6){
try {
System.out.println("Enter Option:\n1:Insert Front 2:Delete Front 3:Insert Rear 4:Delete Rear 5:Display List 6:Exit");
//op = reader.nextInt();
op = Integer.parseInt(reader.readLine());
switch (op) {
case 1:
System.out.println("Enter Value: ");
int val = Integer.parseInt(reader.readLine());
root = insertNodeFront(val,root);
display(root);
break;
case 2:
root=removeNodeFront(root);
display(root);
break;
case 3:
System.out.println("Enter Value: ");
val = Integer.parseInt(reader.readLine());
root = insertNodeRear(val,root);
display(root);
break;
case 4:
root=removeNodeRear(root);
display(root);
break;
case 5:
display(root);
break;
default:
System.out.println("Invalid Option");
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Exited!!!");
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static Node insertNodeFront(int value, Node root){
Node temp = new Node(value);
if(root==null){
return temp; // as root or first
}
else
{
temp.next = root;
return temp;
}
}
static Node removeNodeFront(Node root){
if(root==null){
System.out.println("List is Empty");
return null;
}
if(root.next==null){
return null; // remove root itself
}
else
{
root=root.next;// make next node as root
return root;
}
}
static Node insertNodeRear(int value, Node root){
Node temp = new Node(value);
Node cur = root;
if(root==null){
return temp; // as root or first
}
else
{
while(cur.next!=null)
{
cur = cur.next;
}
cur.next = temp;
return root;
}
}
static Node removeNodeRear(Node root){
if(root==null){
System.out.println("List is Empty");
return null;
}
Node cur = root;
Node prev = null;
if(root.next==null){
return null; // remove root itself
}
else
{
while(cur.next!=null)
{
prev = cur;
cur = cur.next;
}
prev.next=null;// remove last node
return root;
}
}
static void display(Node root){
System.out.println("Current List:");
if(root==null){
System.out.println("List is Empty");
return;
}
while (root!=null){
System.out.print(root.val+"->");
root=root.next;
}
System.out.println();
}
static class Node{
int val;
Node next;
public Node(int value) {
// TODO Auto-generated constructor stub
val = value;
next = null;
}
}
}
What you have coded is not a LinkedList, at least not one that I recognize. For this assignment, you want to create two classes:
LinkNode
LinkedList
A LinkNode
has one member field for the data it contains, and a LinkNode
reference to the next LinkNode
in the LinkedList
. Yes, it's a self referential data structure. A LinkedList
just has a special LinkNode
reference that refers to the first item in the list.
When you add an item in the LinkedList
, you traverse all the LinkNode's
until you reach the last one. This LinkNode's
next should be null. You then construct a new LinkNode
here, set it's value, and add it to the LinkedList
.
public class LinkNode {
String data;
LinkNode next;
public LinkNode(String item) {
data = item;
}
}
public class LinkedList {
LinkNode head;
public LinkedList(String item) {
head = new LinkNode(item);
}
public void add(String item) {
//pseudo code: while next isn't null, walk the list
//once you reach the end, create a new LinkNode and add the item to it. Then
//set the last LinkNode's next to this new LinkNode
}
}
Hint 1: read the description of linked lists at http://en.wikipedia.org/wiki/Linked_list
Hint 2: the Java implementation of LinkedList is a doubly linked list. Yours is a singly linked list. The algorithms don't directly apply.
Also:
... but creating [a linked list class] from scratch makes no sense whatsoever.
It depends on what the required outcome of the work is. If the goal is to produce code that meets certain functional / non-functional requirements, then you are right. If the real goal is for you to learn how to program / design APIs / implement non-trivial data structures, then the utility of the final product is almost entirely irrelevant.
And thus magically we have a linked list
What you actually have there is a open data type, that could be used to build a (sort of) list. But that is not what your teacher wants. And it certainly would not be considered to be a useful list abstraction. A useful abstraction would include:
methods to do the things that programmers don't want to have to repeat over and over again, and
an abstraction layer that stops programmers "breaking" the list; e.g. by accidentally creating a cycle, or accidentally stitching a sublist in two lists to create an inverted tree.
Pleas find bellow Program
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedListManual {
Node node;
public void pushElement(int next_node) {
Node nd = new Node(next_node);
nd.next = node;
node = nd;
}
public int getSize() {
Node temp = node;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
public void getElement() {
Node temp = node;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
public static void main(String[] args) {
LinkedListManual obj = new LinkedListManual();
obj.pushElement(1);
obj.pushElement(2);
obj.pushElement(3);
obj.getElement(); //get element
System.out.println(obj.getSize()); //get size of link list
}
}