Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Assignment # 4

Class: BSCS-2A/2C Date: 3rd May 2020


Teacher: Dr. Abbas Khalid Deadline: 8th May 2020
Topic: Arrays
------------------------------------------------------------------------------------
Please do not copy the code from others.

List Implementation
A simple list can be implemented using arrays. To declare a list, we will use following
statements:

const int size = 5;


int list[size];

Following operations can be performed on a list:

1. addElement  Adds a new element in the list


2. deleteElement  Removes an element from the list
3. countList  Returns number of elements in the list
4. displayList  Displays the contents of the list
5. searchElement  Searches the given element from the list
6. editElement  Modifies the element at specified position
7. deleteAll  Deletes all elements from the list
8. sortList  Sorts the elements in the list
9. reverseList  Reverses the contents of the list
10. copyList  Creates a copy of the current list

At any moment, the current status of the list can be inspected using a variable ‘count’. We
declare this variable as a global so that it can reflect the current situation of the list to every
module of the program.

int count = 0; // A global variable

Adding an Element in the List


Case 1: When there is a capacity to insert an element in the list i.e.
if(count < size)
1. Get the value from the user.
2. Insert this value at the position the count is pointing.
3. Increment the value of count so that it can point to the next location.

Case 2: When the list is already full i.e.


if(count >= size )

Display a message that “List is Full.”


0 1 2 3 4
11 12 13

count
Deleting an Element from the List
Case 1: When there is no element in the list i.e.
if(count == 0)

Display a message that “List is Empty.”

Case 2: When there is atleast one element in the list

1. Get the position of element to be deleted from the user.


2. Verify that at given position, a value exists i.e.
if(position < count) // In user’s point of view, position start from one

3. Replace the value of given position with the value of next position
4. Repeat (3) until end of the list (count - 1) is reached.
5. Decrement the value of the count variable.

Counting Elements in the List


The count variable keeps the track of number of values in the list. To find the number of elements in the
list, we will just return the value of count. It is a global variable and hence accessible within the entire
program.

Displaying Elements of the List


Case 1: When there is no element in the list i.e.
if (count == 0)

Display a message that “List is Empty.”

Case 2: When there is atleast one element in the list

Traverse the list until count - 1 is reached.


Searching Element in the List
1. Get the value to be searched from the user.
2. Traverse the list to find this value till the end of list is reached or the desired value is found.
3. If value is found, return the position of the value; else return -1.

Modifying a Value in the List


Get the position of the value to be modified from the user.

Case 1: When this position has some value i.e.


if(position < count)

1. Get the new value from the user.


2. Replace the old value with the new value.
Case 2: When this position is empty i.e.
if(position >= count)

Display a message that “Position is Empty.”

Deleting the entire List


Just set the count variable to zero.

Sorting the List


Bubble sort algorithm can be employed to sort the list. Before calling the sorting algorithm, make sure
that list contains atleast one element i.e.

if(count >= 1)
sortList(list);
else
cout<<”List is EMPTY”<<endl;

Reversing the List


Before reversing the list, make sure that list is not empty i.e.

if(count == 0)
cout<<”List is EMPTY”<<endl;
else
reverseList(List,size);

1. Declare two temporary variables temp1 and temp2.


2. Point temp1 to first element of the list i.e temp1 = 0;
3. Point temp2 to last element in the list i.e. temp2 = count-1;
4. If temp1 < temp2 then swap the values of temp1 and temp2.
5. Increment temp1
6. Decrement temp2
7. Repeat (4 – 6) till the condition temp1<temp2 fails.

Or

1. Declare a temporary array.


2. Copy the elements of the original list in temporary array in reverse order.
3. Copy the elements of temporary array in the original list.

Copying the List


1. Traverse the list and store each element of list in another array.
2. Display this new list

You might also like