Updated DS Lab 2 - List Implementation Using Arrays
Updated DS Lab 2 - List Implementation Using Arrays
Lab-2
List Implementation using Arrays
Table of Contents
1. Introduction 19
1.1 Lists and Dynamic Lists 19
1.2 Relevant Lecture Readings 19
4. Concept Map 20
4.1 Dynamic Lists in C++ using Arrays 20
6. Procedure& Tools 23
6.1 Tools 23
6.2 Walk through Tasks [Expected time = 20mins] 23
7. Practice Tasks 24
7.1 Practice Task 1 [Expected time = 15mins] 24
7.2 Practice Task 2 [Expected time = 20mins] 26
7.3Practice Task 3 [Expected time = 20mins] 27
7.4 Out comes 27
7.6Testing 27
9. Evaluation criteria 28
1. Introduction
In this lab, you will learn about the dynamic list implementation using arrays in C++, ADT
(Abstract Data Types) and Stack ADT. A list is a collection of items of the same type. The list can
be based on numbers (natural or floating-point), text, dates, times, or such values as long as the
items are of the same type.An abstract data type (ADT) is a precise model for a certain class of
data structures that have similar actions; or for definite data types of one or more programming
languages that have similar semantics. Stack is a data structure which allows placement of
elements in Last in First out (LIFO) fashion.
Lists are such data structures which are used to maintain elements of same data type in a
linear/sequential way. A list or series is an abstract data type that implements an ordered collection
of values, where the same value can occur more than one time. Static list structures allow only
inspection and enumeration of the values of its elements. A dynamic list may allow items to be
inserted, replaced, or deleted during the list's existence. Size of dynamic lists can be changed
during program execution.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.
Dynamic list in C++ can be designed using arrays. We may add or remove elements from
this list during execution of program and arrays size can controlled at program execution time.
public:
CListOfNumbers();
~CListOfNumbers();
};
You are required to count the number of elements in list after performing each add or delete
operation on list.
class CListOfNumbers
{
private:
double Item[MaxItems];
int Size;
public:
int Count() const;
CListOfNumbers();
};
CListOfNumbers::CListOfNumbers()
{
Size = 0;
}
intCListOfNumbers::Count() const
{
return Size;
}
Creating a list, consists of adding elements to it. Elements are usually added one at a time
and easiest way to do this is to add an item at the end of the list.
To add an item to the list, you first need to test whether the list is already full or not. A list
is full if its count of items is equal to or higher than the maximum number you had set. If the list
is not empty, you can add an item at the end and increase the count by one. Following code segment
represent this functionality.
Item[size] = item;
size++;
return true;
}
return false;
}
After adding elements to a list, you can retrieve them to do what you intended the list for.
To retrieve an item, you can locate an item by its position. By using index of this array, you can
check if the position specified is negative or higher than the current total of elements. It means
index is out of range. If the index is in the right range, you can retrieve its item against that index.
Following code segment represents this functionality.
return 0;
}
Inserting a new item in the list allows you to add it at a position of you chooses. To insert
a new element in the list, you must provide the new item and the desired position. Before
performing this operation, you must first check two conditions. First, the list must not be empty.
Second, the specified position must be in the valid range. Following code segment provides
implementation of this method.
Item[pos] = item;
size++;
return true;
}
return false;
}
Another operation you may perform on a list consists of deleting an element. To delete an
item from the list, you need to provide its position in the list. Before performing the operation, you
can first check that the specified position is valid. Following code segment provides
implementation.
size--;
return true;
}
return false;
}
After studying the introduction and concept map sections you should be ready to provide
the solution of following problems. Design the solutions of the problems in C++ and bring
your code to lab so that lab instructor should access and grade it.
5.2Problem description:
Design a dynamic list using array whose each element is an object of “person” class.
“person” class should have some privately defined attributes: per_id (int),
per_name(string) and per_age (int). Some member functions which are defined publicly
are: constructor function which should initialize the attributes of “person” object using
blank and zero values, input() function which should allow user to provide input for
attributes of object, output() function which should allow user to display the values of
attributes of object. This dynamic array should allow insertion, removal, count of total
element and displaying the values of elements of list.
5.3.1 Task-1
Make a list of at least 5 benefits we may get while using dynamic lists.
5.3.2 Task-2
Provide comparison between implementation of lists using static and dynamic arrays.
6. Procedure& Tools
This section provides information about tools and programming procedures used for the
lab.
6.1 Tools
Microsoft Visual Studio 2017 with Visual C++ compiler configured.
Operations related to list such as addition, removal, element count and element retrieval
are implemented in this program as well. Figure 3 shows some of these operations.
Figure 3: Insert operation on lists using arrays in Microsoft Visual Studio 2017.
7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and place solution code in a
folder specified by your lab instructor.
Lab instructors are guided to help students learn how ACM problems work and provide
students with certain practice/home tasks on the pattern of ACM Problems.
int *elements;
int size;
int length;
////////////////////////////////// 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
if(!isEmpty())
{
for(int i=0;i<length;i++)
{
//implement your logic here
}
}
Create a list of Employees (List can be as long as user wants). Program should save the
following information for each Employee: Name, Emp. No., Experience, Designation and
Salary. Now perform the following tasks:
a) Your program should save the information of only those employees whose experience is
at least 2 years.
b) Display only those Employees whose Salary is greater than 50,000
c) Display the Employees alphabetically
After completing this lab, student will be able to understand the concepts related to dynamic list
using arrays and stack abstract data type. They will be able to develop programs related to dynamic
lists using arrays in C++ using Microsoft Visual Studio 2017 environment.
7.6Testing
Test Cases for Practice Task-1
Sample Input Sample Output
2
3
4
6
9
Delete an element from Element deleted
location: 3
Enter a value to search in 9 is at location 3 in list
list: 9
Sample Sample
Inputs-1 Outputs-1
Input elements in list:
Bookno : B123
Title: C++ programming
Price: 234.95
Author: Jone Sage
Bookno: B345
Title: Object Oriented Programming in C++
Price: 235.95
Author: B J Shamy
Bookno: B133
Title: Data Structures using C and C++
Price: 400.35
Author: Dietel J.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).