Question

In: Computer Science

Write a program of linked list in which in which element can be inserted only at...

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.

Solutions

Expert Solution

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 .

Related Solutions

1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
I want the linked list to be inserted by the user and donot use getline to...
I want the linked list to be inserted by the user and donot use getline to insert it #include <bits/stdc++.h> using namespace std;    // A linked list Node struct Node {     int data;     struct Node* next; };    // Size of linked list int size = 0;    // function to create and return a Node Node* getNode(int data) {     // allocating space     Node* newNode = new Node();        // inserting the required data     newNode->data = data;     newNode->next =...
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
C++ Write a program to create a linked list which stores the details of employees(Employee number,...
C++ Write a program to create a linked list which stores the details of employees(Employee number, employee name, rate, hours worked). Create a menu to manage the emoployee data. MENU 1. ADD EMPLOYEE DETAILS 2. DELETE EMPLOYEE 3. SEARCH EMPLOYEE 4. PRINT EMPLOYEE PAYROLL 5. EXIT When the user selected option #4 the program should print the following pay report for each employee: EmpNo.     Name      Rate    Hours    Regular Pay      Overtime Pay     Gross Pay Any hours worked above 40 hours are...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
In java. Using a linked list only, iterator and scanner. Develop a program to maintain a...
In java. Using a linked list only, iterator and scanner. Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method: Add a new assignment. (3 pts) Remove an assignment. (3pts) Provide a list of the assignments in...
Outcomes: • Write a Java program that implements linked list algorithms can u also show thee...
Outcomes: • Write a Java program that implements linked list algorithms can u also show thee testing code -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- This is the starter code import java.util.NoSuchElementException; // Put your prologue comments here public class LinkedAlgorithms {       private class Node {        private String data;        private Node next;        private Node(String data) {            this.data = data;            this.next = null;        }    }    public Node head;   ...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
get the minimum element from linked list c++
get the minimum element from linked list c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT