You are on page 1of 24

NAME: R B SHARAN

REG NO: 20MID0209


COURSE: Data Structures and Algorithm Analysis
COURSE CODE: CSI 2002
SLOT: L43+L44

LAB ASSIGNMENT – 1
1) Write a program to implement list and to perform operations:
Insertion and deletion, using array.

Program Code:
#include <iostream>
using namespace std;

const int MAX_SIZE = 100; // Maximum size of the list

class List {
private:
int arr[MAX_SIZE]; // Array to store the list elements
int size; // Current size of the list

public:
List() {
size = 0; // Initialize the list size to 0
}

// Function to insert an element at the end of the list


void insert(int element) {
if (size < MAX_SIZE) {
arr[size] = element;
size++;
cout << "Element " << element << " inserted at the end of the list." <<
endl;
} else {
cout << "List is full. Cannot insert more elements." << endl;
}
}

// Function to delete an element from the list


void remove(int element) {
bool found = false;
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
found = true;
for (int j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size--;
cout << "Element " << element << " removed from the list." << endl;
break;
}
}
if (!found) {
cout << "Element " << element << " not found in the list." << endl;
}
}

// Function to display the current elements of the list


void display() {
if (size == 0) {
cout << "The list is empty." << endl;
} else {
cout << "List elements: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};

int main() {
List myList;

// Insert elements into the list


myList.insert(10);
myList.insert(20);
myList.insert(30);
myList.insert(40);
myList.insert(50);
myList.insert(60);
myList.insert(70);
myList.insert(80);
myList.insert(90);

// Display the list


myList.display();

// Remove an element from the list


myList.remove(20);
myList.remove(40);
myList.remove(70);
myList.remove(90);

// Display the updated list


myList.display();

return 0;
}
Screenshot:
Sample output:
2) Write a program using arrays
• To create a list L containing your date of birth in the following format:
ddmmyyyy
• To create a list M containing your pincode
• To merge the lists L and M ( L||M - || indicates concatenation)
• To split the list created in 3 into two equal parts
• To delete 0’s in the list L (created in 1)
• To duplicate odd numbers in the list M (created in 2) Eg, If the
pincode is 632178, then we o/p is 633211778

Program Code:
#include <iostream>
#include <vector>
using namespace std;

int dob[8];
int pin[6];
int merg[14];

// Function to split a list into two equal parts


pair<vector<int>, vector<int>> splitList(const vector<int>& mergedList) {
int middle = mergedList.size() / 2;
vector<int> part1(mergedList.begin(), mergedList.begin() + middle);
vector<int> part2(mergedList.begin() + middle, mergedList.end());
return make_pair(part1, part2);
}

// Function to delete 0's in list L


vector<int> deleteZeros(const vector<int>& L) {
vector<int> updatedL;

for (int digit : L) {


if (digit != 0) {
updatedL.push_back(digit);
}
}

return updatedL;
}

// Function to duplicate odd numbers in list M


vector<int> duplicateOddNumbers(const vector<int>& M) {
vector<int> updatedM;

for (int digit : M) {


updatedM.push_back(digit);
if (digit % 2 == 1) {
updatedM.push_back(digit);
}
}

return updatedM;
}

int main() {
cout << "Enter your date of birth in format ddmmyyyy \n";
for (int i = 0; i < 8; i++) {
cin >> dob[i];
}

cout << "Date of birth is : ";


for (int i = 0; i < 8; i++) {
cout << dob[i];
}

cout << "\nEnter your pincode \n";


for (int i = 0; i < 6; i++) {
cin >> pin[i];
}

cout << "Pincode is : ";


for (int i = 0; i < 6; i++) {
cout << pin[i];
}

int j = 0;
int k = 0;
for (int i = 0; i < 8; i++) {
merg[i] = dob[j];
j++;
}

for (int i = 8; i < 14; i++) {


merg[i] = pin[k];
k++;
}

cout << "\nMerged list is : ";


for (int i = 0; i < 14; i++) {
cout << merg[i];
}

// Split the merged list into two equal parts


auto split = splitList(vector<int>(merg, merg + 14));
cout << "\nPart 1: ";
for (int digit : split.first) {
cout << digit;
}
cout << "\nPart 2: ";
for (int digit : split.second) {
cout << digit;
}

// Delete 0's in list L


vector<int> updatedDOB = deleteZeros(vector<int>(dob, dob + 8));
cout << "\nList L after deleting 0's: ";
for (int digit : updatedDOB) {
cout << digit;
}

// Duplicate odd numbers in list M


vector<int> updatedPIN = duplicateOddNumbers(vector<int>(pin, pin + 6));
cout << "\nList M after duplicating odd numbers: ";
for (int digit : updatedPIN) {
cout << digit;
}

return 0;
}
Screenshot:
Sample Output:

3) Write a program using structure to store following details of


