Answers for "linked list methods"

3

Linked List implementation

public class LinkedList {

    private Node head;
    private int length = 0;

    public LinkedList() {
        this.head = new Node(null);
    }

    public int size() {
        return length;
    }


     // Adds an element to the end of the list
    public void add(Object data)  {

        Node node = new Node(data);
        Node iterator = head;
        while (iterator.getNext() != null){
            iterator = iterator.getNext();
        }
        iterator.setNext(node);
        length++;
    }


     // Obtains an element by index
    public Object get(int index) {

        if (head.getNext() == null || index >= length){
            return null;
        }

        Node iterator = head.getNext();
        int counter = 0;

        while(counter < index){

            iterator = iterator.getNext();
            counter++;
        }
        return iterator.getData();

    }


     // Returns the index of the element in the list
    public int indexOf(Object data) {
        Node obj=head;
        for (int i = 0; i < length; i++) {
            obj = obj.getNext();
            if (obj.getData().equals(data)) {
                return i;
            }
        }
        return -1;
        //throw new Exception("Data not found");
    }


     // Removes an element from the list
    public boolean remove(Object data) {

        if (head.getNext() == null){
            return false;
        }

        Node iterator = head;

        while(iterator.getNext() != null){

            if (iterator.getNext().getData().equals(data)){
                iterator.setNext(iterator.getNext().getNext());
                length--;
                return true;
            }

            iterator = iterator.getNext();
        }

        return false;
    }

    private class Node {

        private Object data;
        private Node next;

        public Node(Object data) {
            this.data = data;
            next = null;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

}
Posted by: Guest on June-08-2020
3

Linked List implementation

public class LinkedList {

    private Node head;
    private int length = 0;

    public LinkedList() {
        this.head = new Node(null);
    }

    public int size() {
        return length;
    }


     // Adds an element to the end of the list
    public void add(Object data)  {

        Node node = new Node(data);
        Node iterator = head;
        while (iterator.getNext() != null){
            iterator = iterator.getNext();
        }
        iterator.setNext(node);
        length++;
    }


     // Obtains an element by index
    public Object get(int index) {

        if (head.getNext() == null || index >= length){
            return null;
        }

        Node iterator = head.getNext();
        int counter = 0;

        while(counter < index){

            iterator = iterator.getNext();
            counter++;
        }
        return iterator.getData();

    }


     // Returns the index of the element in the list
    public int indexOf(Object data) {
        Node obj=head;
        for (int i = 0; i < length; i++) {
            obj = obj.getNext();
            if (obj.getData().equals(data)) {
                return i;
            }
        }
        return -1;
        //throw new Exception("Data not found");
    }


     // Removes an element from the list
    public boolean remove(Object data) {

        if (head.getNext() == null){
            return false;
        }

        Node iterator = head;

        while(iterator.getNext() != null){

            if (iterator.getNext().getData().equals(data)){
                iterator.setNext(iterator.getNext().getNext());
                length--;
                return true;
            }

            iterator = iterator.getNext();
        }

        return false;
    }

    private class Node {

        private Object data;
        private Node next;

        public Node(Object data) {
            this.data = data;
            next = null;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

}
Posted by: Guest on June-08-2020
19

java linked list functions

import java.util.LinkedList;
LinkedList<Integer> myList = new LinkedList<Integer>();
myList.add(0);
myList.remove(0);//Remove at index 0
myList.size();
myList.get(0);//Return element at index 0
Posted by: Guest on June-24-2020
19

java linked list functions

import java.util.LinkedList;
LinkedList<Integer> myList = new LinkedList<Integer>();
myList.add(0);
myList.remove(0);//Remove at index 0
myList.size();
myList.get(0);//Return element at index 0
Posted by: Guest on June-24-2020
0

linked list operations

/* Given a Singly linked list L0->L1->L2->L3->L4->.......;Write a program to rearrange the nodes in the list so that the newly
    formed list is :L0->Ln->L1->Ln-1->L2->Ln-2->.....
    Sample input: 1 2 3 4 5 6
    Sample output:1 6 2 5 3 4
*/

#include<iostream>
using namespace std;
struct node
{
    int data;
    node* next;
};
node *head=new node;
node *head1,*head2;
void dummy(int s)
{
    node *temp=new node;
    temp->data=s;
    temp->next=NULL;
    head=temp;
}
void insert_ll(int k)//Inserting into the list
{
    node* temp=new node;
    temp->data=k;
    temp->next=NULL;
    if(head==NULL)
    {
        head=temp;
    }
    else
    {
        node* ptr=head;
        while(ptr->next!=NULL)
        {
            ptr=ptr->next;
        }
        ptr->next=temp;
    }
}
void split()//Splitting the list into two halves
{
    node* slow=head;
    node* fast=head->next;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
    }
    head1=head;
    head2=slow->next;
    slow->next=NULL;
}
void reversing()//Reversing the list
{
    node* current=head2;
    node* prev=NULL;
    node*temp;
    while(current)
    {
        temp=current->next;
        current->next=prev;
        prev=current;
        current=temp;
    }
    head2=prev;
}
void merge__ll()//Merging the list
{
    dummy(0);
    node*current=head;
    while(head1||head2)
    {
        if(head1)
        {
            current->next=head1;
            current=current->next;
            head1=head1->next;
        }
        if(head2)
        {
            current->next=head2;
            current=current->next;
            head2=head2->next;
        }
    }
    head=head->next;
}
void print()//Printing the list
{
    node*temp =head;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
}
int main()
{
    head=NULL;
    int n;
    cout<<"Enter the size:"<<endl;//for this question we will consider size though linked list is dynamic and does not have a fixed size
    cin>>n;
    int x;
    cout<<"enter the elements:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        insert_ll(x);
    }
    cout<<"Linked list without the modification is:";
    print();
    split();
    reversing();
    merge__ll();
    print();
    return 0;
}
Posted by: Guest on July-22-2021
0

linked list operations

/* Given a Singly linked list L0->L1->L2->L3->L4->.......;Write a program to rearrange the nodes in the list so that the newly
    formed list is :L0->Ln->L1->Ln-1->L2->Ln-2->.....
    Sample input: 1 2 3 4 5 6
    Sample output:1 6 2 5 3 4
*/

#include<iostream>
using namespace std;
struct node
{
    int data;
    node* next;
};
node *head=new node;
node *head1,*head2;
void dummy(int s)
{
    node *temp=new node;
    temp->data=s;
    temp->next=NULL;
    head=temp;
}
void insert_ll(int k)//Inserting into the list
{
    node* temp=new node;
    temp->data=k;
    temp->next=NULL;
    if(head==NULL)
    {
        head=temp;
    }
    else
    {
        node* ptr=head;
        while(ptr->next!=NULL)
        {
            ptr=ptr->next;
        }
        ptr->next=temp;
    }
}
void split()//Splitting the list into two halves
{
    node* slow=head;
    node* fast=head->next;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
    }
    head1=head;
    head2=slow->next;
    slow->next=NULL;
}
void reversing()//Reversing the list
{
    node* current=head2;
    node* prev=NULL;
    node*temp;
    while(current)
    {
        temp=current->next;
        current->next=prev;
        prev=current;
        current=temp;
    }
    head2=prev;
}
void merge__ll()//Merging the list
{
    dummy(0);
    node*current=head;
    while(head1||head2)
    {
        if(head1)
        {
            current->next=head1;
            current=current->next;
            head1=head1->next;
        }
        if(head2)
        {
            current->next=head2;
            current=current->next;
            head2=head2->next;
        }
    }
    head=head->next;
}
void print()//Printing the list
{
    node*temp =head;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
}
int main()
{
    head=NULL;
    int n;
    cout<<"Enter the size:"<<endl;//for this question we will consider size though linked list is dynamic and does not have a fixed size
    cin>>n;
    int x;
    cout<<"enter the elements:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        insert_ll(x);
    }
    cout<<"Linked list without the modification is:";
    print();
    split();
    reversing();
    merge__ll();
    print();
    return 0;
}
Posted by: Guest on July-22-2021
0

linked list operations

// the function is returning the head of the singly linked list
Node insertAtEnd(Node head, int val)
{
    if( head == NULL ) // handing the special case
    {
        newNode = new Node(val)
        head = newNode
        return head
    }
    Node temp = head
    // traversing the list to get the last node
    while( temp.next != NULL )
    {
        temp = temp.next
    }
    newNode = new Node(val)
    temp.next = newNode
    return head
}
Posted by: Guest on April-28-2021
0

linked list operations

// the function is returning the head of the singly linked list
Node insertAtEnd(Node head, int val)
{
    if( head == NULL ) // handing the special case
    {
        newNode = new Node(val)
        head = newNode
        return head
    }
    Node temp = head
    // traversing the list to get the last node
    while( temp.next != NULL )
    {
        temp = temp.next
    }
    newNode = new Node(val)
    temp.next = newNode
    return head
}
Posted by: Guest on April-28-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language