In: Computer Science
Write a program of linked list in which in which element can be inserted only at the end of the linked list and deletion can also take place at the end.
Code needed in java.
Note=> I have made the program in which main will call 'insert' function so as to insert the element at the last node and main only will call 'delete' function so as to delete the last node.
Code :
import java.util.*;
class Node { // class for node pointer of linked list
int data;
Node next;
Node (int d)
{
data=d;
next=null;
}
}
class link_list {
static Node head;
public static void main(String arg[]){
Scanner sc=new Scanner (System.in);
System.out.print("Do you want to perform insertion or deletion
enter 1 for yes and 0 for No: ");
int n=sc.nextInt();
System.out.println(n);
head=null;
while (n!=0)
{
System.out.print("press 1 for insertion and 0 for deletion:
");
int op=sc.nextInt();
System.out.println(op);
if (op==1)
{ System.out.print("Enter no to be inserted: ");
int d=sc.nextInt();
System.out.println(d);
insert (d);
}
else
delete();
System.out.println ("After above operation: Elements in list will
be like that ");
Node node=head;
while (node!=null)
{ System.out.print(node.data+" ");node=node.next;}
System.out.println();
System.out.print("Do you want to perform insertion or deletion
enter 1 for yes and 0 for No: ");
n=sc.nextInt();
System.out.println(n);
}
}
public static void insert( int d){
Node node =new Node (d); //creating a object of type Node
node.next=head; // We are pointing the next of this element to head
so that it becomec last element
head=node; // Now making it as a head only
}
public static void delete (){
if (head==null) // if the head is null then we cannot delete any
element
{System.out.println("Element cannot be deleted as list is
empty");
return;
}
head=head.next; // deleting the last node by pointing the head to
its next
}
}
Here, The main functions are 'insert' and 'delete' that are
inserting and deleting the element at the end of link list.
Note : => Here, head pointer points to last node of linked
list.
'insert' function : It will first create the
object of type Node of the element that we want to insert then it
will make its next to point to the head pointer. And after that we
will make this object as head only. So new element will become head
and we know that head pointer is the last node. So, in this way new
node is inserted at the last.
So in our linked list, last node points to second lasst node,
second last node points to third last node and so on .
'delete' function : As we know that head is the
last node so we will free the memory of head pointer and we will
make head pointer to point its next element.
Output :
Do you want to perform insertion or deletion enter 1 for yes and 0 for No: 1 press 1 for insertion and 0 for deletion: 1 Enter no to be inserted: 2 After above operation: Elements in list will be like that 2 Do you want to perform insertion or deletion enter 1 for yes and 0 for No: 1 press 1 for insertion and 0 for deletion: 1 Enter no to be inserted: 3 After above operation: Elements in list will be like that 3 2 Do you want to perform insertion or deletion enter 1 for yes and 0 for No: 1 press 1 for insertion and 0 for deletion: 1 Enter no to be inserted: 4 After above operation: Elements in list will be like that 4 3 2 Do you want to perform insertion or deletion enter 1 for yes and 0 for No: 1 press 1 for insertion and 0 for deletion: 0 After above operation: Elements in list will be like that 3 2 Do you want to perform insertion or deletion enter 1 for yes and 0 for No: 0 Here, we can se that we have linked list as 3,2. And now the element is inserted so link list will become 4, 3, 2. So, 4 is the elemnt that is inserted last. After, performing the delete operation link list becomes 3,2 which means 4 is deleted and we know that 4 is the last element that was inserted. Therefore, it delete the element from last .