“n” students: Name, age, Dateofbirth(day, month, year),
Address(doorno, streetname, areaname, city, pincone), phoneno.

Program Code:
#include <iostream>
#include <string>

using namespace std;

// Define a structure to store student details


struct Student {
string name;
int age;
struct DateOfBirth {
int day;
int month;
int year;
} dob;
struct Address {
string doorno;
string streetname;
string areaname;
string city;
string pincode;
} address;
string phoneno;
};

int main() {
int n;

cout << "Enter the number of students: ";


cin >> n;

// Create an array of structures to store student details


Student students[n];

// Input details for each student


for (int i = 0; i < n; i++) {
cout << "Enter details for student " << i + 1 << ":" << endl;
cout << "Name: ";
cin.ignore(); // Ignore any previous newline character in the input buffer
getline(cin, students[i].name);

cout << "Age: ";


cin >> students[i].age;

cout << "Date of Birth (day month year): ";


cin >> students[i].dob.day >> students[i].dob.month >>
students[i].dob.year;

cout << "Address:" << endl;


cout << "Door No.: ";
cin.ignore();
getline(cin, students[i].address.doorno);

cout << "Street Name: ";


getline(cin, students[i].address.streetname);

cout << "Area Name: ";


getline(cin, students[i].address.areaname);

cout << "City: ";


getline(cin, students[i].address.city);

cout << "Pincode: ";


getline(cin, students[i].address.pincode);
cout << "Phone Number: ";
getline(cin, students[i].phoneno);
}

// Display the details of all students


cout << "Details of " << n << " students:" << endl;
for (int i = 0; i < n; i++) {
cout << "Student " << i + 1 << " Details:" << endl;
cout << "Name: " << students[i].name << endl;
cout << "Age: " << students[i].age << endl;
cout << "Date of Birth: " << students[i].dob.day << "/" <<
students[i].dob.month << "/" << students[i].dob.year << endl;
cout << "Address: " << students[i].address.doorno << ", " <<
students[i].address.streetname << ", " << students[i].address.areaname << ", "
<< students[i].address.city << " - " << students[i].address.pincode << endl;
cout << "Phone Number: " << students[i].phoneno << endl;
cout << endl;
}

return 0;
}
Screenshot:
Sample Output:
4) Write a program to illustrate implementation of union.
Program Code:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
union student // union is keyword & student is union name
{
char name[25];
int age;
float height;
}s; //instance s is created while declartion of union.
//instance can also be created after declaration as: union student s; or as: student
s;
strcpy( s.name, "sharan");
cout<<s.name<<endl;
s.age=21;
cout<<s.age<<endl;
s.height=181;
cout<<s.height<<endl;

cout<<"size of union s is "<<sizeof(s);

return 0;
}

Screenshot:
Sample Output;

5) Write a program to demonstrate difference between structure


and union.
Program Code:
//program for structure & union.

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
union student
{
char name[25];
int age;
float height;
}s;
strcpy( s.name, "sharan");
cout<<s.name<<endl;
s.age=21;
cout<<s.age<<endl;
s.height=181;
cout<<s.height<<endl;
cout<<"size of union s is "<<sizeof(s);

struct stud
{
char name[25];
int age;
float height;
}t;

strcpy( t.name, "kamal");


cout<<endl<<t.name<<endl;
t.age=16;
cout<<t.age<<endl;
t.height=175;
cout<<t.height<<endl;
cout<<"size of structure t is "<<sizeof(t);

return 0;
}
Screenshot:
Sample Output:

You might also like