0% found this document useful (0 votes)
105 views12 pages

DS Lab 2 Tasks

The document describes a C++ program that implements functions to insert and delete values from an array-based list data structure. The program defines a class called "array" that contains a private integer array and size variable to store the list items and length. Public insert and remove functions are provided to add and remove values from the list by index, and a show function prints the current list. The main function contains a menu loop that calls the class functions to test inserting, removing and displaying values in the list.

Uploaded by

ASIM ZUBAIR 111
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)
105 views12 pages

DS Lab 2 Tasks

The document describes a C++ program that implements functions to insert and delete values from an array-based list data structure. The program defines a class called "array" that contains a private integer array and size variable to store the list items and length. Public insert and remove functions are provided to add and remove values from the list by index, and a show function prints the current list. The main function contains a menu loop that calls the class functions to test inserting, removing and displaying values in the list.

Uploaded by

ASIM ZUBAIR 111
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/ 12

Mohammad Abdullah Tariq

BCS 173083
DS Lab 2 – S6
24/09/2018

Class task: Insert and Delete

#include <iostream>

using namespace std;

class array{

private:

int item [20];

int size;

public:

array(){

for (int i=0; i<20; i++)

item[i]=0;

size=0;

insert(){

if (size < 20 ){

cout << "Enter value \n";

int num;

cin >> num;


item[size]=num;

size++;

remove(){

int del_index;

cout << "enter index you want to delete value from\n";

cin >> del_index;

if ( del_index >= size ) cout << "Invalid index \n";

if( del_index < size){

while( del_index != size){

item[del_index]=item[del_index+1];

del_index++;

item[size]=0;

size--;

show(){

cout << "current list: ";

for(int i=0; i<size; i++)

cout << item[i] << " ";

cout << "\n\n\n";

};

int main(){
int menu;

array a1;

do{

cout << "Press \n 1 to insert value\n 2 to remove value\n 3 to exit menu\n";

cin >> menu;

if (menu == 1 ){

a1.insert();

a1.show();

if (menu ==2 ){

a1.remove();

a1.show();

if (menu == 3 ) cout << "Exit from Program \n";

}while(menu != 3 );

return 0;

7.1
#include <iostream>

using namespace std;

class List{

int *elements;

int size;
int length;

public:

////////////////////////////////// Constructor

List(int maxsize)

size=maxsize;

elements=new int[size];

length=0;

/////////////////////////////////// Destructor

~List ()

delete []elements;

/////////////////////////////////// Output the list structure

void showStructure ()

if(!isEmpty())

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

cout<<"Element:"<<elements[i]<<endl;

else

cout<<"Display: No items to be displayed. List is empty\n";

}
////////////////////////////////// List manipulation operations

// Insert after cursor

void insert (int newDataItem)

if(!isFull())

elements[length]=newDataItem;

length++;

else

cout<<"Insert: Cannot insert more items. List is full\n";

// Remove data item

int remove ()

if(!isEmpty())

length--;

return elements[length];

else

cout<<"Remove: Cannot remove the item. List is empty\n";

// Replace data item

void replace ( int newDataItem, int position )


{

//Condition: List contains at least position+1 data items.

//Result: Removes the data item present at the position from a list and replace the number
requested by user at its position.

if(length<position)

cout<<"Replace: Cannot replace the item. Invalid position requested\n";

else

//implement your logic here

// find any number

bool find ( int searchDataItem )

//Condition: List is not empty.

//Result: Check if the number is present in the list or not

bool opt;

if(!isEmpty())

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

if ( elements[i] == searchDataItem )

opt = 1;

if (opt) return 1;

}
/////////////////////////////////////// List status operations

// check if List is empty

bool isEmpty ()

if (length==0) return 1;

else

return 0;

// check if List is full

bool isFull ()

if (length == size) return 1;

else

return 0;

};

int main(){

List listobj(3);

listobj.insert(20);

listobj.showStructure();

listobj.find(2);
return 0;

7.2
#include <iostream>

using namespace std;

class item {

private:

int list[20];

int size;

public:

item() {

for (int i = 0; i<20; i++)

list[i] = 0;

size = 0;

void insert() {

if (size < 20) {

cout << "Enter value \n";

int num;

cin >> num;

list[size] = num;

size++;

cout << "Value inserted \n";

}
}

void remove() {

if (size<20 && size >0) {

list[size] = 0;

size--;

cout << "Value deleted \n";

void insert_at_index() {

int in_index, in_val, temp1, temp2;

if (size < 19) {

cout << "enter index you want to insert value on\n";

cin >> in_index;

if (in_index >= size) cout << "Invalid index \n";

else

cout << "Enter value at index " << in_index << endl;

cin >> in_val;

temp1 = list[in_index]; list[in_index] = in_val;

while (in_index != size) {

temp2 = list[in_index + 1];

list[in_index + 1] = temp1;

temp1 = temp2;

in_index++;

}
size++;

cout << "Value inserted \n";

void remove_at_index() {

int del_index;

cout << "enter index you want to delete value from\n";

cin >> del_index;

if (del_index >= size) cout << "Invalid index \n";

if (del_index < size) {

while (del_index != size) {

list[del_index] = list[del_index + 1];

del_index++;

list[size] = 0;

size--;

cout << "Value deleted \n";

void show() {

cout << "current list: ";

for (int i = 0; i<size; i++)

cout << list[i] << " ";


cout << "\n\n\n";

void length() {

cout << "current Length of list: " << size << endl;

};

void main() {

item a1;

int menu;

do {

cout << "Press \n 1 to insert value (sequentially)\n 2 to delete value (sequentially)\n";

cout << " 3 to Insert value at specific index \n 4 to delete value from specific index \n";

cout << " 5 to get length of list \n 6 to display current values in list \n 7 to exit menu\n";

cin >> menu;

if (menu == 1) {

a1.insert();

a1.show();

if (menu == 2) {

a1.remove();

a1.show();

if (menu == 3) {

a1.insert_at_index();

a1.show();

if (menu == 4) {
a1.remove_at_index();

a1.show();

if (menu == 5)

a1.length();

if (menu == 6)

a1.show();

if (menu == 7) cout << "Exit from Program \n";

} while (menu != 7);

system("Pause");

You might also like