Answers for "Linked list"

26

linkedlist implementation in c++

#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;
}
Posted by: Guest on March-18-2020
13

linked list in python

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())
Posted by: Guest on October-09-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
0

Linked list

#include <iostream>
using namespace std;
 


class Node {
public:
    string name;
	string surName;
	int age;
	string gender;
    Node* next;
};
 
void Insert(Node** head, Node* newNode)
{
    Node* currentNode;
    
    if (*head == NULL|| (*head)->age >= newNode->age) {
        newNode->next = *head;
        *head = newNode;
    }
    else {
        
        currentNode = *head;
        while (currentNode->next != NULL && currentNode->next->age< newNode->age) {
            currentNode = currentNode->next;
        }
        newNode->next = currentNode->next;
        currentNode->next = newNode;
    }
}
 
Node* new_node(string name,string surName,	int age,	string gender)
{
    
    Node* newNode = new Node();
 
   
    newNode->name = name;
    newNode->surName = surName;
    newNode->age = age;
    newNode->gender = gender;
    newNode->next = NULL;
 
    return newNode;
}
 


void printList(Node* head)
{
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->name << " "<<temp->surName<<"  "<<temp->age<<"  "<<temp->gender<<endl;
        temp = temp->next;
    }
}
 
 
void deleteNode(Node** head_ref, int key)
{
     
    
    Node* temp = *head_ref;
    Node* prev = NULL;
     
    
    if (temp != NULL && temp->age == key)
    {
        *head_ref = temp->next; 
        delete temp;            
        return;
    }
 
    
      else
    {
    while (temp != NULL && temp->age != key)
    {
        prev = temp;
        temp = temp->next;
    }
 
    
    if (temp == NULL)
        return;
 
   
    prev->next = temp->next;
 
    
    delete temp;
    }
}


int main()
{
    
    Node* head = NULL;
    Node* node = new_node("Donald", "Brown", 67, "M" );
    Insert(&head, node);
    node = new_node("Jason", "Travis", 90, "F");
    Insert(&head, node);
    node = new_node("Max", "Black", 27, "M");
    Insert(&head, node);
    node = new_node("Bobi", "Frank", 17, "F");
    Insert(&head, node);
   
    cout << "The list is sorted by gender in ascending order\n";
    printList(head);
    cout<<"After deleting a person who age is 17 the list becomes \n";
    deleteNode(&head, 17);
     printList(head);
     
     cout << "When a person whose is 20 is inserted the linked list becomes\n";
     node = new_node("Victoria", "Riberry", 20, "M");
    Insert(&head, node);
    printList(head);
    return 0;
}
Posted by: Guest on October-04-2021
0

linked list

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

    def __repr__(self):
        return self.data

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

    def __repr__(self):
        node = self.head
        nodes = []
        while node is not None:
            nodes.append(node.data)
            node = node.next
        nodes.append("None")
        return " -> ".join(nodes)
Posted by: Guest on September-27-2021
0

linked lists

node_t * head = NULL;
head = (node_t *) malloc(sizeof(node_t));
if (head == NULL) {
    return 1;
}

head->val = 1;
head->next = NULL;
Posted by: Guest on June-08-2021

Browse Popular Code Answers by Language