linkedlist
class LinkedList { constructor(head = null) { this.head = head } } class ListNode { constructor(value,next=null){ this.value = value; this.next = next; }; } let node1 = new ListNode(2); let node2 = new ListNode(4); node1.next = node2; let list = new LinkedList(node1); console.log(list.head.next);
Source: ulvimmeedov.herokuapp.com
linked list
// C++ program to convert a given Binary Tree to Doubly Linked List #include <bits/stdc++.h> // Structure for tree and linked list struct Node { int data; Node *left, *right; }; // Utility function for allocating node for Binary // Tree. Node* newNode(int data) { Node* node = new Node; node->data = data; node->left = node->right = NULL; return node; } // A simple recursive function to convert a given // Binary tree to Doubly Linked List // root --> Root of Binary Tree // head --> Pointer to head node of created doubly linked list void BToDLL(Node* root, Node*& head) { // Base cases if (root == NULL) return; // Recursively convert right subtree BToDLL(root->right, head); // insert root into DLL root->right = head; // Change left pointer of previous head if (head != NULL) head->left = root; // Change head of Doubly linked list head = root; // Recursively convert left subtree BToDLL(root->left, head); } // Utility function for printing double linked list. void printList(Node* head) { printf("Extracted Double Linked list is:\n"); while (head) { printf("%d ", head->data); head = head->right; } } // Driver program to test above function int main() { /* Constructing below tree 5 / \ 3 6 / \ \ 1 4 8 / \ / \ 0 2 7 9 */ Node* root = newNode(5); root->left = newNode(3); root->right = newNode(6); root->left->left = newNode(1); root->left->right = newNode(4); root->right->right = newNode(8); root->left->left->left = newNode(0); root->left->left->right = newNode(2); root->right->right->left = newNode(7); root->right->right->right = newNode(9); Node* head = NULL; BToDLL(root, head); printList(head); return 0; }
Source: www.geeksforgeeks.org
linked list
#include<bits/stdc++.h> using namespace std; class node { public: int num; node* next; node(int val) { num = val; next = NULL; } }; void insertNode(node* head,int val) { node* newNode = new node(val); if(head == NULL) { head = newNode; return; } node* temp = head; while(temp->next != NULL) temp = temp->next; temp->next = newNode; return; } node* reverse(node* ptr) { node* pre=NULL; node* nex=NULL; while(ptr!=NULL) { nex = ptr->next; ptr->next = pre; pre=ptr; ptr=nex; } return pre; } bool isPalindrome(node* head) { if(head==NULL||head->next==NULL) return true; node* slow = head; node* fast = head; while(fast->next!=NULL&&fast->next->next!=NULL) { slow = slow->next; fast = fast->next->next; } slow->next = reverse(slow->next); slow = slow->next; node* dummy = head; while(slow!=NULL) { if(dummy->num != slow->num) return false; dummy = dummy->next; slow = slow->next; } return true; } int main() { node* head = NULL; insertNode(head,1); insertNode(head,2); insertNode(head,3); insertNode(head,2); insertNode(head,1); isPalindrome(head)? cout<<"True" : cout<<"False"; return 0; }
Source: takeuforward.org
Linked List
"""Linear Linked List Included: - put - sort - delete - find - display """ # Linear Linked List class Node: def __init__(self, val, next_=None): self.val = val self.next = next_ class LLL: def __init__(self, start=None): self.head = self.tail = start def empty(self): return self.head is None def put(self, data): # Create a new node new_node = Node(data) # Empty: assign head and tail to new node if self.empty(): self.head = self.tail = new_node else: # Not Empty: new node added after tail, becomes new tail self.tail.next = new_node self.tail = new_node def sort_it(self): if self.empty(): return # Bubblesort curr = self.head while curr: temp = curr.next while temp: if curr.val > temp.val: curr.val, temp.val = temp.val, curr.val temp = temp.next curr = curr.next def delete(self, val): if self.empty(): return f"Empty!" curr, prev = self.head, None # Update curr and prev until find item while curr and val > curr.val: prev, curr = curr, curr.next # Found if curr and val == curr.val: if curr == self.head: self.head = self.head.next else: prev.next = curr.next return f"Del: {val}" # Not found return f"No: {val}" def find(self, val): curr = self.head # Update curr until item found while curr and val > curr.val: curr = curr.next # Found if curr and val == curr.val: return True # Not found return False def size(self): count = 0 curr = self.head # Loop through all nodes and count while curr: count += 1 curr = curr.next return count def display(self): nodes = [] curr = self.head # Loop through all filled nodes while curr: nodes.append(curr.val) curr = curr.next return nodes # Test from random import randint def LLL_Test(LLL): # Create random lists r = randint(15, 20) test = [randint(10, 100) for _ in range(r)] a, b = len(test) // 3, -3 notinlst, inlst, after_empty = test[:a], test[a:b], test[b:] print(f"test: {test}", f"notinlst: {notinlst}", f"inlst: {inlst}", f"after_empty: {after_empty}", sep = "\n", end = "\n\n") # Create empty LLL LLL = LLL() # Put items from inlst into LLL, and sort LLL for num in inlst: LLL.put(num) LLL.sort_it() print(f"Size: {LLL.size()}", f"Data ( ): ", f" ", f"LLL: {LLL.display()}", f"{LLL.display() == sorted(inlst)}", sep = " | ", end = "\n\n") ## Test notinlst print("notinlst:") for num in notinlst: print(f"Size: {LLL.size()}", f"Data ({num}): {LLL.find(num)}", f"{LLL.delete(num)}", f"LLL: {LLL.display()}", sep = " | ") print() # Test inlst print("inlst:") for num in inlst: print(f"Size: {LLL.size()}", f"Data ({num}): {LLL.find(num)}", f"{LLL.delete(num)}", f"LLL: {LLL.display()}", sep = " | ") print() # Test after_empty print("after_empty:") for num in after_empty: print(f"Size: {LLL.size()}", f"Data ({num}): {LLL.find(num)}", f"{LLL.delete(num)}", f"LLL: {LLL.display()}", sep = " | ") LLL_Test(LLL)
linkedlist
// try to do it by your own logic ( ͡~ ͜ʖ ͡°) don't forget to upvote the answer and search for me @mohammad alshraideh ( ͡°( ͡° ͜ʖ( ͡° ͜ʖ ͡°)ʖ ͡°) ͡°) 'use strict';'use strict'; class Node{ constructor(data) { this.data =data; this.next =null; } } class LinkedList { constructor() { this.head = null; this.length =0 ; } insertFirstNode(data) { this.head = new Node(data, this.head); this.length++; } insertLastNode(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; } else { let currentNode = this.head; while (currentNode.next) { currentNode = currentNode.next; } currentNode.next = newNode; } this.length++; } insertNodeAtIndex(data, index) { if (this.length < index) { console.log("index is greater than size of list"); return null ; } const node = new Node(data); // if (!this.head) { // // this.head = node; // } else if (index === 0) { const head = this.head; this.head = node; node.next = head; } else { let previousNode; let currentNode = this.head; let indexCounter = 0; while (indexCounter < index) { previousNode = currentNode; currentNode = previousNode.next; indexCounter++; } previousNode.next = node; node.next = null; if (currentNode) { node.next = currentNode; } } this.length++; } getNodeAtIndex(index) { if (index > this.length) { console.log("index is greater than size of list"); return null; } let currentNode = this.head; let counter = 0; while (counter < index) { currentNode = currentNode.next; counter++; } return currentNode.data ; } includes(values) { let isIncludes = false; let currentNode = this.head; while (currentNode) { if (currentNode.data === values) { isIncludes = true; } currentNode = currentNode.next; } return isIncludes; } removeNodeAtIndex(index) { if (index > this.length) { console.log("index is greater than size of list"); return null; } if (!this.head) { console.log("List is empty"); return null; } if (index === 0) { this.head = null; } else { let previousNode; let currentNode = this.head; let counter = 0; while (counter < index) { previousNode = currentNode; currentNode = previousNode.next; counter++; } if (currentNode.next) { previousNode.next = currentNode.next; } } this.size--; console.log( `Removed node at index(${index}) and current linked list is: })` ); } clear() { //for each of the nodes, remove at index let index = 0; while (index < this.length) { console.log(`removing at index: ${index}`); this.removeNodeAtIndex(index); this.length--; index++; } } toString() { let currentNode = this.head; let output =''; while (currentNode) { output = ` ${output}${currentNode.data} -> ` ; currentNode = currentNode.next; } console.log(`${output}null`); } } module.exports=LinkedList ;
Source: github.com
linkedlist
# A simple Python program to introduce a linked list # Node class class Node: # Function to initialise the node object def __init__(self, data): self.data = data # Assign data self.next = None # Initialize next as null # Linked List class contains a Node object class LinkedList: # Function to initialize head def __init__(self): self.head = None # Code execution starts here if __name__=='__main__': # Start with the empty list llist = LinkedList() llist.head = Node(1) second = Node(2) third = Node(3) ''' Three nodes have been created. We have references to these three blocks as head, second and third llist.head second third | | | | | | +----+------+ +----+------+ +----+------+ | 1 | None | | 2 | None | | 3 | None | +----+------+ +----+------+ +----+------+ ''' llist.head.next = second; # Link first node with second ''' Now next of first Node refers to second. So they both are linked. llist.head second third | | | | | | +----+------+ +----+------+ +----+------+ | 1 | o-------->| 2 | null | | 3 | null | +----+------+ +----+------+ +----+------+ ''' second.next = third; # Link second node with the third node ''' Now next of second Node refers to third. So all three nodes are linked. llist.head second third | | | | | | +----+------+ +----+------+ +----+------+ | 1 | o-------->| 2 | o-------->| 3 | null | +----+------+ +----+------+ +----+------+ '''
Source: www.geeksforgeeks.org
linked lists
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
linked list
package stack // Node structure type Node struct { Val interface{} Next *Node } // Stack has jost top of node and with length type Stack struct { top *Node length int } // push add value to last index func (ll *Stack) push(n interface{}) { newStack := &Node{} // new node newStack.Val = n newStack.Next = ll.top ll.top = newStack ll.length++ } // pop remove last item as first output func (ll *Stack) pop() interface{} { result := ll.top.Val if ll.top.Next == nil { ll.top = nil } else { ll.top.Val, ll.top.Next = ll.top.Next.Val, ll.top.Next.Next } ll.length-- return result } // isEmpty to check our array is empty or not func (ll *Stack) isEmpty() bool { return ll.length == 0 } // len use to return length of our stack func (ll *Stack) len() int { return ll.length } // peak return last input value func (ll *Stack) peak() interface{} { return ll.top.Val } // show all value as an interface array func (ll *Stack) show() (in []interface{}) { current := ll.top for current != nil { in = append(in, current.Val) current = current.Next } return }
Source: github.com
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; }
Source: www.assignmentexpert.com