'What to push as arguments for Node head in Sorted Insert(sortedIn() method) in Singly Linked List in java
I have passed a parameter 'head' to my sortedIn method which inserts in a sorted manner in a linked list, and geeks4geeks states it as, we have to pass the head as a parameter to this function. I did so, but then I don't know how and what do I pass in the main method as argument for the sortedIn method. Here is the code attached... And kindly excuse me if I haven't followed any rule as this is my first question here.
// EZ reference problems at method from line 69 and argument passing at line 160...
// Have always faced an issue where we are to pass arguments like this
class LL {
Node head;
private int size;
class Node {
int data;
Node ref;
Node(int data) {
this.data = data;
this.ref = null;
size++;
}
}
public void push(int data) {
Node newnode = new Node(data);
if (head == null) {
head = newnode;
return;
}
newnode.ref = head;
head = newnode;
}
public void append(int data) {
Node newnode = new Node(data);
if (head == null) {
head = newnode;
return;
}
Node curr = head;
while (curr.ref != null) {
curr = curr.ref;
}
curr.ref = newnode;
}
public void insert(int data, int pos_data) {
Node newnode = new Node(data);
if (head == null) {
head = newnode;
return;
}
Node curr = head;
while (curr.ref != null) {
if (curr.data == pos_data)break;
curr = curr.ref;
}
newnode.ref = curr.ref;
curr.ref = newnode;
}
public void inatp(int data, int p) {
Node newnode = new Node(data);
if (head == null) {
head = newnode;
return;
}
Node curr = head;
if (p == 1) {
newnode.ref = head;
head = newnode;
return;
}
for (int i = 0; i < p-2; i++){
curr = curr.ref;
}
newnode.ref = curr.ref;
curr.ref = newnode;
}
// Here is the sortedInsert method where i have pushed Node head as parameter but am confused to as what to pass as argument!!!
public Node sortedIn(Node head, int data) {
Node newnode = new Node(data);
if (head == null) {
head = newnode;
return newnode;
}
else if (head.ref == null) {
newnode.ref = head;
head = newnode;
return newnode;
}
else {
Node curr = head;
while (curr.ref != null && curr.ref.data < data) {
curr = curr.ref;
}
newnode.ref = curr.ref;
curr.ref = newnode;
return head;
}
}
public void delf() {
if (head == null) return;
size--;
if (head.ref == null) {
head.ref = null;
}
head = head.ref.ref;
}
public void dell() {
if (head == null) return;
size--;
if (head.ref == null) {
head = null;
return;
}
Node curr = head;
while (curr.ref!=null) {
if (curr.ref.ref == null)break;
curr = curr.ref;
}
curr.ref = null;
}
public void delm(int pos) {
if (head == null) return;
size--;
if (head.ref == null) {
head = null;
return;
}
Node curr = head;
while (curr.ref != null) {
if (curr.ref.data == pos) break;
curr = curr.ref;
}
curr.ref = curr.ref.ref;
}
public int find(int posdata) {
int c = 0;
Node curr = head;
while (curr!= null) {
if (curr.data == posdata) return c;
curr = curr.ref;
c++;
}
return -1;
}
public void size() {
System.out.println(size);
}
public void p() {
if (head == null) {
System.out.println("Empty");
return;
}
Node curr = head;
while (curr != null) {
System.out.print(curr.data + "==>");
curr = curr.ref;
}
System.out.println();
}
public static void main(String[] args) {
LL l = new LL();
l.push(1);
l.push(2);
l.append(3);
l.p();
System.out.println(l.find(1));
l.inatp(4, 4);
l.p();
l.sortedIn(????, 5); // what do i push as argument for sortedIn method as Node head parameter's argument?
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
