Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- class ListElement{
- public int data;
- public ListElement link;
- public ListElement list1,list2;
- Scanner sc = new Scanner(System.in);
- //construtor
- ListElement(int data){
- this.data = data;
- this.link = null;
- }
- //method to update data in List element
- void updatedata(int data){
- this.data = data;
- }
- //method to setup the Link
- void setLink(ListElement node){
- this.link = node;
- }
- int getdata(){
- return this.data;
- }
- ListElement getNextNode(){
- return this.link;
- }
- ListElement insert(ListElement head,ListElement node,int position){
- ListElement previousNode = new ListElement(head.data);
- previousNode.setLink(head.link);
- if(head == null){
- return node;
- }
- else{
- int size = length();
- if(position > size+1){
- System.out.println("Invalid position.");
- System.out.println("Valid positions are from 0 to " + size);
- return head;
- }
- if(position == 1){
- node.setLink(head);
- return node;
- }
- else if(position == 2){
- ListElement temp = new ListElement(-11);
- temp.setLink(head.link);
- head.setLink(node);
- node.setLink(temp.getNextNode());
- }
- else{
- int pos = 1;
- while(pos != position-1){
- previousNode = previousNode.getNextNode();
- pos++;
- }
- //ListElement temp = head.getNextNode();
- previousNode.setLink(node);
- node.setLink(null);
- }
- }
- return head;
- }
- ListElement insertAtEnd(ListElement node){
- ListElement head = this;
- int size = head.length();
- ListElement temp = this;
- while(temp.link!=null){
- temp = temp.getNextNode();
- }
- temp.setLink(node);
- return head;
- }
- void display(ListElement head){
- ListElement node = head;
- int position = 1;
- while(node!= null){
- System.out.println("Position " + position + " = " + node.data);
- position++;
- node = node.getNextNode();
- }
- }
- ListElement makeDummyList() {
- ListElement head = this;
- for(int i=2 ; i < 10 ; i++){
- ListElement node = new ListElement(i);
- head.insert(head,node,i);
- }
- return head;
- }
- }
- public class LinkedList {
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- ListElement head = new ListElement(1);
- head.setLink(null);
- int cont =1;
- head = head.makeDummyList();
- while(cont == 1){
- System.out.println("Program to implement linked list");
- System.out.println("The Following choices are availiable : ");
- System.out.println("1.Insert element in Linked List");
- System.out.println("2.Delete element from Linked List");
- System.out.println("3.Display Linked List");
- System.out.println("0.Exit");
- System.out.print("Enter Your choice :");
- int choice = sc.nextInt();
- switch(choice){
- case 1:
- System.out.print("Enter data : ");
- int data = sc.nextInt();
- System.out.print("Enter position : ");
- int position = sc.nextInt();
- ListElement node = new ListElement(data);
- if(head == null){
- head = node;
- }
- else{
- int length = head.length();
- head = head.insert(head,node,position);
- }
- break;
- case 2:
- System.out.println("There are two choices :");
- System.out.println("1.Delete by data");
- System.out.println("2.Delete by position");
- System.out.print("Enter Your choice :");
- int choices = sc.nextInt();
- switch (choices){
- case 1:
- System.out.print("Enter the data to be deleted : ");
- int deleteData = sc.nextInt();
- head = head.deleteByData(head,deleteData);
- break;
- case 2:
- System.out.print("Enter position of element to be deleted : ");
- int pos = sc.nextInt();
- head = head.deleteByPosition(head, pos);
- break;
- }
- break;
- case 3:
- head.display(head);
- break;
- case 0:
- cont = 0;
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment