June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
49
Task/Deepcopy/C/deepcopy-1.c
Normal file
49
Task/Deepcopy/C/deepcopy-1.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/*Abhishek Ghosh, 15th November 2017*/
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
typedef struct{
|
||||
int a;
|
||||
}layer1;
|
||||
|
||||
typedef struct{
|
||||
layer1 l1;
|
||||
float b,c;
|
||||
}layer2;
|
||||
|
||||
typedef struct{
|
||||
layer2 l2;
|
||||
layer1 l1;
|
||||
int d,e;
|
||||
}layer3;
|
||||
|
||||
void showCake(layer3 cake){
|
||||
printf("\ncake.d = %d",cake.d);
|
||||
printf("\ncake.e = %d",cake.e);
|
||||
printf("\ncake.l1.a = %d",cake.l1.a);
|
||||
printf("\ncake.l2.b = %f",cake.l2.b);
|
||||
printf("\ncake.l2.l1.a = %d",cake.l2.l1.a);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
layer3 cake1,cake2;
|
||||
|
||||
cake1.d = 1;
|
||||
cake1.e = 2;
|
||||
cake1.l1.a = 3;
|
||||
cake1.l2.b = 4;
|
||||
cake1.l2.l1.a = 5;
|
||||
|
||||
printf("Cake 1 is : ");
|
||||
showCake(cake1);
|
||||
|
||||
cake2 = cake1;
|
||||
|
||||
cake2.l2.b += cake2.l2.l1.a;
|
||||
|
||||
printf("\nCake 2 is : ");
|
||||
showCake(cake2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
92
Task/Deepcopy/C/deepcopy-2.c
Normal file
92
Task/Deepcopy/C/deepcopy-2.c
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
/*Abhishek Ghosh, 15th November 2017*/
|
||||
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
typedef struct elem{
|
||||
int data;
|
||||
struct elem* next;
|
||||
}cell;
|
||||
|
||||
typedef cell* list;
|
||||
|
||||
void addToList(list *a,int num){
|
||||
list temp, holder;
|
||||
|
||||
if(*a==NULL){
|
||||
*a = (list)malloc(sizeof(cell));
|
||||
(*a)->data = num;
|
||||
(*a)->next = NULL;
|
||||
}
|
||||
else{
|
||||
temp = *a;
|
||||
|
||||
while(temp->next!=NULL)
|
||||
temp = temp->next;
|
||||
|
||||
holder = (list)malloc(sizeof(cell));
|
||||
holder->data = num;
|
||||
holder->next = NULL;
|
||||
|
||||
temp->next = holder;
|
||||
}
|
||||
}
|
||||
|
||||
list copyList(list a){
|
||||
list b, tempA, tempB, temp;
|
||||
|
||||
if(a!=NULL){
|
||||
b = (list)malloc(sizeof(cell));
|
||||
b->data = a->data;
|
||||
b->next = NULL;
|
||||
|
||||
tempA = a->next;
|
||||
tempB = b;
|
||||
|
||||
while(tempA!=NULL){
|
||||
temp = (list)malloc(sizeof(cell));
|
||||
temp->data = tempA->data;
|
||||
temp->next = NULL;
|
||||
|
||||
tempB->next = temp;
|
||||
tempB = temp;
|
||||
|
||||
tempA = tempA->next;
|
||||
}
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
void printList(list a){
|
||||
list temp = a;
|
||||
|
||||
while(temp!=NULL){
|
||||
printf("%d,",temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("\b");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
list a,b;
|
||||
int i;
|
||||
|
||||
for(i=1;i<=5;i++)
|
||||
addToList(&a,i);
|
||||
|
||||
printf("List a is : ");
|
||||
|
||||
printList(a);
|
||||
|
||||
b = copyList(a);
|
||||
|
||||
free(a);
|
||||
|
||||
printf("\nList a destroyed, List b is : ");
|
||||
|
||||
printList(b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
71
Task/Deepcopy/Kotlin/deepcopy.kotlin
Normal file
71
Task/Deepcopy/Kotlin/deepcopy.kotlin
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
// Version 1.2.31
|
||||
|
||||
import java.io.Serializable
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
import java.io.ObjectInputStream
|
||||
|
||||
fun <T : Serializable> deepCopy(obj: T?): T? {
|
||||
if (obj == null) return null
|
||||
val baos = ByteArrayOutputStream()
|
||||
val oos = ObjectOutputStream(baos)
|
||||
oos.writeObject(obj)
|
||||
oos.close()
|
||||
val bais = ByteArrayInputStream(baos.toByteArray())
|
||||
val ois = ObjectInputStream(bais)
|
||||
@Suppress("unchecked_cast")
|
||||
return ois.readObject() as T
|
||||
}
|
||||
|
||||
class Person(
|
||||
val name: String,
|
||||
var age: Int,
|
||||
val sex: Char,
|
||||
var income: Double,
|
||||
var partner: Person?
|
||||
) : Serializable
|
||||
|
||||
fun printDetails(p1: Person, p2: Person?, p3: Person, p4: Person?) {
|
||||
with (p3) {
|
||||
println("Name : $name")
|
||||
println("Age : $age")
|
||||
println("Sex : $sex")
|
||||
println("Income : $income")
|
||||
if (p4 == null) {
|
||||
println("Partner : None")
|
||||
}
|
||||
else {
|
||||
println("Partner :-")
|
||||
with (p4) {
|
||||
println(" Name : $name")
|
||||
println(" Age : $age")
|
||||
println(" Sex : $sex")
|
||||
println(" Income : $income")
|
||||
}
|
||||
}
|
||||
println("\nSame person as original '$name' == ${p1 === p3}")
|
||||
if (p4 != null) {
|
||||
println("Same person as original '${p2!!.name}' == ${p2 === p4}")
|
||||
}
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var p1 = Person("John", 35, 'M', 50000.0, null)
|
||||
val p2 = Person("Jane", 32, 'F', 25000.0, p1)
|
||||
p1.partner = p2
|
||||
var p3 = deepCopy(p1)
|
||||
val p4 = p3!!.partner
|
||||
printDetails(p1, p2, p3, p4)
|
||||
|
||||
println("..or, say, after 2 years have elapsed:-\n")
|
||||
with (p1) {
|
||||
age = 37
|
||||
income = 55000.0
|
||||
partner = null
|
||||
}
|
||||
p3 = deepCopy(p1)
|
||||
printDetails(p1, null, p3!!, null)
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
// The compiler can automatically implement Clone on structs (assuming all members have implemented Clone).
|
||||
// The compiler can automatically implement Clone on structs (assuming all members have implemented Clone).
|
||||
#[derive(Clone)]
|
||||
struct Tree<T> {
|
||||
left: Leaf<T>,
|
||||
|
|
@ -9,18 +9,19 @@ struct Tree<T> {
|
|||
type Leaf<T> = Option<Box<Tree<T>>>;
|
||||
|
||||
impl<T> Tree<T> {
|
||||
fn root(d: T) -> Self {
|
||||
Tree { left: None, data: d, right: None }
|
||||
fn root(data: T) -> Self {
|
||||
Self { left: None, data, right: None }
|
||||
}
|
||||
|
||||
fn leaf(d: T) -> Leaf<T> {
|
||||
Some(Box::new(Tree::root(d)))
|
||||
Some(Box::new(Self::root(d)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut tree = Tree::root(vec![4,5,6]);
|
||||
tree.right = Tree::leaf(vec![1,2,3]);
|
||||
tree.left = Tree::leaf(vec![7,8,9]);
|
||||
let mut tree = Tree::root([4, 5, 6]);
|
||||
tree.right = Tree::leaf([1, 2, 3]);
|
||||
tree.left = Tree::leaf([7, 8, 9]);
|
||||
|
||||
let newtree = tree.clone();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue