“Verlinkte Liste” Code-Antworten

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);
Clean Crocodile

verlinkte Liste

// 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;
}
Curious Caribou

verlinkte Liste

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
}
gocrazygh

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 |
    +----+------+     +----+------+     +----+------+
    '''
No Name

Ähnliche Antworten wie “Verlinkte Liste”

Fragen ähnlich wie “Verlinkte Liste”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen