"linked list" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "linked list" answered properly. Developers are finding an appropriate answer about linked list related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like linked list. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on linked list. 

linkedlist implementation in c++

By Important ImpalaImportant Impala on Mar 18, 2020
#include <iostream>

using namespace std;

struct node
{
    int data;
    node *next;
};

class linked_list
{
private:
    node *head,*tail;
public:
    linked_list()
    {
        head = NULL;
        tail = NULL;
    }

    void add_node(int n)
    {
        node *tmp = new node;
        tmp->data = n;
        tmp->next = NULL;

        if(head == NULL)
        {
            head = tmp;
            tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
    }
};

int main()
{
    linked_list a;
    a.add_node(1);
    a.add_node(2);
    return 0;
}

Add Comment

21

linked list in python

By BiobopBiobop on Oct 09, 2020
class Node:
    def __init__(self, data = None, next_node = None):
        self.data = data
        self.nextNode = next_node

    def get_data(self):
        return self.data

    def set_data(self, data):
        self.data = data

    def get_nextNode(self):
        return self.nextNode

    def set_nextNode(self, nextNode):
        self.nextNode = nextNode


class LinkedList:
    def __init__(self, head = None):
        self.head = head


    def add_Node(self, data):
        # if empty
        if self.head == None:
            self.head = Node(data)


        # not empty
        else:
            curr_Node = self.head
            
            # if node added is at the start
            if data < curr_Node.get_data():
                self.head = Node(data, curr_Node)
                
            # not at start
            else:
                while data > curr_Node.get_data() and curr_Node.get_nextNode() != None:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()

                # if node added is at the middle
                if data < curr_Node.get_data():
                    prev_Node.set_nextNode(Node(data, curr_Node))
                

                # if node added is at the last
                elif data > curr_Node.get_data() and curr_Node.get_nextNode() == None:
                    curr_Node.set_nextNode(Node(data))



    def search(self, data):
        curr_Node = self.head
        while curr_Node != None:
            if data == curr_Node.get_data():
                return True

            else:
                curr_Node = curr_Node.get_nextNode()

        return False


    def delete_Node(self, data):
        if self.search(data):
            # if data is found

            curr_Node = self.head
            #if node to be deleted is the first node
            if curr_Node.get_data() == data:
                self.head = curr_Node.get_nextNode()

            else:
                while curr_Node.get_data() != data:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()
                    
                #node to be deleted is middle
                if curr_Node.get_nextNode() != None:
                    prev_Node.set_nextNode(curr_Node.get_nextNode())

                # node to be deleted is at the end
                elif curr_Node.get_nextNode() == None:
                    prev_Node.set_nextNode(None)

        else:
            return "Not found."

    def return_as_lst(self):
        lst = []
        curr_Node = self.head
        while curr_Node != None:
            lst.append(curr_Node.get_data())
            curr_Node = curr_Node.get_nextNode()

        return lst

    def size(self):
        curr_Node = self.head
        count = 0
        while curr_Node:
            count += 1
            curr_Node = curr_Node.get_nextNode()
        return count

      
## TEST CASES #
test1 = LinkedList()
test2 = LinkedList()
test1.add_Node(20)
test1.add_Node(15)
test1.add_Node(13)
test1.add_Node(14)
test1.delete_Node(17)
print(test1.return_as_lst())
print(test2.size())

Add Comment

10

Linked List implementation

By NimorumNimorum on Jun 08, 2020
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;
        }
    }

}

Add Comment

3

linked lists

By Gentle GharialGentle Gharial on Jun 08, 2021
node_t * head = NULL;
head = (node_t *) malloc(sizeof(node_t));
if (head == NULL) {
    return 1;
}

head->val = 1;
head->next = NULL;

Source: www.learn-c.org

Add Comment

0

All those coders who are working on the C++ based application and are stuck on linked list can get a collection of related answers to their query. Programmers need to enter their query on linked list related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about linked list for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of linked list while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "linked list". Visit this developer's friendly online web community, CodeProZone, and get your queries like linked list resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "linked list"

View All C++ queries

C++ queries related to "linked list"

c++ linked list clear cpp linked list linked list insertion in c++ circular queue using linked list in c++ linked list in c++ stl infix to prefix using cpp linked list program linked list in c++ using class insert delete display in array menu driven program to delete in linked list linked list operations linked list class c++ basic implementation print circular linked list c++ reverse a linked list linked list how to reverse a linked list reverse linked list list conda environments how to print list in c++ grocery shopping list c++ chegg c++ remove multiple items from list delete a head node in link list how to make a list in c++ traverse through list c++ cpp std list example how to get an element in a list c++ list in cpp member initializer list in c++ initialization list c++ how to write C++ list c++ append to list c++ class member initializer list c++ remove item from list c++ com port list list in c++ stl c++ iterate through constant list list clear c++ list of products on e commerce websites c++ list pop back Given the following declarations below. Write a loop to read a list of numbers from the keyboard terminated by -999 and store the even numbers (skip over the odd numbers) in the vector v. clean list widget qt error: invalid use of template-name without an argument list qt widget list set selected c++ initialization list C++ drop last element of list c++ argument list for class template is missing attack on titan junior high list of episodes initializer list c++ list stl conda list environments how to show list of conda packages adjacency list representation of graph c++ list add

Browse Other Code Languages

CodeProZone