WeixiangYan/CodeTransOcean huggingface dataset:
Task: Singly-linked list_Element removal

C: #include<stdlib.h>
#include<stdio.h>

typedef struct elem{
	int data;
	struct elem* next;
}cell;

typedef cell* list;

list addToList(list a,int num){
	
	list iter, temp;
	int i=0;
	
	if(a==NULL){
		a = (list)malloc(sizeof(cell));
		a->data = num;
		a->next = NULL;
	}
	else{
		iter = a;
		
		while(iter->next!=NULL){
			iter = iter->next;
		}
		
		temp = (list)malloc(sizeof(cell));
		temp->data = num;
		temp->next = NULL;
		
		iter->next = temp;
	}	
	return a;
}

list deleteFromList(list a,int pos){
	
	int i=1;
	list temp,iter;
	
	if(a!=NULL){
		iter = a;
		
		if(pos==1){
			a = a->next;
			iter->next = NULL;
			free(iter);
		}
		
		else{
			while(i++!=pos-1)
				iter = iter->next;
			temp = iter->next;
			iter->next = temp->next;
			temp->next = NULL;
			free(temp);
		}
	}
	return a;
}

void printList(list a){
	list temp = a;
	
	printf("List contains following elements : \n");
	
	while(temp!=NULL){
		printf("%d ",temp->data);
		temp = temp->next;
	}
}

int main(int argC,char* argV[])
{
	list a = NULL;
	int i;
	
	if(argC == 1)
		printf("Usage : %s <list of integers to be inserted into the list>",argV[0]);
	else{
		for(i=2;i<=argC;i++)
			a = addToList(a,atoi(argV[i-1]));
		
		printList(a);
		
		do{
			printf("\nEnter position of element to be removed (1-%d) : ",argC-1);
			
			scanf("%d",&i);
			
			if(i>0 && i<=argC-1){
				a = deleteFromList(a,i);
				printList(a);
			}
		}while(i>argC-1||i<=0);
	}
	return 0;
}




Go: package main

import "fmt"

type Ele struct {
    Data interface{}
    Next *Ele
}

var head *Ele

func (e *Ele) Append(data interface{}) *Ele {
    if e == nil {
        return e
    }
    if e.Next == nil {
        e.Next = &Ele{data, nil}
    } else {
        e.Next = &Ele{data, e.Next}
    }
    return e.Next
}




func (e *Ele) Remove(data interface{}) {
    if e == nil {
        return
    }
    if e.Data == data {
        if e == head {
            head = e.Next
        }
        e.Next = nil
        return
    }
    prev := e
    for iter := e.Next; iter != nil; iter = iter.Next {
        if iter.Data == data {
            prev.Next = iter.Next
            iter.Next = nil
            return
        }
        prev = iter
    }
}

func (e *Ele) String() string {
    return fmt.Sprintf("%v", e.Data)
}

func (e *Ele) Traverse() {
    if e == nil {
        fmt.Println(e)
        return
    }
    for iter := e; iter != nil; iter = iter.Next {
        fmt.Println(iter)
    }
}

func main() {
    head = &Ele{"tacos", nil}
    next := head.Append("burritos")
    next = next.Append("fajitas")
    next = next.Append("enchilatas")

    fmt.Println("Before any removals:")
    head.Traverse()

    head.Remove("fajitas")
    fmt.Println("\nAfter removing fajitas:")
    head.Traverse()

    head.Remove("tacos")
    fmt.Println("\nAfter removing tacos:")
    head.Traverse()

    head.Remove("enchilatas")
    fmt.Println("\nAfter removing enchilatas:")
    head.Traverse()

    head.Remove("burritos")
    fmt.Println("\nAfter removing burritos:")
    head.Traverse()
}
Task: Singly-linked list_Element removal

C: #include<stdlib.h>
#include<stdio.h>

typedef struct elem{
	int data;
	struct elem* next;
}cell;

typedef cell* list;

list addToList(list a,int num){
	
	list iter, temp;
	int i=0;
	
	if(a==NULL){
		a = (list)malloc(sizeof(cell));
		a->data = num;
		a->next = NULL;
	}
	else{
		iter = a;
		
		while(iter->next!=NULL){
			iter = iter->next;
		}
		
		temp = (list)malloc(sizeof(cell));
		temp->data = num;
		temp->next = NULL;
		
		iter->next = temp;
	}	
	return a;
}

list deleteFromList(list a,int pos){
	
	int i=1;
	list temp,iter;
	
	if(a!=NULL){
		iter = a;
		
		if(pos==1){
			a = a->next;
			iter->next = NULL;
			free(iter);
		}
		
		else{
			while(i++!=pos-1)
				iter = iter->next;
			temp = iter->next;
			iter->next = temp->next;
			temp->next = NULL;
			free(temp);
		}
	}
	return a;
}

void printList(list a){
	list temp = a;
	
	printf("List contains following elements : \n");
	
	while(temp!=NULL){
		printf("%d ",temp->data);
		temp = temp->next;
	}
}

int main(int argC,char* argV[])
{
	list a = NULL;
	int i;
	
	if(argC == 1)
		printf("Usage : %s <list of integers to be inserted into the list>",argV[0]);
	else{
		for(i=2;i<=argC;i++)
			a = addToList(a,atoi(argV[i-1]));
		
		printList(a);
		
		do{
			printf("\nEnter position of element to be removed (1-%d) : ",argC-1);
			
			scanf("%d",&i);
			
			if(i>0 && i<=argC-1){
				a = deleteFromList(a,i);
				printList(a);
			}
		}while(i>argC-1||i<=0);
	}
	return 0;
}







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


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

    def insert_first(self, insert_this):
        new_node = Node(insert_this)
        new_node.next = self.head
        self.head = new_node

    def remove_node(self, remove_this):

        
        head_val = self.head

        
        
        
        if head_val is not None:
            if head_val.data == remove_this:
                self.head = head_val.next
                return

        
        
        
        
        
        while head_val is not None:
            if head_val.data == remove_this:
                break
            prev = head_val
            head_val = head_val.next

        
        if head_val is None:
            return

        
        prev.next = head_val.next

    def print_llist(self):
        print_node = self.head
        while print_node:
            print(print_node.data),
            print_node = print_node.next

mylist = SLinkedList()
mylist.insert_first("red")
mylist.insert_first("orange")
mylist.insert_first("yellow")
mylist.insert_first("green")
print("Original list:")
mylist.print_llist()
mylist.remove_node("orange")
print("\nAfter orange is removed:")
mylist.print_llist()
Task: Singly-linked list_Element removal

C: #include<stdlib.h>
#include<stdio.h>

typedef struct elem{
	int data;
	struct elem* next;
}cell;

typedef cell* list;

list addToList(list a,int num){
	
	list iter, temp;
	int i=0;
	
	if(a==NULL){
		a = (list)malloc(sizeof(cell));
		a->data = num;
		a->next = NULL;
	}
	else{
		iter = a;
		
		while(iter->next!=NULL){
			iter = iter->next;
		}
		
		temp = (list)malloc(sizeof(cell));
		temp->data = num;
		temp->next = NULL;
		
		iter->next = temp;
	}	
	return a;
}

list deleteFromList(list a,int pos){
	
	int i=1;
	list temp,iter;
	
	if(a!=NULL){
		iter = a;
		
		if(pos==1){
			a = a->next;
			iter->next = NULL;
			free(iter);
		}
		
		else{
			while(i++!=pos-1)
				iter = iter->next;
			temp = iter->next;
			iter->next = temp->next;
			temp->next = NULL;
			free(temp);
		}
	}
	return a;
}

void printList(list a){
	list temp = a;
	
	printf("List contains following elements : \n");
	
	while(temp!=NULL){
		printf("%d ",temp->data);
		temp = temp->next;
	}
}

int main(int argC,char* argV[])
{
	list a = NULL;
	int i;
	
	if(argC == 1)
		printf("Usage : %s <list of integers to be inserted into the list>",argV[0]);
	else{
		for(i=2;i<=argC;i++)
			a = addToList(a,atoi(argV[i-1]));
		
		printList(a);
		
		do{
			printf("\nEnter position of element to be removed (1-%d) : ",argC-1);
			
			scanf("%d",&i);
			
			if(i>0 && i<=argC-1){
				a = deleteFromList(a,i);
				printList(a);
			}
		}while(i>argC-1||i<=0);
	}
	return 0;
}








VB:     Module Module1

      Public Class ListEntry
        Public value As String
        Public [next] As ListEntry
      End Class

      Public Head As ListEntry

      
      
      
      
      Sub RemoveListEntryTasteless(entry As ListEntry)

        Dim prev As ListEntry = Nothing
        Dim walk = Head

        
        While walk IsNot entry
          prev = walk
          walk = walk.next
        End While

        
        If prev Is Nothing Then
          Head = entry.next
        Else
          prev.next = entry.next
        End If
      End Sub

      
      
      
      
      Sub RemoveListEntryTastefull(entry As ListEntry)

        Dim indirect = New ListEntry
        indirect.next = Head

        
        
        While indirect.next IsNot entry
          indirect = indirect.next
        End While

        
        indirect.next = entry.next

      End Sub

End Module
Task: Singly-linked list_Element removal


C++: #include <iostream>


struct link
{
  link* next;
  int data;

  link(int newItem, link* head)
  : next{head}, data{newItem}{}
};

void PrintList(link* head)
{
    if(!head) return;
    std::cout << head->data << " ";
    PrintList(head->next);
}

link* RemoveItem(int valueToRemove, link*&head)
{
    
    
    for(link** node = &head; *node; node = &((*node)->next))
    {
        if((*node)->data == valueToRemove)
        {
            
            link* removedNode = *node;
            *node = removedNode->next;
            removedNode->next = nullptr;
            return removedNode;
        }
    }

    
    return nullptr;
}

int main()
{
    
    link link33{33, nullptr};
    link link42{42, &link33};
    link link99{99, &link42};
    link link55{55, &link99};
    link* head = &link55;

    std::cout << "Full list: ";
    PrintList(head);

    std::cout << "\nRemove 55: ";
    auto removed = RemoveItem(55, head);
    PrintList(head);
    std::cout << "\nThe removed item: ";
    PrintList(removed);

    std::cout << "\nTry to remove -3: ";
    auto removed2 = RemoveItem(-3, head);
    PrintList(head);
    if (!removed2) std::cout << "\nItem not found\n";
}


C#: using System;
using System.Runtime.InteropServices;

static unsafe class Program
{
    ref struct LinkedListNode
    {
        public int Value;
        public LinkedListNode* Next;
        public override string ToString() => this.Value + (this.Next == null ? string.Empty : " -> " + this.Next->ToString());
    }

    static void Remove(LinkedListNode** head, LinkedListNode* entry)
    {
        
        

        LinkedListNode** indirect = head;

        
        

        while (*indirect != entry)
            indirect = &(*indirect)->Next;

        
        *indirect = entry->Next;
    }

    static void Main()
    {
        
        var head = (LinkedListNode*)Marshal.AllocHGlobal(sizeof(LinkedListNode));
        head->Value = 1;
        head->Next = (LinkedListNode*)Marshal.AllocHGlobal(sizeof(LinkedListNode));
        head->Next->Value = 2;
        head->Next->Next = null;

        LinkedListNode copy = *head;

        Console.WriteLine("original:                    " + head->ToString());

        Remove(&head, head);
        Console.WriteLine("after removing head:         " + head->ToString());

        head = &copy;
        Console.WriteLine("restored from copy:          " + head->ToString());

        Remove(&head, head->Next);
        Console.WriteLine("after removing second node:  " + head->ToString());
    }
}
Task: Singly-linked list_Element removal


C++: #include <iostream>


struct link
{
  link* next;
  int data;

