How to resolve the algorithm Deepcopy step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Deepcopy step by step in the C programming language

Table of Contents

Problem Statement

Demonstrate how to copy data structures containing complex heterogeneous and cyclic semantics. This is often referred to as deep copying, and is normally required where structures are mutable and to ensure that independent copies can be manipulated without side-effects. If this facility is not built into the language, it is permissible to use functions from a common library, or a coded procedure.

The task should show:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Deepcopy step by step in the C programming language

Source Code 1:

This code defines three nested structures (layer1, layer2, and layer3) and a function (showCake) to display the contents of layer3. The main function creates two instances of layer3 and demonstrates how to access and modify their members using the dot operator.

Source Code 2:

This code implements a simple linked list using the cell structure and the list typedef. It includes functions to add new elements to the list (addToList), copy the list (copyList), and print the list (printList). The main function creates a linked list, copies it, and then frees the original list to demonstrate how the copy is independent of the original.

Detailed Explanation:

Source Code 1:

  • Structures:

    • layer1: Contains an integer member a.
    • layer2: Contains a layer1 member l1 and two float members b and c.
    • layer3: Contains a layer2 member l2, a layer1 member l1, and two integer members d and e.
  • Function showCake(layer3 cake):

    • Takes a layer3 structure as an argument and displays its members.
  • Main Function:

    • Creates two instances of layer3 (cake1 and cake2).
    • Initializes the members of cake1.
    • Prints the contents of cake1.
    • Copies cake1 to cake2.
    • Modifies a member of cake2.
    • Prints the contents of cake2 to show that the copy is independent of the original.

Source Code 2:

  • Structures:
    • cell: Contains an integer data member and a pointer to the next cell.
  • Typedef:
    • list: A pointer to the first cell in the linked list.
  • Functions:
    • addToList(list *a, int num):
      • Adds a new cell with the given integer data to the end of the linked list.
    • copyList(list a):
      • Creates a new linked list and copies the elements from the input linked list.
    • printList(list a):
      • Traverses the linked list and prints the data in each cell.
  • Main Function:
    • Creates a linked list a and adds integers 1 to 5 to it.
    • Prints the contents of a.
    • Creates a copy b of a using the copyList function.
    • Frees the original list a.
    • Prints the contents of b to demonstrate that it is independent of the original list.

Source code in the c programming language

#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;
}


#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;
}


  

You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the Pascal programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Lang5 programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the REXX programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Prolog programming language
You may also check:How to resolve the algorithm Distribution of 0 digits in factorial series step by step in the REXX programming language