WeixiangYan/CodeTransOcean huggingface dataset:
Task: Queue_Definition





Java: public class Queue<E>{
    Node<E> head = null, tail = null;

    static class Node<E>{
        E value;
        Node<E> next;

        Node(E value, Node<E> next){
            this.value= value;
            this.next= next;
        }

    }

    public Queue(){
    }

    public void enqueue(E value){ 
        Node<E> newNode= new Node<E>(value, null);
        if(empty()){
            head= newNode;
        }else{
            tail.next = newNode;
        }
        tail= newNode;
    }

    public E dequeue() throws java.util.NoSuchElementException{
        if(empty()){
            throw new java.util.NoSuchElementException("No more elements.");
        }
        E retVal= head.value;
        head= head.next;
        return retVal;
    } 

    public boolean empty(){
        return head == null;
    }
}






Fortran: module FIFO
  use fifo_nodes










  type fifo_head
     type(fifo_node), pointer :: head, tail
  end type fifo_head

contains

  subroutine new_fifo(h)
    type(fifo_head), intent(out) :: h
    nullify(h%head)
    nullify(h%tail)
  end subroutine new_fifo

  subroutine fifo_enqueue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(inout), target :: n

    if ( associated(h%tail) ) then
       h%tail%next => n
       h%tail => n
    else
       h%tail => n
       h%head => n
    end if

    nullify(n%next)
  end subroutine fifo_enqueue

  subroutine fifo_dequeue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(out), target :: n

    if ( associated(h%head) ) then
       n = h%head
       if ( associated(n%next) ) then
          h%head => n%next
       else
          nullify(h%head)
          nullify(h%tail)
       end if
       n%valid = .true.
    else
       n%valid = .false.
    end if
    nullify(n%next)
  end subroutine fifo_dequeue

  function fifo_isempty(h) result(r)
    logical :: r
    type(fifo_head), intent(in) :: h
    if ( associated(h%head) ) then
       r = .false.
    else
       r = .true.
    end if
  end function fifo_isempty

end module FIFO
Task: Queue_Definition







Python:    class FIFO(object):
       def __init__(self, *args):
           self.contents = list(args)
       def __call__(self):
           return self.pop()
       def __len__(self):
           return len(self.contents)
       def pop(self):
           return self.contents.pop(0)
       def push(self, item):
           self.contents.append(item)
       def extend(self,*itemlist):
           self.contents += itemlist
       def empty(self):
           return bool(self.contents)
       def __iter__(self):
           return self
       def next(self):
           if self.empty():
               raise StopIteration
           return self.pop()

if __name__ == "__main__":
    
    f = FIFO()
    f.push(3)
    f.push(2)
    f.push(1)
    while not f.empty():
        print f.pop(),
    
    
    f = FIFO(3,2,1)
    while not f.empty():
        print f(),
    
    
    
    f = FIFO(3,2,1)
    while f:
        print f(),
    
    f = FIFO(3,2,1)
    for i in f:
        print i,




Fortran: module FIFO
  use fifo_nodes










  type fifo_head
     type(fifo_node), pointer :: head, tail
  end type fifo_head

contains

  subroutine new_fifo(h)
    type(fifo_head), intent(out) :: h
    nullify(h%head)
    nullify(h%tail)
  end subroutine new_fifo

  subroutine fifo_enqueue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(inout), target :: n

    if ( associated(h%tail) ) then
       h%tail%next => n
       h%tail => n
    else
       h%tail => n
       h%head => n
    end if

    nullify(n%next)
  end subroutine fifo_enqueue

  subroutine fifo_dequeue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(out), target :: n

    if ( associated(h%head) ) then
       n = h%head
       if ( associated(n%next) ) then
          h%head => n%next
       else
          nullify(h%head)
          nullify(h%tail)
       end if
       n%valid = .true.
    else
       n%valid = .false.
    end if
    nullify(n%next)
  end subroutine fifo_dequeue

  function fifo_isempty(h) result(r)
    logical :: r
    type(fifo_head), intent(in) :: h
    if ( associated(h%head) ) then
       r = .false.
    else
       r = .true.
    end if
  end function fifo_isempty

end module FIFO
Task: Queue_Definition








VB: Public queue As New Collection

Private Sub push(what As Variant)
    queue.Add what
End Sub

Private Function pop() As Variant
    If queue.Count > 0 Then
        what = queue(1)
        queue.Remove 1
    Else
        what = CVErr(461)
    End If
    pop = what
End Function

Private Function empty_()
    empty_ = queue.Count = 0
End Function



Fortran: module FIFO
  use fifo_nodes










  type fifo_head
     type(fifo_node), pointer :: head, tail
  end type fifo_head

contains

  subroutine new_fifo(h)
    type(fifo_head), intent(out) :: h
    nullify(h%head)
    nullify(h%tail)
  end subroutine new_fifo

  subroutine fifo_enqueue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(inout), target :: n

    if ( associated(h%tail) ) then
       h%tail%next => n
       h%tail => n
    else
       h%tail => n
       h%head => n
    end if

    nullify(n%next)
  end subroutine fifo_enqueue

  subroutine fifo_dequeue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(out), target :: n

    if ( associated(h%head) ) then
       n = h%head
       if ( associated(n%next) ) then
          h%head => n%next
       else
          nullify(h%head)
          nullify(h%tail)
       end if
       n%valid = .true.
    else
       n%valid = .false.
    end if
    nullify(n%next)
  end subroutine fifo_dequeue

  function fifo_isempty(h) result(r)
    logical :: r
    type(fifo_head), intent(in) :: h
    if ( associated(h%head) ) then
       r = .false.
    else
       r = .true.
    end if
  end function fifo_isempty

end module FIFO
Task: Queue_Definition






PHP: class Fifo {
  private $data = array();
  public function push($element){
    array_push($this->data, $element);
  }
  public function pop(){
    if ($this->isEmpty()){
      throw new Exception('Attempt to pop from an empty queue');
    }
    return array_shift($this->data);
  }

  public function enqueue($element) { $this->push($element); }
  public function dequeue() { return $this->pop(); }

  public function isEmpty(){
    return empty($this->data);
  }
}





Fortran: module FIFO
  use fifo_nodes










  type fifo_head
     type(fifo_node), pointer :: head, tail
  end type fifo_head

contains

  subroutine new_fifo(h)
    type(fifo_head), intent(out) :: h
    nullify(h%head)
    nullify(h%tail)
  end subroutine new_fifo

  subroutine fifo_enqueue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(inout), target :: n

    if ( associated(h%tail) ) then
       h%tail%next => n
       h%tail => n
    else
       h%tail => n
       h%head => n
    end if

    nullify(n%next)
  end subroutine fifo_enqueue

  subroutine fifo_dequeue(h, n)
    type(fifo_head), intent(inout) :: h
    type(fifo_node), intent(out), target :: n

    if ( associated(h%head) ) then
       n = h%head
       if ( associated(n%next) ) then
          h%head => n%next
       else
          nullify(h%head)
          nullify(h%tail)
       end if
       n%valid = .true.
    else
       n%valid = .false.
    end if
    nullify(n%next)
  end subroutine fifo_dequeue

  function fifo_isempty(h) result(r)
    logical :: r
    type(fifo_head), intent(in) :: h
    if ( associated(h%head) ) then
       r = .false.
    else
       r = .true.
    end if
  end function fifo_isempty

end module FIFO
Task: Queue_Definition






PHP: class Fifo {
  private $data = array();
  public function push($element){
    array_push($this->data, $element);
  }
  public function pop(){
    if ($this->isEmpty()){
      throw new Exception('Attempt to pop from an empty queue');
    }
    return array_shift($this->data);
  }

  public function enqueue($element) { $this->push($element); }
  public function dequeue() { return $this->pop(); }

  public function isEmpty(){
    return empty($this->data);
  }
}
Task: Queue_Definition






PHP: class Fifo {
  private $data = array();
  public function push($element){
    array_push($this->data, $element);
  }
  public function pop(){
    if ($this->isEmpty()){
      throw new Exception('Attempt to pop from an empty queue');
    }
    return array_shift($this->data);
  }

  public function enqueue($element) { $this->push($element); }
  public function dequeue() { return $this->pop(); }

  public function isEmpty(){
    return empty($this->data);
  }
}






Ada: generic
   type Element_Type is private;
package Fifo is
   type Fifo_Type is private;
   procedure Push(List : in out Fifo_Type; Item : in Element_Type);
   procedure Pop(List : in out Fifo_Type; Item : out Element_Type);
   function Is_Empty(List : Fifo_Type) return Boolean;
   Empty_Error : exception;
private
   type Fifo_Element;
   type Fifo_Ptr is access Fifo_Element;
   type Fifo_Type is record
      Head : Fifo_Ptr := null;
      Tail : Fifo_Ptr := null;
   end record;
   type Fifo_Element is record
      Value : Element_Type;
      Next  : Fifo_Ptr := null;
   end record;
end Fifo;
Task: Remove lines from a file



C#: using System;
using System.IO;
using System.Linq;

public class Rosetta
{
    public static void Main() => RemoveLines("foobar.txt", start: 1, count: 2);

    static void RemoveLines(string filename, int start, int count = 1) =>
        File.WriteAllLines(filename, File.ReadAllLines(filename)
            .Where((line, index) => index < start - 1 || index >= start + count - 1));
}









Ada: with Ada.Text_IO, Ada.Directories, Ada.Command_Line, Ada.IO_Exceptions;
use Ada.Text_IO;

procedure Remove_Lines_From_File is
   Temporary: constant String := ".tmp";
begin
   if Ada.Command_Line.Argument_Count /= 3 then
      raise Constraint_Error;
   end if;
   declare
      Filename: String := Ada.Command_Line.Argument(1);
      First: Positive := Integer'Value(Ada.Command_Line.Argument(2));
      Last: Natural := Integer'Value(Ada.Command_Line.Argument(3)) + First - 1;
      Input, Output: File_Type;
      Line_Number: Positive := 1;
   begin
      Open(Input, In_File, Filename); 
      Create(Output, Out_File, Filename & Temporary); 
      while not End_Of_File(Input) loop
         declare
            Line: String := Get_Line(Input);
         begin
            if Line_Number < First or else Line_Number > Last then
               Put_Line(Output, Line);
            end if;
         end;
         Line_Number := Line_Number + 1;
      end loop;
      Close(Input);
      Close(Output);
      Ada.Directories.Rename(Old_Name => Filename & Temporary,
                             New_Name => Filename);
   end;
exception
   when Constraint_Error | Ada.IO_Exceptions.Name_Error =>
      Put_Line("usage: " & Ada.Command_Line.Command_Name &
                 " <filename> <first> <length>");
      Put_Line("  opens <filename> for reading and " &
                 "<filename>" & Temporary & " for temporary writing");
      Put_Line("  requires first > 0, length >= 0");
end Remove_Lines_From_File;
Task: Remove lines from a file

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


#define ERROR(fmt, arg) return fprintf(stderr, fmt "\n", arg)

int main(int argc, char **argv)
{
    FILE *fp;
    char *buf;
    size_t sz;
    int start, count, lines = 1;
    int dest = 0, src = 0, pos = -1;

    
    if (argc != 4)
        ERROR("Usage: %s <file> <start> <count>", argv[0]);

    if ((count = atoi(argv[3])) < 1) 
        return 0;

    if ((start = atoi(argv[2])) < 1)
        ERROR("Error: <start> (%d) must be positive", start);

    if ((fp = fopen(argv[1], "r")) == NULL)
        ERROR("No such file: %s", argv[1]);

    
    fseek(fp, 0, SEEK_END);
    sz = ftell(fp);
    buf = malloc(sz + 1);
    rewind(fp);

    
    while ((buf[++pos] = fgetc(fp)) != EOF) {
        if (buf[pos] == '\n') {
            ++lines;
            if (lines == start) dest = pos + 1;
            if (lines == start + count) src = pos + 1;
        }
    }

    
    if (start + count > lines) {
        free(buf);
        fclose(fp);
        ERROR("Error: invalid parameters for file with %d lines", --lines);
    }

    
    memmove(buf + dest, buf + src, pos - src);

    
    freopen(argv[1], "w", fp);
    fwrite(buf, pos - src + dest, 1, fp);

    free(buf);
    fclose(fp);
    return 0;
}











