Question: Draw memory map of this link list code and add insert at last ( java): class listnodes { int data; listnodes link; listnodes() { data
Draw memory map of this link list code and add insert at last ( java):
class listnodes { int data; listnodes link;
listnodes() { data = 0; link = null; }
listnodes(int d, listnodes l) { data = d; link = l; } } class singlelinkedlist { public listnodes insertnode(int data, listnodes head) {
listnodes newnode = new listnodes(data, null);
newnode.link = head;
head = newnode; return head; }
public listnodes insertAtPosition(listnodes head, int data, int position) { listnodes newnode = new listnodes(data, null); listnodes previous = head; int count = 1; while (count <= position - 1) { previous = previous.link; count++; }
listnodes current = previous.link; newnode.link = current; previous.link = newnode; return head; }
public listnodes deletefirst(listnodes head) { listnodes pre = head; head = head.link; pre.link = null; return pre; }
public listnodes deletelast(listnodes head) { listnodes cur = head; listnodes previouscur = head; while (cur.link != null) { previouscur = cur; cur = cur.link; } previouscur.link = null; return cur; }
public int length(listnodes head) { listnodes current = head; int c = 0; while (current != null) { c++; current = current.link; } return c; }
public boolean find(listnodes head, int searchdata) { listnodes cur = head; while (cur != null) { if (cur.data == searchdata) { return true; } cur = cur.link; } return false; }
public void display(listnodes head) { listnodes current = head; while (current.link != null) { System.out.print(current.data + "-->"); current = current.link; } System.out.print(current.data); }
public listnodes insertAtEnd(listnodes head, int data) { listnodes newnode = new listnodes(data, null); if (head == null) { return newnode; } listnodes current = head; while (current.link != null) { current = current.link; } current.link = newnode; return head; } } public class LinkedList {
public static void main(String[] args) { listnodes head=new listnodes(10,null); singlelinkedlist sl=new singlelinkedlist(); sl.display(head);
System.out.println(" *****Insert at front*****"); listnodes newhead=sl.insertnode(20, head); sl.display(newhead);
System.out.println(" *****Insert at a position*****"); sl.insertAtPosition(newhead, 40, 1); sl.display(newhead);
System.out.println(" *****Delete at front*****"); listnodes deletednode=sl.deletefirst(newhead); System.out.println("Deleted node is: "+deletednode.data); sl.display(newhead);
System.out.println(" *****Delete at end*****"); listnodes deletedlast=sl.deletelast(newhead); System.out.println("Deleted node is: "+deletedlast.data); sl.display(newhead);
System.out.println(" *****Length of Linked List*****"); int length=sl.length(newhead); System.out.println("Length of Linked List is: "+length);
System.out.println(" *****Search for an element*****"); boolean found=sl.find(newhead, 20); if(found){ System.out.println("Element found"); } else{ System.out.println("Element not found"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
