0% found this document useful (0 votes)
184 views

Dsa Lab 1

The document describes a data structures and algorithms course lab assignment. The objectives are to implement pointers to functions and objects in C++. The problem is to write code for a class called arrayListType that can perform various tasks on character arrays, including removing elements by shifting or swapping, and removing single or all occurrences of a given element. The complete program solution provides the class definition and implementations of the required methods to test the functionality.

Uploaded by

Ali Sarmad
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)
184 views

Dsa Lab 1

The document describes a data structures and algorithms course lab assignment. The objectives are to implement pointers to functions and objects in C++. The problem is to write code for a class called arrayListType that can perform various tasks on character arrays, including removing elements by shifting or swapping, and removing single or all occurrences of a given element. The complete program solution provides the class definition and implementations of the required methods to test the functionality.

Uploaded by

Ali Sarmad
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
You are on page 1/ 4

Course Title: Data Structures & Algorithms

Instructor(s): Ms Hira Zahid Time: 1hr

Lab 1
Objectives: Get the knowledge of implementing pointers to function/object using C++
programming language.

Problem:
Write the C++ code to implement the class arrayListType which performs the following tasks:

Task 1: Write the function definition of removeAt of the class arrayListType which removes
an element from the list by shifting the elements of the list. Test this function by taking a char
array and position/location of the element to be removed as input.

void arrayListType::removeAt(char * plist, int loc) \\ Task 1


{
listlength = findlength(plist);
if (loc < 0 || loc >= listlength)
cout << "The location of the item to be removed is out of range" << endl;
else
{
for (int i = loc; i <= listlength - 1; i++)
plist[i] = plist[i + 1];
listlength--;
}
}
Task 2: The function removeAt of the class arrayListType removes an element from the list
by shifting the elements of the list. However, if the element to be removed is at the beginning of
the list and the list is fairly large, it could take a lot of computer time. Because the list elements
are in no particular order, you could simply remove the element by swapping the last element of
the list with the item to be removed and reducing the length of the list. Rewrite the definition of
the function removeAt using this technique and rename it as removeBySwapAt.

void arrayListType::removeBySwapAt(char *plist, int loc) \\Task 2


{
listlength = findlength(plist);
if (loc < 0 || loc >= listlength)
cout << "The location of the item to be removed is out of range" << endl;
else
{
plist[loc] = plist[listlength - 1];
plist[listlength - 1] = '\0';
listlength--;
}
}

Department of Computer Science, GC University Lahore.


Task 3: The function remove of the class arrayListType removes only the first occurrence of
an element. Add the function removeAll to the class arrayListType that would remove all
occurrences of a given element. Write the definition of the both functions i.e. remove and
removeAll and a program to test this function. In this task elements should be removed by the
method implemented in Task 1.

void arrayListType::remove(char * plist, char element) \\ Task 3


{
for (int i = 0; plist[i] != '\0'; i++)
{
if (plist[i] == element)
{
removeAt(plist, i);
break;
}
}
}

void arrayListType::removeAll(char * plist, char element) \\ Task 3


{
for (int i = 0; plist[i] != '\0'; i++)
{
if (plist[i] == element)
{
removeAt(plist, i);
}
}
}

Note: To calculate the length of an array using pointer a separate function findlength should be
performed.

Complete Program Solution:

#include<iostream>

using namespace std;

class arrayListType {
private:
int listlength;
public:
void removeAt(char* plist, int loc);
void remove(char *plist, char element);
void removeAll(char *plist, char element);
void removeBySwapAt(char *plist, int loc);
int findlength(char *ptr);
void print(char *plist);
};

int main()
{
int eno;

Department of Computer Science, GC University Lahore.


char element;
char *list = new char[255];
cout << "Enter a character array\n";
cin >> list;
arrayListType *obj = new arrayListType();
cout << "Entered Array: \t ";
obj->print(list);
cout << "\nEnter the element number you want to remove\n";
cin >> eno;
obj->removeAt(list, eno - 1);
cout << "\nAfter removing element number " << eno << endl;
obj->print(list);
cout << "\nEnter the element number you want to remove by swapping\n";
cin >> eno;
obj->removeBySwapAt(list, eno-1);
cout << "\nAfter removing element number " << eno << endl;
obj->print(list);
cout << "\n Enter the element you want to remove\n";
cin >> element;
obj->remove(list, element);
cout << "\nAfter removing only first occurrence of element " << element << endl;
obj->print(list);
cout << "\nEnter the element you want to remove from the list\n";
cin >> element;
obj->removeAll(list, element);
cout << "\After removing each occurrence of " << element << endl;
obj->print(list);
cout << endl;
system("pause");
return 0;
}

void arrayListType::removeBySwapAt(char *plist, int loc) \\Task 2


{
listlength = findlength(plist);
if (loc < 0 || loc >= listlength)
cout << "The location of the item to be removed is out of range" << endl;
else
{
plist[loc] = plist[listlength - 1];
plist[listlength - 1] = '\0';
listlength--;
}
}

void arrayListType::remove(char * plist, char element) \\ Task 3


{
for (int i = 0; plist[i] != '\0'; i++)
{
if (plist[i] == element)
{
removeAt(plist, i);
break;
}
}
}

void arrayListType::removeAll(char * plist, char element) \\ Task 3

Department of Computer Science, GC University Lahore.


{
for (int i = 0; plist[i] != '\0'; i++)
{
if (plist[i] == element)
{
removeAt(plist, i);
}
}
}

void arrayListType::removeAt(char * plist, int loc) \\ Task 1


{
listlength = findlength(plist);
if (loc < 0 || loc >= listlength)
cout << "The location of the item to be removed is out of range" << endl;
else
{
for (int i = loc; i <= listlength - 1; i++)
plist[i] = plist[i + 1];
listlength--;
}
}

int arrayListType::findlength(char *ptr)


{
int length = 0;
for (int i = 0; ptr[i] != '\0'; i++)
{
length++;
}
return length;
}

void arrayListType::print(char * plist)


{
for(int i = 0; plist[i] != '\0' ; i++)
{
cout << plist[i] << " ";
}
}

Department of Computer Science, GC University Lahore.

You might also like