Ada: with Ada.Text_IO, Ada.Directories, Ada.Command_Line, Ada.IO_Exceptions;
use Ada.Text_IO;

procedure Remove_Lines_From_File is
   Temporary: constant String := ".tmp";
begin
   if Ada.Command_Line.Argument_Count /= 3 then
      raise Constraint_Error;
   end if;
   declare
      Filename: String := Ada.Command_Line.Argument(1);
      First: Positive := Integer'Value(Ada.Command_Line.Argument(2));
      Last: Natural := Integer'Value(Ada.Command_Line.Argument(3)) + First - 1;
      Input, Output: File_Type;
      Line_Number: Positive := 1;
   begin
      Open(Input, In_File, Filename); 
      Create(Output, Out_File, Filename & Temporary); 
      while not End_Of_File(Input) loop
         declare
            Line: String := Get_Line(Input);
         begin
            if Line_Number < First or else Line_Number > Last then
               Put_Line(Output, Line);
            end if;
         end;
         Line_Number := Line_Number + 1;
      end loop;
      Close(Input);
      Close(Output);
      Ada.Directories.Rename(Old_Name => Filename & Temporary,
                             New_Name => Filename);
   end;
exception
   when Constraint_Error | Ada.IO_Exceptions.Name_Error =>
      Put_Line("usage: " & Ada.Command_Line.Command_Name &
                 " <filename> <first> <length>");
      Put_Line("  opens <filename> for reading and " &
                 "<filename>" & Temporary & " for temporary writing");
      Put_Line("  requires first > 0, length >= 0");
end Remove_Lines_From_File;
Task: Remove lines from a file




Go: package main

import (
    "bytes"
    "errors"
    "fmt"
    "io/ioutil"
    "os"
)

func main() {
    if err := removeLines("foobar.txt", 1, 2); err != nil {
        fmt.Println(err)
    }
}

func removeLines(fn string, start, n int) (err error) {
    if start < 1 {
        return errors.New("invalid request.  line numbers start at 1.")
    }
    if n < 0 {
        return errors.New("invalid request.  negative number to remove.")
    }
    var f *os.File
    if f, err = os.OpenFile(fn, os.O_RDWR, 0); err != nil {
        return
    }
    defer func() {
        if cErr := f.Close(); err == nil {
            err = cErr
        }
    }()
    var b []byte
    if b, err = ioutil.ReadAll(f); err != nil {
        return
    }
    cut, ok := skip(b, start-1)
    if !ok {
        return fmt.Errorf("less than %d lines", start)
    }
    if n == 0 {
        return nil
    }
    tail, ok := skip(cut, n)
    if !ok {
        return fmt.Errorf("less than %d lines after line %d", n, start)
    }
    t := int64(len(b) - len(cut))
    if err = f.Truncate(t); err != nil {
        return
    }
    if len(tail) > 0 {
        _, err = f.WriteAt(tail, t)
    }
    return
}

func skip(b []byte, n int) ([]byte, bool) {
    for ; n > 0; n-- {
        if len(b) == 0 {
            return nil, false
        }
        x := bytes.IndexByte(b, '\n')
        if x < 0 {
            x = len(b)
        } else {
            x++
        }
        b = b[x:]
    }
    return b, true
}








Ada: with Ada.Text_IO, Ada.Directories, Ada.Command_Line, Ada.IO_Exceptions;
use Ada.Text_IO;

procedure Remove_Lines_From_File is
   Temporary: constant String := ".tmp";
