[英]how to form and traverse an array of linked list in java?
I have written a single linked list for insertion of elements, where each element is having two data values. 我为插入元素编写了一个链接列表,其中每个元素都有两个数据值。 Now what I want is that to make like jagged array.
现在我想要的是使它像锯齿状的数组。 That means I want a 1d array and where each element will be a linkedlist of items.
这意味着我想要一个一维数组,并且每个元素都将是项目的链表。 Is it really possible to make the below single linkedlist into an array of linkedlist, ie L[0],L[1], etc. each will be the starting of similar linkedlist.
是否真的有可能将下面的单个链表变成链表的数组,即L [0],L [1]等。每个链表都是相似链表的开始。 Then what shall I modify in the code given below so that I can form and traverse and get the values printed.
然后,我应该在下面给出的代码中进行哪些修改,以便形成并遍历并获得打印的值。
// Java Program to insert in a sorted list
class LinkedList1
{
Node head; // head of list
/* Linked list Node*/
class Node
{
int s;
int a;
Node next;
Node(int starting_time,int arrival_time) {s = starting_time; a=arrival_time;next = null; }
}
/* function to insert a new_node in a list. */
void sortedInsert(Node new_node)
{
Node current;
/* Special case for head node */
if (head == null || head.a >= new_node.a)
{
new_node.next = head;
head = new_node;
}
else {
/* Locate the node before point of insertion. */
current = head;
while (current.next != null &&
current.next.a < new_node.a)
current = current.next;
new_node.next = current.next;
current.next = new_node;
}
}
/*Utility functions*/
/* Function to create a node */
Node newNode(int s,int a)
{
Node x = new Node(s,a);
return x;
}
/* Function to print linked list */
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print("["+temp.s+","+temp.a+"] ");
temp = temp.next;
}
}
/* Drier function to test above methods */
public static void main(String args[])
{
LinkedList1 llist = new LinkedList1();
Node new_node;
new_node = llist.newNode(5,6);
llist.sortedInsert(new_node);
new_node = llist.newNode(10,2);
llist.sortedInsert(new_node);
new_node = llist.newNode(7,3);
llist.sortedInsert(new_node);
new_node = llist.newNode(3,4);
llist.sortedInsert(new_node);
new_node = llist.newNode(1,5);
llist.sortedInsert(new_node);
new_node = llist.newNode(9,1);
llist.sortedInsert(new_node);
System.out.println("Created Linked List");
llist.printList();
}
}
I have wrapped up the LinkedList1 class inside LinkedListArray and create couple of constructors and a get and insert method. 我在LinkedListArray中包装了LinkedList1类,并创建了几个构造函数以及一个get和insert方法。 Similarly, you can write other methods as required.
同样,您可以根据需要编写其他方法。 Hope this will make things clear.
希望这能使事情变得清楚。
public class LinkedListArray{
private int DEFAULT_CAPACITY=10;
private int SIZE=0;
private LinkedList1[] arr;
public LinkedListArray() {
arr=new LinkedList1[DEFAULT_CAPACITY];
}
public LinkedListArray(int capacity) {
arr=new LinkedList1[capacity];
}
public LinkedList1 insert(int index, LinkedListArray.LinkedList1.Node Node) {
if(arr[index]==null) arr[index]=new LinkedList1();
arr[index].sortedInsert(Node);;
SIZE++;
return arr[index];
}
public LinkedList1 get(int index) {
return arr[index];
}
public int size() {
return SIZE;
}
//Java Program to insert in a sorted list
class LinkedList1
{
public LinkedList1() {}
Node head; // head of list
/* Linked list Node*/
class Node
{
int s;
int a;
Node next;
Node(int starting_time,int arrival_time) {s = starting_time; a=arrival_time;next = null; }
}
/* function to insert a new_node in a list. */
void sortedInsert(Node new_node)
{
Node current;
/* Special case for head node */
if (head == null || head.a >= new_node.a)
{
new_node.next = head;
head = new_node;
}
else {
/* Locate the node before point of insertion. */
current = head;
while (current.next != null &&
current.next.a < new_node.a)
current = current.next;
new_node.next = current.next;
current.next = new_node;
}
}
/*Utility functions*/
/* Function to create a node */
Node newNode(int s,int a)
{
Node x = new Node(s,a);
return x;
}
/* Function to print linked list */
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print("["+temp.s+","+temp.a+"] ");
temp = temp.next;
}
}
/* Drier function to test above methods */
}
public static void main(String args[])
{
LinkedListArray arr=new LinkedListArray();
arr.insert(0, new LinkedListArray().new LinkedList1().newNode(5, 4));
arr.insert(0, new LinkedListArray().new LinkedList1().newNode(10, 4));
arr.insert(0, new LinkedListArray().new LinkedList1().newNode(4, 34));
System.out.println("Created Linked List and inserted in array");
arr.get(0).printList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.