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

Data Structure Assignment

This document contains code for an arraylist data structure implemented using a dynamic array. It includes a header file with the class declaration, a resource file with function definitions, and a main file to test it. The arraylist class uses a dynamic array that doubles in size when needed. It implements functions for adding, removing, accessing, and searching elements in the list. The main file tests it by adding sample data, providing a menu to call the functions, and printing the results.

Uploaded by

Irfan Haider
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
55 views7 pages

Data Structure Assignment

This document contains code for an arraylist data structure implemented using a dynamic array. It includes a header file with the class declaration, a resource file with function definitions, and a main file to test it. The arraylist class uses a dynamic array that doubles in size when needed. It implements functions for adding, removing, accessing, and searching elements in the list. The main file tests it by adding sample data, providing a menu to call the functions, and printing the results.

Uploaded by

Irfan Haider
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Data Structure & Algorithm

CSC-221

Sir Noman
Assignment 1

NAME::Irfan Haider
CLASS: BCE-4
ENROLLMENT NO: 01-132182-035
DEPARTMENT OF COMPUTER ENGINEERING
BAHRIA UNIVERSITY | ISLAMABAD CAMPUS

Header file
#pragma once

#define ARRAYLIST_H
template <class T>
class arraylist
{
private:
T* data;
int arrlength;
int listsize;
void resize();
bool needtoresize();

public:
//constructor
arraylist();
{
//get an array set up
data = new T[10];
// initilize parameter
arrlength = 10;
listsize = 0;

}
//accessor
bool contains(T items);
int indexof(T items);
int lastindexof(T items);
T get(int index);
int size;
//modifer
void add(T item);
void add(int index, T item);
void set(int index, T item);
void remove(int index);
void remove(T item);

};

Resource file

#ifndef ARRAYLIST_H
//#include"arraylist.h"
#endif
#include "Header.h"

template <class T>


void arraylist<T>::resize()
{
//resixe the array
//get a new array
T* temp = new T[arrlength * 2];
for (int i = 0; i < arrlength; i++)
temp[i] = data[i];
data = temp;
arrlength *= 2;
}
template <class T>
bool arraylist<T>::needtoresize()
{
// return if data equal to the array length
return arrlength == listsize;

}
template <class T>
void arraylist<T>::add(T item)
{
// add item to the end of list
if (needtoresize())
resize();

data[listsize] = item;
listsize++;
}

template <class T>


void arraylist<T>::add(int index, T item)
{
// add item to the indicated index
// everything else get bumped down
if (needtoresize())
resize();

// 0 1 2 3 4
// 8 3 2

// insert 3 in location 1

// 0 1 2 3 4
// 8 3 3 2

for (int i = listsize; i >= index; i--){


data[listsize + 1] = data[listsize];

data[index] = item;
listsize++;
}
// remove
template <class T>
void arratlist<T>::remove(int index)
{
for (int i = index; i < listsize; i++)
data[i] = data[i + 1];
listsize--;
}
template <class T>
T arraylist<T>::get(int index)
{
if (index>=0 && index <= listsize)
return data[index];
// else
return 0;
}

template <class T>


void arraylist<T>::set(int index, T item)
{
if (index >= 0 && index <= listsize)
data[index] = item;
}

template <class T>


int arraylist<T>::indexof(T item)
{
for (int i = 0; i <= listsize; i++)

if (item == data[i])
return i;
// did not find return
return -1;
}

template <class T>


int arraylist<T>::lastindexof(T item)
{
for (int i = listsize; i <= 0; i--)

if (item == data[i])
return i;
// did not find return
return -1;
}

template <class T>


bool arraylist<T>::contains(T item)
{
return (indexof(item) > -1);
}

template <class T>


int arraylist<T>::size()
{
return listsize;
}
Main file

#include"Header.h"
#include <iostream>
using namespace std;

int main() {
// Array creation
int myarray[10];
int dataarray[10];
int size;
cout << "Enter 10 integers in any order: " << endl;
for (int i = 0; i < 10; i++) {
cin >> myarray[i];
cin >> dataarray[i];
}
cout << " Data Added" << endl;
for (int i = 0; i < 10; i++) {
cout << myarray[i] << " ";
}
// Menu
int choice;
cout << "\n\n1. for Add Data.\n2. for Delete data.\n3. for update.\n4. search
data\n5. for print \n\nEnter your Choice :";
cin >> choice;

switch (choice)
{
//cases....
case 1:
{
void add(int index);
break;
}

case 2:
{
void remove(int index);
break;
}
case 3:
{
void set(int index);
break;
}
case 4:
{
void get(int index);
break;
}
case 5:
{
void print(int index);
break;
}
default:
break;
}

cout << endl << "After Modifying" << endl;


for (int i = 0; i < 10; i++) {
cout << myarray[i] << " ";
}

return 0;
}

Conclusion
By doing this assignment with full effort and hard work I have learnt how to
create three file structure and the another thing are I learnt how to create the
classes and the third things is to create the Dynamic array and the forth things is
how to Add, delete, update, find, data in the Array. And the fifth things are to
create the MENU bar Program.

You might also like