begin
   if Ada.Command_Line.Argument_Count /= 3 then
      raise Constraint_Error;
   end if;
   declare
      Filename: String := Ada.Command_Line.Argument(1);
      First: Positive := Integer'Value(Ada.Command_Line.Argument(2));
      Last: Natural := Integer'Value(Ada.Command_Line.Argument(3)) + First - 1;
      Input, Output: File_Type;
      Line_Number: Positive := 1;
   begin
      Open(Input, In_File, Filename); 
      Create(Output, Out_File, Filename & Temporary); 
      while not End_Of_File(Input) loop
         declare
            Line: String := Get_Line(Input);
         begin
            if Line_Number < First or else Line_Number > Last then
               Put_Line(Output, Line);
            end if;
         end;
         Line_Number := Line_Number + 1;
      end loop;
      Close(Input);
      Close(Output);
      Ada.Directories.Rename(Old_Name => Filename & Temporary,
                             New_Name => Filename);
   end;
exception
   when Constraint_Error | Ada.IO_Exceptions.Name_Error =>
      Put_Line("usage: " & Ada.Command_Line.Command_Name &
                 " <filename> <first> <length>");
      Put_Line("  opens <filename> for reading and " &
                 "<filename>" & Temporary & " for temporary writing");
      Put_Line("  requires first > 0, length >= 0");
end Remove_Lines_From_File;
Task: Remove lines from a file





Java: import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class RemoveLines
{
	public static void main(String[] args)
	{
		
		String filename="foobar.txt";
		
		int startline=1;
		
		int numlines=2;
		
		RemoveLines now=new RemoveLines();
		now.delete(filename,startline,numlines);
	}
	void delete(String filename, int startline, int numlines)
	{
		try
		{
			BufferedReader br=new BufferedReader(new FileReader(filename));
			
			
			StringBuffer sb=new StringBuffer("");
			
			
			int linenumber=1;
			String line;
			
			while((line=br.readLine())!=null)
			{
				
				if(linenumber<startline||linenumber>=startline+numlines)
					sb.append(line+"\n");
				linenumber++;
			}
			if(startline+numlines>linenumber)
				System.out.println("End of file reached.");
			br.close();
			
			FileWriter fw=new FileWriter(new File(filename));
			
			fw.write(sb.toString());
			fw.close();
		}
		catch (Exception e)
		{
			System.out.println("Something went horribly wrong: "+e.getMessage());
		}
	}
}







Ada: with Ada.Text_IO, Ada.Directories, Ada.Command_Line, Ada.IO_Exceptions;
use Ada.Text_IO;

procedure Remove_Lines_From_File is
   Temporary: constant String := ".tmp";
begin
   if Ada.Command_Line.Argument_Count /= 3 then
      raise Constraint_Error;
   end if;
   declare
      Filename: String := Ada.Command_Line.Argument(1);
      First: Positive := Integer'Value(Ada.Command_Line.Argument(2));
      Last: Natural := Integer'Value(Ada.Command_Line.Argument(3)) + First - 1;
      Input, Output: File_Type;
      Line_Number: Positive := 1;
   begin
      Open(Input, In_File, Filename); 
      Create(Output, Out_File, Filename & Temporary); 
      while not End_Of_File(Input) loop
         declare
            Line: String := Get_Line(Input);
         begin
            if Line_Number < First or else Line_Number > Last then
               Put_Line(Output, Line);
            end if;
         end;
         Line_Number := Line_Number + 1;
      end loop;
      Close(Input);
      Close(Output);
      Ada.Directories.Rename(Old_Name => Filename & Temporary,
                             New_Name => Filename);
   end;
exception
   when Constraint_Error | Ada.IO_Exceptions.Name_Error =>
      Put_Line("usage: " & Ada.Command_Line.Command_Name &
                 " <filename> <first> <length>");
      Put_Line("  opens <filename> for reading and " &
                 "<filename>" & Temporary & " for temporary writing");
      Put_Line("  requires first > 0, length >= 0");
end Remove_Lines_From_File;
Task: Remove lines from a file







Python: 

import fileinput, sys

fname, start, count = sys.argv[1:4]
start, count = int(start), int(count)

for line in fileinput.input(fname, inplace=1, backup='.orig'):
    if start <= fileinput.lineno() < start + count:
        pass
    else:
        print line.rstrip("\n")
fileinput.close()





Ada: with Ada.Text_IO, Ada.Directories, Ada.Command_Line, Ada.IO_Exceptions;
use Ada.Text_IO;

procedure Remove_Lines_From_File is
   Temporary: constant String := ".tmp";
begin
   if Ada.Command_Line.Argument_Count /= 3 then
      raise Constraint_Error;
   end if;
   declare
      Filename: String := Ada.Command_Line.Argument(1);
      First: Positive := Integer'Value(Ada.Command_Line.Argument(2));
      Last: Natural := Integer'Value(Ada.Command_Line.Argument(3)) + First - 1;
      Input, Output: File_Type;
      Line_Number: Positive := 1;
   begin
      Open(Input, In_File, Filename); 
      Create(Output, Out_File, Filename & Temporary); 
      while not End_Of_File(Input) loop
         declare
            Line: String := Get_Line(Input);
         begin
            if Line_Number < First or else Line_Number > Last then
               Put_Line(Output, Line);
            end if;
         end;
         Line_Number := Line_Number + 1;
      end loop;
      Close(Input);
      Close(Output);
      Ada.Directories.Rename(Old_Name => Filename & Temporary,
                             New_Name => Filename);
   end;
exception
   when Constraint_Error | Ada.IO_Exceptions.Name_Error =>
      Put_Line("usage: " & Ada.Command_Line.Command_Name &
                 " <filename> <first> <length>");
      Put_Line("  opens <filename> for reading and " &
                 "<filename>" & Temporary & " for temporary writing");
      Put_Line("  requires first > 0, length >= 0");
end Remove_Lines_From_File;
Task: Remove lines from a file








VB: Option Explicit

Sub Main()
   
   RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 11, 5
   
   RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 8, 5
   
   RemoveLines "C:\Users\" & Environ("username") & "\Desktop\foobar.txt", 3, 5
End Sub

Private Sub RemoveLines(StrFile As String, StartLine As Long, NumberOfLines As Long)
Dim Nb As Integer, s As String, count As Long, out As String
   Nb = FreeFile
   Open StrFile For Input As #Nb
      While Not EOF(Nb)
         count = count + 1
         Line Input #Nb, s
         If count < StartLine Or count >= StartLine + NumberOfLines Then
            out = out & s & vbCrLf
         End If
      Wend
   Close #Nb
   If StartLine >= count Then
      MsgBox "The file contains only " & count & " lines"
   ElseIf StartLine + NumberOfLines > count Then
      MsgBox "You only can remove " & count - StartLine & " lines"
   Else
      Nb = FreeFile
      Open StrFile For Output As #Nb
         Print #Nb, out
      Close #Nb
   End If
End Sub




Ada: with Ada.Text_IO, Ada.Directories, Ada.Command_Line, Ada.IO_Exceptions;
use Ada.Text_IO;

procedure Remove_Lines_From_File is
   Temporary: constant String := ".tmp";
begin
   if Ada.Command_Line.Argument_Count /= 3 then
      raise Constraint_Error;
   end if;
   declare
      Filename: String := Ada.Command_Line.Argument(1);
      First: Positive := Integer'Value(Ada.Command_Line.Argument(2));
      Last: Natural := Integer'Value(Ada.Command_Line.Argument(3)) + First - 1;
      Input, Output: File_Type;
      Line_Number: Positive := 1;
   begin
      Open(Input, In_File, Filename); 
      Create(Output, Out_File, Filename & Temporary); 
      while not End_Of_File(Input) loop
         declare
            Line: String := Get_Line(Input);
         begin
            if Line_Number < First or else Line_Number > Last then
               Put_Line(Output, Line);
            end if;
         end;
         Line_Number := Line_Number + 1;
      end loop;
      Close(Input);
      Close(Output);
      Ada.Directories.Rename(Old_Name => Filename & Temporary,
                             New_Name => Filename);
   end;
exception
   when Constraint_Error | Ada.IO_Exceptions.Name_Error =>
      Put_Line("usage: " & Ada.Command_Line.Command_Name &
                 " <filename> <first> <length>");
      Put_Line("  opens <filename> for reading and " &
                 "<filename>" & Temporary & " for temporary writing");
      Put_Line("  requires first > 0, length >= 0");
end Remove_Lines_From_File;