• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

implementing linked list using core java

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have been trying to re-create the infamous singly linked list code, implemented using C, with the help of core java.
I am trying to incorporate the functionality, like
1. create a node
2. insert a node,
2.1. at the beginning of the list
2.2. at the end of the list
2.3. in between the list
3. remove
4. modify
5. print the list

and sadly am stuck at inserting in-between the list.

what i am trying to do is to take data and link as int, if i want to insert in between then find the node whose link is same as that of the incoming node and increment the link of the nodes with link value greater than the incoming link.
and yes there is headCount to keep track of the number of elements
whew!!!

here is the piece of code i have created,
--------------------------------------------------------------------------
class Node.java
public class Node {

public Node head, nextNode;
public Node newNode;
public int data, link;
public static int headCount=0;

public Node(int data, int link){
this.link=link;
this.data=data;
}
public void setLink(int link){
this.link= link;
}
public void printList(){
System.out.println("{"+data+","+link+"}");
}
}

class Operations.java

public class Operations {

public Node firstNode, lastNode;
public Node currentNode;
public Operations(){
firstNode=null;
// nextNode=null;
currentNode=null;
lastNode=null;
}
public void insert(int data, int link){

// entry values are 0, -ve or greater than the accepted value
if(link<=0 || link>Node.headCount+1){
System.out.println("invalid entry, cannot be 0, -ve or greater than "+(Node.headCount+1));
}
if(link>0){
// element entering at location 1
if(link==1){
//first element
if(Node.headCount==0){
Node newNode= new Node(data, link);
firstNode=newNode;
lastNode=newNode;
newNode.nextNode=null;
lastNode.nextNode=null;
Node.headCount++;
System.out.println("{"+newNode.data+","+newNode.li nk+"}");

}

// Beginning of the list
if(Node.headCount==1){
Node newNode= new Node(data, link);
firstNode.link=(firstNode.link+1);
currentNode=firstNode;
lastNode=currentNode;
newNode.nextNode=lastNode;
newNode=firstNode;
lastNode.nextNode=null;
currentNode=null;
}
Node.headCount++;
}

// at the end of the list
if(link>1 && link==(Node.headCount+1)){
Node newNode= new Node(data, link);
lastNode.nextNode=newNode;
newNode=lastNode;
newNode.nextNode=null;
Node.headCount++;
}

//In-between
if(link>1 && link<(Node.headCount+1)){
Node newNode= new Node(data, link);
currentNode.link= newNode.link;
//temp.nextNode=currentNode;

while(currentNode.nextNode != null){
currentNode.link=(currentNode.link+1);
currentNode=currentNode.nextNode;
}
}

}


}
// the print functionality is incomplete, but i am posting it anyway
public void printList(){
Node curr= firstNode;
while(curr != null){
curr.printList();
curr=curr.nextNode;
}
}
}

class Test.java
public class Test {
public static void main(String[] args) {
Operations operate= new Operations();
operate.insert(10, 1);
operate.insert(20, 1);

}

}
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What is infamous about it? Singly linked lists are easy to implement.
You are supposed to have a Node as a self-referential class, with a reference to the data and a reference to the next Node. Its fields would be different. You would not want public fields, nor would you want all the references to other Nodes.
You also want a List class, which calls the different Nodes, and has a reference to the first Node. You can maintain a reference to the currently-accessed Node in that class too. In fact you can have the Node class as a private inner class of the List class.

By the way: Please maintain indentation and use the code button when quoting code.
 
pranay jha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

sorry for the late reply...
but, i got the concept through your post and finally got the "self referential" concept right.
and implemented lot of linked list and tree and search codes....
Thanks a lot ... i guess i have a better understanding of self referential types..
and i now know how to use "code" tool too..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic