Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
1 views11 pages

Dsa Lab 1

The document contains a series of C++ programs that demonstrate basic operations on a fixed-size array, including insertion at the end, insertion at the start, insertion before a key, deletion from the start and end, and deletion at a specific position. Each program includes functions to print the list and manage the array's length. The examples illustrate how to manipulate the array while handling edge cases such as full or empty lists.

Uploaded by

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

Dsa Lab 1

The document contains a series of C++ programs that demonstrate basic operations on a fixed-size array, including insertion at the end, insertion at the start, insertion before a key, deletion from the start and end, and deletion at a specific position. Each program includes functions to print the list and manage the array's length. The examples illustrate how to manipulate the array while handling edge cases such as full or empty lists.

Uploaded by

bizzylazing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

DSA LAB 1

1. #include <iostream>

using namespace std;

#define SIZE 50 // fixed array size

// Function to print the current list

void printList(int A[], int length) {

if (length == 0) {

cout << "List is empty." << endl;

return;

for (int i = 0; i < length; i++) {

cout << A[i] << " ";

cout << endl;

// Insert at the end

void insertEnd(int A[], int &length, int value) {

if (length >= SIZE) {

cout << "List is full. Cannot insert." << endl;

return;

A[length] = value;

length++;

int main() {

int A[SIZE]; // array of size 50


int length = 0; // number of elements currently in list

// Before insertion

cout << "Before inserting at end:" << endl;

printList(A, length);

// Values to insert

int nums[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

// Insert each at the end

for (int i = 0; i < 10; i++) {

insertEnd(A, length, nums[i]);

// After insertion

cout << "After inserting at end:" << endl;

printList(A, length);

return 0;

OUTPUT:

2. #include <iostream>

using namespace std;

#define SIZE 50 // fixed array size


// Function to print the current list

void printList(int A[], int length) {

if (length == 0) {

cout << "List is empty." << endl;

return;

for (int i = 0; i < length; i++) {

cout << A[i] << " ";

cout << endl;

// Insert at the end

void insertEnd(int A[], int &length, int value) {

if (length >= SIZE) {

cout << "List is full. Cannot insert." << endl;

return;

A[length] = value;

length++;

// Insert at the start

void insertStart(int A[], int &length, int value) {

if (length >= SIZE) {

cout << "List is full. Cannot insert." << endl;

return;

for (int i = length; i > 0; i--) { // shift right

A[i] = A[i - 1];

}
A[0] = value;

length++;

int main() {

int A[SIZE];

int length = 0;

// Step 1: Insert 10 integers at the end

cout << "Before inserting at end:" << endl;

printList(A, length);

int nums1[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

for (int i = 0; i < 10; i++) {

insertEnd(A, length, nums1[i]);

cout << "After inserting at end:" << endl;

printList(A, length);

// Step 2: Insert 5, 6, 7 at the start

cout << "\nBefore inserting at start:" << endl;

printList(A, length);

insertStart(A, length, 5);

insertStart(A, length, 6);

insertStart(A, length, 7);

cout << "After inserting at start:" << endl;

printList(A, length);
return 0;

OUTPUT:

3. #include <iostream>

using namespace std;

#define SIZE 50 // fixed array size

// Print the list

void printList(int A[], int length) {

if (length == 0) {

cout << "List is empty." << endl;

return;

for (int i = 0; i < length; i++) {

cout << A[i] << " ";

cout << endl;

// Insert before first occurrence of a given key

void insertBefore(int A[], int &length, int key, int value) {

if (length >= SIZE) {

cout << "List is full. Cannot insert." << endl;

return;

}
int pos = -1;

for (int i = 0; i < length; i++) {

if (A[i] == key) {

pos = i;

break;

if (pos == -1) {

cout << "Key " << key << " not found." << endl;

return;

for (int i = length; i > pos; i--) { // shift right

A[i] = A[i - 1];

A[pos] = value;

length++;

int main() {

int A[SIZE] = {7, 6, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

int length = 13; // current number of elements

int keys[] = {40, 60, 90};

for (int i = 0; i < 3; i++) {

cout << "\nBefore inserting 0 before " << keys[i] << ":" << endl;

printList(A, length);

insertBefore(A, length, keys[i], 0);

cout << "After inserting 0 before " << keys[i] << ":" << endl;

printList(A, length);

}
return 0;

OUTPUT:

4. #include <iostream>

using namespace std;

#define SIZE 50

// Print the list

void printList(int A[], int length) {

if (length == 0) {

cout << "List is empty." << endl;

return;

for (int i = 0; i < length; i++) {

cout << A[i] << " ";

cout << endl;

// Delete from start


void deleteStart(int A[], int &length) {

if (length == 0) {

cout << "List is empty. Cannot delete." << endl;

return;

for (int i = 0; i < length - 1; i++) {

A[i] = A[i + 1];

length--;

// Delete from end

void deleteEnd(int A[], int &length) {

if (length == 0) {

cout << "List is empty. Cannot delete." << endl;

return;

length--;

int main() {

// Starting list after step 3

int A[SIZE] = {7, 6, 5, 10, 20, 30, 0, 40, 50, 0, 60, 70, 80, 0, 90, 100};

int length = 16;

// Delete from start

cout << "Before deleting from start:" << endl;

printList(A, length);

deleteStart(A, length);

cout << "After deleting from start:" << endl;

printList(A, length);
// Delete from end

cout << "\nBefore deleting from end:" << endl;

printList(A, length);

deleteEnd(A, length);

cout << "After deleting from end:" << endl;

printList(A, length);

return 0;

OUTPUT:

5. #include <iostream>

using namespace std;

#define SIZE 50

// Print the list

void printList(int A[], int length) {

if (length == 0) {

cout << "List is empty." << endl;

return;

for (int i = 0; i < length; i++) {

cout << A[i] << " ";


}

cout << endl;

// Delete element at given position (0-based index)

void deleteAt(int A[], int &length, int pos) {

if (pos < 0 || pos >= length) {

cout << "Invalid position to delete." << endl;

return;

for (int i = pos; i < length - 1; i++) {

A[i] = A[i + 1];

length--;

int main() {

// List after step 4

int A[SIZE] = {6, 5, 10, 20, 30, 0, 40, 50, 0, 60, 70, 80, 0, 90};

int length = 14;

cout << "Before deletion around first occurrence of 70:" << endl;

printList(A, length);

// Find first occurrence of 70

int pos70 = -1;

for (int i = 0; i < length; i++) {

if (A[i] == 70) {

pos70 = i;

break;

}
}

if (pos70 != -1) {

// Delete AFTER first (pos70 + 1 first so deletion doesn't shift the before element)

if (pos70 + 1 < length) {

deleteAt(A, length, pos70 + 1);

// Delete BEFORE

if (pos70 - 1 >= 0) {

deleteAt(A, length, pos70 - 1);

cout << "After deletion around first occurrence of 70:" << endl;

printList(A, length);

return 0;

OUTPUT:

You might also like