0% found this document useful (0 votes)
9 views7 pages

Ds Lab Program

The document contains three C++ code snippets demonstrating dynamic memory allocation for different data structures. The first snippet performs addition of 'n' elements using a class with dynamic array allocation. The second and third snippets create single linked lists, with the second hardcoding four nodes and the third allowing user input for the number of nodes and their values.

Uploaded by

sreeja18138
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Ds Lab Program

The document contains three C++ code snippets demonstrating dynamic memory allocation for different data structures. The first snippet performs addition of 'n' elements using a class with dynamic array allocation. The second and third snippets create single linked lists, with the second hardcoding four nodes and the third allowing user input for the number of nodes and their values.

Uploaded by

sreeja18138
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write C++ code to perform addition of n elements using dynamic memory allocation

#include <iostream>

using namespace std;

class Addition

private:

int n;

int *ptr;

int sum;

public:

void getSize()

cout << "Enter number of elements: ";

cin >> n;

ptr = new int[n];

void getElements()

cout << "Enter " << n << " elements:\n";

for(int i = 0; i < n; i++)

cin >> ptr[i];

void calculateSum()
{

sum = 0;

for(int i = 0; i < n; i++)

sum = sum + ptr[i];

void displaySum()

cout << "Sum of elements = " << sum << endl;

~Addition()

delete[] ptr;

};

int main()

Addition obj;

[Link]();

[Link]();

[Link]();

[Link]();

return 0;
}

[Link] C++ code to create 4 nodes in a Single Linked List using Dynamic Memory Allocation

#include <iostream>

using namespace std;

class Node

public:

int data;

Node *next;

};

int main()

Node *head = NULL, *second = NULL, *third = NULL, *fourth = NULL;

head = new Node();

second = new Node();

third = new Node();

fourth = new Node();

head->data = 10;

head->next = second;

second->data = 20;

second->next = third;
third->data = 30;

third->next = fourth;

fourth->data = 40;

fourth->next = NULL;

Node *temp = head;

cout << "Single Linked List Elements are:\n";

while(temp != NULL)

cout << temp->data << " -> ";

temp = temp->next;

cout << "NULL";

return 0;

[Link] C++ code to create Single Linked List nodes based on user input using Dynamic Memory
Allocation

#include <iostream>

using namespace std;


class Node

public:

int data;

Node *next;

};

int main()

int n, value;

Node *head = NULL, *newnode = NULL, *temp = NULL;

cout << "Enter number of nodes: ";

cin >> n;

for(int i = 1; i <= n; i++)

newnode = new Node();

cout << "Enter data for node " << i << ": ";

cin >> value;

newnode->data = value;
newnode->next = NULL;

if(head == NULL)

head = newnode;

temp = head;

else

temp->next = newnode;

temp = newnode;

temp = head;

cout << "Single Linked List Elements are:\n"

while(temp != NULL)

cout << temp->data << " -> ";

temp = temp->next;

cout << "NULL";


return 0;

You might also like