  link(int newItem, link* head)
  : next{head}, data{newItem}{}
};

void PrintList(link* head)
{
    if(!head) return;
    std::cout << head->data << " ";
    PrintList(head->next);
}

link* RemoveItem(int valueToRemove, link*&head)
{
    
    
    for(link** node = &head; *node; node = &((*node)->next))
    {
        if((*node)->data == valueToRemove)
        {
            
            link* removedNode = *node;
            *node = removedNode->next;
            removedNode->next = nullptr;
            return removedNode;
        }
    }

    
    return nullptr;
}

int main()
{
    
    link link33{33, nullptr};
    link link42{42, &link33};
    link link99{99, &link42};
    link link55{55, &link99};
    link* head = &link55;

    std::cout << "Full list: ";
    PrintList(head);

    std::cout << "\nRemove 55: ";
    auto removed = RemoveItem(55, head);
    PrintList(head);
    std::cout << "\nThe removed item: ";
    PrintList(removed);

    std::cout << "\nTry to remove -3: ";
    auto removed2 = RemoveItem(-3, head);
    PrintList(head);
    if (!removed2) std::cout << "\nItem not found\n";
}



Go: package main

import "fmt"

type Ele struct {
    Data interface{}
    Next *Ele
}

var head *Ele

func (e *Ele) Append(data interface{}) *Ele {
    if e == nil {
        return e
    }
    if e.Next == nil {
        e.Next = &Ele{data, nil}
    } else {
        e.Next = &Ele{data, e.Next}
    }
    return e.Next
}




func (e *Ele) Remove(data interface{}) {
    if e == nil {
        return
    }
    if e.Data == data {
        if e == head {
            head = e.Next
        }
        e.Next = nil
        return
    }
    prev := e
    for iter := e.Next; iter != nil; iter = iter.Next {
        if iter.Data == data {
            prev.Next = iter.Next
            iter.Next = nil
            return
        }
        prev = iter
    }
}

func (e *Ele) String() string {
    return fmt.Sprintf("%v", e.Data)
}

func (e *Ele) Traverse() {
    if e == nil {
        fmt.Println(e)
        return
    }
    for iter := e; iter != nil; iter = iter.Next {
        fmt.Println(iter)
    }
}

func main() {
    head = &Ele{"tacos", nil}
    next := head.Append("burritos")
    next = next.Append("fajitas")
    next = next.Append("enchilatas")

    fmt.Println("Before any removals:")
    head.Traverse()

    head.Remove("fajitas")
    fmt.Println("\nAfter removing fajitas:")
    head.Traverse()

    head.Remove("tacos")
    fmt.Println("\nAfter removing tacos:")
    head.Traverse()

    head.Remove("enchilatas")
    fmt.Println("\nAfter removing enchilatas:")
    head.Traverse()

    head.Remove("burritos")
    fmt.Println("\nAfter removing burritos:")
    head.Traverse()
}
Task: Singly-linked list_Element removal


C++: #include <iostream>


struct link
{
  link* next;
  int data;

  link(int newItem, link* head)
  : next{head}, data{newItem}{}
};

void PrintList(link* head)
{
    if(!head) return;
    std::cout << head->data << " ";
    PrintList(head->next);
}

link* RemoveItem(int valueToRemove, link*&head)
{
    
    
    for(link** node = &head; *node; node = &((*node)->next))
    {
        if((*node)->data == valueToRemove)
        {
            
            link* removedNode = *node;
            *node = removedNode->next;
            removedNode->next = nullptr;
            return removedNode;
        }
    }

    
    return nullptr;
}

int main()
{
    
    link link33{33, nullptr};
    link link42{42, &link33};
    link link99{99, &link42};
    link link55{55, &link99};
    link* head = &link55;

    std::cout << "Full list: ";
    PrintList(head);

    std::cout << "\nRemove 55: ";
    auto removed = RemoveItem(55, head);
    PrintList(head);
    std::cout << "\nThe removed item: ";
    PrintList(removed);

    std::cout << "\nTry to remove -3: ";
    auto removed2 = RemoveItem(-3, head);
    PrintList(head);
    if (!removed2) std::cout << "\nItem not found\n";
}






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


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

    def insert_first(self, insert_this):
        new_node = Node(insert_this)
        new_node.next = self.head
        self.head = new_node

    def remove_node(self, remove_this):

        
        head_val = self.head

        
        
        
        if head_val is not None:
            if head_val.data == remove_this:
                self.head = head_val.next
                return

        
        
        
        
        
        while head_val is not None:
            if head_val.data == remove_this:
                break
            prev = head_val
            head_val = head_val.next

        
        if head_val is None:
            return

        
        prev.next = head_val.next

    def print_llist(self):
        print_node = self.head
        while print_node:
            print(print_node.data),
            print_node = print_node.next

mylist = SLinkedList()
mylist.insert_first("red")
mylist.insert_first("orange")
mylist.insert_first("yellow")
mylist.insert_first("green")
print("Original list:")
mylist.print_llist()
mylist.remove_node("orange")
print("\nAfter orange is removed:")
mylist.print_llist()
Task: Singly-linked list_Element removal


C++: #include <iostream>


struct link
{
  link* next;
  int data;

  link(int newItem, link* head)
  : next{head}, data{newItem}{}
};

void PrintList(link* head)
{
    if(!head) return;
    std::cout << head->data << " ";
    PrintList(head->next);
}

link* RemoveItem(int valueToRemove, link*&head)
{
    
    
    for(link** node = &head; *node; node = &((*node)->next))
    {
        if((*node)->data == valueToRemove)
        {
            
            link* removedNode = *node;
            *node = removedNode->next;
            removedNode->next = nullptr;
            return removedNode;
        }
    }

    
    return nullptr;
}

int main()
{
    
    link link33{33, nullptr};
    link link42{42, &link33};
    link link99{99, &link42};
    link link55{55, &link99};
    link* head = &link55;

    std::cout << "Full list: ";
    PrintList(head);

    std::cout << "\nRemove 55: ";
    auto removed = RemoveItem(55, head);
    PrintList(head);
    std::cout << "\nThe removed item: ";
    PrintList(removed);

    std::cout << "\nTry to remove -3: ";
    auto removed2 = RemoveItem(-3, head);
    PrintList(head);
    if (!removed2) std::cout << "\nItem not found\n";
}







VB:     Module Module1

      Public Class ListEntry
        Public value As String
        Public [next] As ListEntry
      End Class

      Public Head As ListEntry

      
      
      
      
      Sub RemoveListEntryTasteless(entry As ListEntry)

        Dim prev As ListEntry = Nothing
        Dim walk = Head

        
        While walk IsNot entry
          prev = walk
          walk = walk.next
        End While

        
        If prev Is Nothing Then
          Head = entry.next
        Else
          prev.next = entry.next
        End If
      End Sub

      
      
      
      
      Sub RemoveListEntryTastefull(entry As ListEntry)

        Dim indirect = New ListEntry
        indirect.next = Head

        
        
        While indirect.next IsNot entry
          indirect = indirect.next
        End While

        
        indirect.next = entry.next

      End Sub

End Module
Task: Singly-linked list_Element removal



C#: using System;
using System.Runtime.InteropServices;

static unsafe class Program
{
    ref struct LinkedListNode
    {
        public int Value;
        public LinkedListNode* Next;
        public override string ToString() => this.Value + (this.Next == null ? string.Empty : " -> " + this.Next->ToString());
    }

    static void Remove(LinkedListNode** head, LinkedListNode* entry)
    {
        
        

        LinkedListNode** indirect = head;

        
        

        while (*indirect != entry)
            indirect = &(*indirect)->Next;

        
        *indirect = entry->Next;
    }

    static void Main()
    {
        
        var head = (LinkedListNode*)Marshal.AllocHGlobal(sizeof(LinkedListNode));
        head->Value = 1;
        head->Next = (LinkedListNode*)Marshal.AllocHGlobal(sizeof(LinkedListNode));
        head->Next->Value = 2;
        head->Next->Next = null;

        LinkedListNode copy = *head;

        Console.WriteLine("original:                    " + head->ToString());

        Remove(&head, head);
        Console.WriteLine("after removing head:         " + head->ToString());

        head = &copy;
        Console.WriteLine("restored from copy:          " + head->ToString());

        Remove(&head, head->Next);
        Console.WriteLine("after removing second node:  " + head->ToString());
    }
}






VB:     Module Module1

      Public Class ListEntry
        Public value As String
        Public [next] As ListEntry
      End Class

      Public Head As ListEntry

      
      
      
      
      Sub RemoveListEntryTasteless(entry As ListEntry)

        Dim prev As ListEntry = Nothing
        Dim walk = Head

        
        While walk IsNot entry
          prev = walk
          walk = walk.next
        End While

        
        If prev Is Nothing Then
          Head = entry.next
        Else
          prev.next = entry.next
        End If
      End Sub

      
      
      
      
      Sub RemoveListEntryTastefull(entry As ListEntry)

        Dim indirect = New ListEntry
        indirect.next = Head

        
        
        While indirect.next IsNot entry
          indirect = indirect.next
        End While

        
        indirect.next = entry.next

      End Sub

End Module
Task: Singly-linked list_Element removal




Go: package main

import "fmt"

type Ele struct {
    Data interface{}
    Next *Ele
}

var head *Ele

func (e *Ele) Append(data interface{}) *Ele {
    if e == nil {
        return e
    }
    if e.Next == nil {
        e.Next = &Ele{data, nil}
    } else {
        e.Next = &Ele{data, e.Next}
    }
    return e.Next
}




func (e *Ele) Remove(data interface{}) {
    if e == nil {
        return
    }
    if e.Data == data {
        if e == head {
            head = e.Next
        }
        e.Next = nil
        return
    }
    prev := e
    for iter := e.Next; iter != nil; iter = iter.Next {
        if iter.Data == data {
            prev.Next = iter.Next
            iter.Next = nil
            return
        }
        prev = iter
    }
}

func (e *Ele) String() string {
    return fmt.Sprintf("%v", e.Data)
}

func (e *Ele) Traverse() {
    if e == nil {
        fmt.Println(e)
        return
    }
    for iter := e; iter != nil; iter = iter.Next {
        fmt.Println(iter)
    }
}

func main() {
    head = &Ele{"tacos", nil}
    next := head.Append("burritos")
    next = next.Append("fajitas")
    next = next.Append("enchilatas")

    fmt.Println("Before any removals:")
    head.Traverse()

    head.Remove("fajitas")
    fmt.Println("\nAfter removing fajitas:")
    head.Traverse()

    head.Remove("tacos")
    fmt.Println("\nAfter removing tacos:")
    head.Traverse()

    head.Remove("enchilatas")
    fmt.Println("\nAfter removing enchilatas:")
    head.Traverse()

    head.Remove("burritos")
    fmt.Println("\nAfter removing burritos:")
    head.Traverse()
}




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


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

    def insert_first(self, insert_this):
        new_node = Node(insert_this)
        new_node.next = self.head
        self.head = new_node

    def remove_node(self, remove_this):

        
        head_val = self.head

        
        
        
        if head_val is not None:
            if head_val.data == remove_this:
                self.head = head_val.next
                return

        
        
        
        
        
        while head_val is not None:
            if head_val.data == remove_this:
                break
            prev = head_val
            head_val = head_val.next

        
        if head_val is None:
            return

        
        prev.next = head_val.next

    def print_llist(self):
        print_node = self.head
        while print_node:
            print(print_node.data),
            print_node = print_node.next

mylist = SLinkedList()
mylist.insert_first("red")
mylist.insert_first("orange")
mylist.insert_first("yellow")
mylist.insert_first("green")
print("Original list:")
mylist.print_llist()
mylist.remove_node("orange")
print("\nAfter orange is removed:")
mylist.print_llist()