insertion in doubly linked list in c++ code example

Example 1: linked list insertion in c++

#include <iostream>
// Linked list
struct node {
    int data ;
    node * link;
};


node * Node(int data) {
    node * temp = new node();
    temp->data = data;
    temp->link = NULL;
    return temp;
}

void append(node ** head, int data) {

    if(*head == NULL) {
        *head =  Node(data);
    }else {
        node * temp = * head;
        while (temp->link != NULL) {
            temp=temp->link;
        }
        temp->link = Node(data);

    }

}


// insertion at begining

void insertBeg(node **head , int data) {
    if(*head == NULL) {
        * head = Node(data);
    }else {
        node * temp = Node(data);
        temp->link = *head;
        *head = temp;

    }


}
// insert at specific position

void addafter(node * head , int loc , int data) {
    node * temp , * r ;
    temp = head ;
    for( int i = 0 ; i<loc;i++ ) {
        temp = temp->link;
        if(temp == NULL) {
            cout<<"there ar less elemtns" ;
            return;
        }

    }
    // insert new node
    r = Node(data);
    r->link = temp->link;
    temp->link = r;


}

void display(node * head) {

    node * temp = head;
    while(temp!= NULL) {
        cout<<temp->data<<" ";
        temp = temp->link;
    }
}



int main() {
    node * head = NULL;
    append(&head,5);
    append(&head,5);
    append(&head,5);
    append(&head,5);
    display(head);
    cout<<endl;
    insertBeg(&head,6);
    insertBeg(&head,6);
    insertBeg(&head,6);
    display(head);
addafter(head,4,7);
cout<<endl;
display(head);

    return 0;
}

Example 2: doubly linked list python

class ListNode:
    def __init__(self, value, prev=None, next=None):
        self.prev = prev
        self.value = value
        self.next = next

class DoublyLinkedList:
    def __init__(self, node=None):
        self.head = node
        self.tail = node
        self.length = 1 if node is not None else 0

    def __len__(self):
        return self.length
   
  	def add_to_head(self, value):
        new_node = ListNode(value, None, None)
        self.length += 1
        if not self.head and not self.tail:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head.prev = new_node
            self.head = new_node
 
    def remove_from_head(self):
        value = self.head.value
        self.delete(self.head)
        return value

    def add_to_tail(self, value):
        new_node = ListNode(value, None, None)
        self.length += 1
        if not self.tail and not self.head:
            self.tail = new_node
            self.head = new_node
        else:
            new_node.prev = self.tail
            self.tail.next = new_node
            self.tail = new_node
            

    def remove_from_tail(self):
        value = self.tail.value
        self.delete(self.tail)
        return value
            
    def move_to_front(self, node):
        if node is self.head:
            return
        value = node.value
        if node is self.tail:
            self.remove_from_tail()
        else:
            node.delete()
            self.length -= 1
        self.add_to_head(value)
        
    def move_to_end(self, node):
        if node is self.tail:
            return
        value = node.value
        if node is self.head:
            self.remove_from_head()
            self.add_to_tail(value)
        else:
            node.delete()
            self.length -= 1
            self.add_to_tail(value)

    def delete(self, node):
        self.length -= 1
        if not self.head and not self.tail:
            return
        if self.head == self.tail:
            self.head = None
            self.tail = None
        elif self.head == node:
            self.head = node.next
            node.delete()
        elif self.tail == node:
            self.tail = node.prev
            node.delete()
        else:
            node.delete()

    def get_max(self):
        if not self.head:
            return None
        max_val = self.head.value
        current = self.head
        while current:
            if current.value > max_val:
                max_val = current.value
            current = current.next
        return max_val

Tags:

Java Example