0% found this document useful (0 votes)
13 views3 pages

Vector

This document defines a dynamic vector data structure class in C++. The Vector class uses a dynamically allocated array to store elements. It includes methods for insertion, deletion, searching, replacing elements, and dynamically resizing the underlying array.

Uploaded by

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

Vector

This document defines a dynamic vector data structure class in C++. The Vector class uses a dynamically allocated array to store elements. It includes methods for insertion, deletion, searching, replacing elements, and dynamically resizing the underlying array.

Uploaded by

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

#include <iostream>

using namespace std;

//vector data structure


class Vector {
private:
//private data members
int* vect;
int capacity;
int increment = 0;
int elmCount = 0;

bool isFull() {
return (elmCount == capacity);
}

bool isEmpty() {
return (elmCount == 0);
}

void dynamicGrow() { //doubles the size of vector if increment is not


provided
int* toDelete = vect;
if (increment == 0) {
capacity *= 2; //doubles the size of the vector
}
else {
capacity += increment; //increases the size by increment
}
vect = new int[capacity];

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


vect[i] = toDelete[i]; //copy elms of old arr to the
dynamically grown one
}
delete[] toDelete; //delete old array
}

void dynamicShrink() { //reduces the size of array to the number of


elms it contains
int* toDelete = vect;
capacity = elmCount;
vect = new int[capacity];

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


vect[i] = toDelete[i]; //copy elms of the old array to the
dynamically shrinked one
}
delete[] toDelete;
}

public:
Vector() : capacity(10), vect(new int[capacity]) {} //default size array of
size 10

Vector(int cap) : capacity(cap), vect(new int[cap]) {}

Vector(int cap, int inc) : capacity(cap), vect(new int[cap]), increment(inc)


{}
void insert(int student) {
if (isFull()) {
dynamicGrow();
}
vect[elmCount] = student;
elmCount++;
}

void deleteElm(int value) {


int loc = search(value); //index at which the elm is stored

if (loc != -1) {
for (int i = loc; i < elmCount - 1; i++) { //from the elm to
delete onwards
vect[i] = vect[i + 1]; //copy every next elm to the
previous index
}
elmCount--;
dynamicShrink();
}
}

int search(int value) { //return index of the elm otherwise -1


if (isEmpty()) {
cout << "The list is empty.\n" << endl;
}
else {
for (int i = 0; i < elmCount; i++) {
if (vect[i] == value) {
return i;
}
}
cout << "Entered value is not in the list.\n" << endl;
}
return -1;
}

void replace(int toRep, int repWith) { //replace a student with another


int loc = search(toRep);
if (loc != -1) {
vect[loc] = repWith;
}
}

void replaceAtIndex(int index, int repWith) { //replace a student at a


specified index
if ((index > 0) && (index < elmCount)) {
vect[index] = repWith;
}
else {
cout << "Could not replace. Invalid index entered.";
}
}

void display(){ //prints all elememts in the vector


for (int i = 0; i < elmCount; i++){
cout << vect[i] << " ";
}
cout << endl << endl;
}
};

int main(){
Vector vect(3);

for (int i; i < 7; i++){


vect.insert(i);
}

vect.display();
vect.deleteElm(3);
vect.display();
vect.replace(1, 5);
vect.replaceAtIndex(3,2);
vect.display();
}

You might also like