Professional Documents
Culture Documents
Laboratory experiment #2
Lab Instructor:
Dr. SAHNOUN
Prepared by:
Tareq SI SALEM
Introduction:
A list is a tool that can be used to store multiple pieces of information at once. It can also be defined as a
variable containing multiple other variables. A list consists of numbers paired with items (items can take any
type).
In this laboratory experiment well deal with list in both their implementations (pointers or using arrays), but
with a constrain that the list must be kept in an increasing order for each operation we do on it.
Implementation:
#include <iostream>
#include <cstdio>
const int MAX = 10;
// MAX specifies the maximum size the list can take
// in the array case.
using namespace std;
namespace PartA {
// we used separate name spaces
// for array and pointer implementation
typedef struct List {
char *names[MAX];
// a string field to hold names.
int size;
// size of the List must not exceed MAX;
// usually with array lists (Like in Java) it has a capacity variable
// that increases whenever the list is near to be full
// and copies old data to a new array with a larger capacity
} List;
// list is passed by reference
// so a change that happens within the function
// changes the list (not its copy)
int init(List &l) {
l.size = 0;
// initialize the list by setting size to 0
// the list is initially empty
}
temp->next = item;
// old list cellp-1 -> cellp ->cellp+1
// new list cellp-1 -> newcell -> cellp->cellp+1
} else {
cout << "Cannot insert a new name to maintain the list order" << endl;
}
} else {
cout << "position out of list range" << endl;
}
}
}
// delete a cell from the list
//
void remove(LinkedList &list, int p) {
LinkedList temp = list;
int count = 0;
if (list != NULL) {
if (p == 0) {
LinkedList temp2 = temp;
temp = temp->next;
delete temp2;
} else {
while (count < p - 1 && temp != NULL) {
temp = temp->next;
count++;
}
if (temp != NULL) {
LinkedList temp2 = temp->next;
temp->next = temp2->next;
delete temp2;
} else {
cout << "position out of list range" << endl;
}
}
} else {
cout << "The list is empty" << endl;
}
}
//
int main() {
cout << "Where do you want to store names ?" << endl;
cout << "1. Array list " << endl;
cout << "2. Linked list" << endl;
char optionI, optionII;
char *name;
int p, index;
bool loop = true;
PartA::List arrayList;
PartA::init(arrayList);
PartB::LinkedList linkedList;
PartB::init(linkedList);
cout << "option: ";
cin >> optionI;
cout << "1. insert new item" << endl;
cout << "2. delete an item" << endl;
cout << "3. find an item" << endl;
cout << "4. print the list in increasing order" << endl;
cout << "5. print the list in decreasing order" << endl;
cout << "q. to exit" << endl;
while (loop) {
cout << "Select option: ";
cin >> optionII;
cin.ignore();
name = new char[10];
if (optionI == '1') {
switch (optionII) {
case '1':
cout << "Enter name: ";
cin.getline(name, 10);
cout << "Enter position: ";
cin >> p;
PartA::insert(arrayList, p, name);
break;
case '2':
cout << "Enter position: ";
cin >> p;
PartA::remove(arrayList, p);
break;
case '3':
cout << "Enter name to find: ";
cin.getline(name, 10);
index = PartA::find(arrayList, name);
if (index != -1) {
cout << "name found at: " << index << endl;
} else {
cout << "name not found" << endl;
}
break;
case '4':
PartA::traverseForward(arrayList);
break;
case '5':
PartA::traverseBackward(arrayList);
break;
case 'q':
loop = false;
break;
}
} else {
switch (optionII) {
case '1':
cout << "Enter name: ";
cin.getline(name, 10);
cout << "Enter position: ";
cin >> p;
PartB::insert(linkedList, p, name);
break;
case '2':
cout << "Enter position: ";
cin >> p;
PartB::remove(linkedList, p);
break;
case '3':
cout << "Enter name to find: ";
cin.getline(name, 10);
index = PartB::find(linkedList, name);
if (index != -1) {
cout << "name found at: " << index << endl;
} else {
cout << "name not found" << endl;
}
break;
case '4':
PartB::traverseForward(linkedList);
break;
case '5':
PartB::traverseBackward(linkedList);
break;
case 'q':
loop = false;
break;
}
}
cout << "------------------------" << endl;
}
return 0;
}
------------------------
1. Array list
Select option: 1
2. Linked list
option: 1
Enter position: 1
------------------------
2. delete an item
Select option: 4
3. find an item
------------------------
Select option: 5
q. to exit
Select option: 1
------------------------
Enter name: a
Select option: 2
Enter position: 0
Enter position: 0
------------------------
------------------------
Select option: 1
Select option: 4
Enter name: b
Enter position: 1
------------------------
------------------------
Select option: 3
Select option: 1
Enter position: 3
------------------------
Select option: 3
------------------------
Select option: 1
------------------------
Enter position: 2
Select option: q
------------------------
------------------------
Select option: 4
List(forward): [ a b caa ]
- Pointers Implementation
Where do you want to store names ?
------------------------
1. Array list
Select option: 1
2. Linked list
Enter name: ab
option: 2
Enter position: 1
------------------------
2. delete an item
Select option: 4
3. find an item
List (forward)[ a ab b c ]
------------------------
Select option: 2
q. to exit
Enter position: 1
Select option: 5
------------------------
Select option: 4
------------------------
List (forward)[ a b c ]
Select option: 1
------------------------
Enter name: a
Select option: 3
Enter position: 0
------------------------
Select option: 1
------------------------
Enter name: b
Select option: 3
Enter position: 1
------------------------
Select option: 1
------------------------
Enter name: c
Select option: 1
Enter position: 2
Enter name: e
------------------------
Enter position: 1
Select option: 4
List (forward)[ a b c ]
------------------------
------------------------
Select option: 5
Select option: q
List (backward)[ c b a ]
------------------------