19i0902 LAB 1 DS and Transform
19i0902 LAB 1 DS and Transform
19i0902 LAB 1 DS and Transform
CL210
LABORATORY MANUAL
Fall 2020
LAB 01
Design and Implementation of Link Lists and Its
Operations using C++
Engr. Muhammad Adan
2. Equipment Required:
A working computer having Visual Studio or any other good compiler Installed.
3. Introduction:
Linked list is a linear collection of data elements, called nodes, where the linear order is given by
means of “pointers”. Each node is divided into two parts.
● Linked lists are dynamic, so the length of a list can increase or decrease as necessary.
● Storage problems with fragmented memory
● Array requires contiguous space in memory. Array cannot dynamically grow incrementally.
Must de-allocate and reallocate the whole array to change size.
● Linked list solves the above problems. Doesn't require contiguous memory. Linked list is a
chain of pointer {connected nodes. Nodes store information just like array indices.
● Linked list access is slower than array. Must traverse the whole list to reach a particular node.
But, storage and size is flexible.
● Array is preferable when speed is desired and storage size is known.
Example: Number of students in a class cannot exceed 50. Create an array of 50 size for
students.
● Linked list is preferable when speed is not a major concern and storage size is highly
dynamic. Example: Daily record of cars using motorway. On weekends traffic is high but
there aren't many cars on weekdays. Numbers of cars can be highly varied.
● Adding a Node
● Deleting a Node
● Displaying Link List
Lab Tasks
1. Write a program to interact with a link list with the following menu
● AppendAtEnd
● DisplayAll
● Destructor
● Default Constructor
2. Make a function DeleteValue(float value), which should delete a node with the given value.
3. Make a function AddAtFront(), which should store the new value at first location. Use it to
store some values, and see the result by calling the DisplayAll function.
4. Make a function insertAt(float value, int location), which should insert a new node at the
given location.
5. Given a link list with some values (at least 10), write a program to find and print the sum,
avg, min and max values.
6. Overload equality = = operator for the link list class, if the contents of the two link lists are
same, then it should return true, else false
7. Copy Constructor(for when linked list is passed as an argument)
TASK 1-5:
#include <iostream>
using namespace std;
struct nodeType
{
float info;
nodeType *link;
};
class FloatList
{
private:
nodeType *head;
public:
FloatList();
~FloatList();
void appendNode(float a);
void displayList();
void DeleteValue(float a);
void AddAtFront(float a);
void InsertAt(float a, int b);
void ListDetails();
};
int main()
{
FloatList list;
int choice;
int total;
float x;
do
{
cout << "\n\n";
cout << "->1. AppendAtEnd \n";
cout << "->2. DisplayAll \n";
cout << "->3. Destructor \n";
cout << "->4. DeleteValue \n";
cout << "->5. AddAtFront \n";
cout << "->6. InsertAt \n";
cout << "->7. Display List Details \n";
cout << "->0. Exit \n";
if (choice == 1)
{
if (total >0)
{
cout << "\nEnter Values : \n\n";
if (choice == 2)
{
list.displayList();
}
if (choice == 3)
{
list.~FloatList();
}
if (choice == 4)
{
cout << "Enter Number to be deleted : ";
cin >> x;
list.DeleteValue(x);
}
if (choice == 5)
{
cout << "Enter new value to be added : ";
cin >> x;
list.AddAtFront(x);
total++;
}
if (choice == 6)
{
int y;
cout << "Enter new value to be added : ";
cin >> x;
cout << "Enter Location (2 to " << total << " ) : ";
cin >> y;
list.InsertAt(x, y);
total++;
}
if (choice == 7)
{
list.ListDetails();
}
return 0;
}
FloatList::FloatList()
{
head = NULL;
}
FloatList::~FloatList()
{
nodeType *temp;
while(head != NULL)
{
temp = head;
head = head->link;
delete(temp);
}
void FloatList::appendNode(float a)
{
nodeType *newnode, *current;
if (head == NULL)
head = newnode;
else
{
current = head;
current->link = newnode;
}
}
void FloatList::displayList()
{
nodeType *current;
current = head;
if (current == NULL)
cout << "List is empty.";
else
{
cout << "Stored List :\n\n";
void FloatList::DeleteValue(float a)
{
nodeType *p, *q, *temp;
if (head->info == a)
{
temp = head;
head = head->link;
delete (temp);
}
p = head; q = p->link;
while (q != NULL)
{
if (p->info == a)
{
temp = head;
head= head->link;
p = head;
q= p->link;
delete (temp);
}
if (q->info == a)
{
temp = q;
p->link = q->link;
q = q->link;
delete (temp);
}
else
{
p = p->link;
q = q->link;
}
}
}
void FloatList::AddAtFront(float a)
{
nodeType *temp;
p = head;
b--;
b--;
for (int i = 0; i < b; i++)
{
p = p->link;
}
q= p->link;
p->link = temp;
temp->link = q;
}
void FloatList::ListDetails()
{
int count=1;
float sum, maxima, minima;
p = head;
q = p->link;
sum = p->info;
maxima = p->info;
minima = p->info;
while (q != NULL)
{
sum = sum + q->info;
p = p->link;
q = q->link;
count++;
}
}
TASK 6:
#include <iostream>
using namespace std;
struct nodeType
{
float info;
nodeType *link;
};
class FloatList
{
private:
nodeType *head;
public:
FloatList();
void appendNode(float a);
void displayList();
bool operator == (FloatList& a);
};
int main()
{
FloatList list1;
FloatList list2;
int total;
float x;
if (total >0)
{
cout << "\nEnter Values : \n\n";
if (total >0)
{
cout << "\nEnter Values : \n\n";
for (int i = 0; i < total; i++)
{
cout << i+1 << ". ";
cin >> x;
list2.appendNode(x);
}
}
if (list1 == list2)
{
cout << "Entered lists are equal.";
}
else
{
cout << "Entered lists are not equal.";
}
return 0;
}
FloatList::FloatList()
{
head = NULL;
}
void FloatList::appendNode(float a)
{
nodeType *newnode, *current;
if (head == NULL)
head = newnode;
else
{
current = head;
current->link = newnode;
}
}
void FloatList::displayList()
{
nodeType *current;
current = head;
if (current == NULL)
cout << "List is empty.";
else
{
while (current != NULL)
{
cout << current->info << '\t';
current = current->link;
}
cout << "\n\n";
}
}
p1 = head; p2 = a.head;
if (count1 != count2)
{
return 0;
}
if (count1 == count2)
{
p1 = head; p2 = a.head;
return check;
}
}
TASK 7:
#include <iostream>
using namespace std;
struct nodeType
{
float info;
nodeType *link;
};
class FloatList
{
private:
nodeType *head;
public:
FloatList();
void appendNode(float a);
void displayList();
FloatList(const FloatList &a);
bool operator == (FloatList& a);
};
int main()
{
FloatList list1;
int total;
float x;
if (total >0)
{
cout << "\nEnter Values : \n\n";
for (int i = 0; i < total; i++)
{
cout << i+1 << ". ";
cin >> x;
list1.appendNode(x);
}
}
FloatList list2(list1);
if (list1 == list2)
{
cout << "Entered lists are equal.";
}
else
{
cout << "Entered lists are not equal.";
}
return 0;
}
FloatList::FloatList()
{
head = NULL;
}
void FloatList::appendNode(float a)
{
nodeType *newnode, *current;
if (head == NULL)
head = newnode;
else
{
current = head;
void FloatList::displayList()
{
nodeType *current;
current = head;
if (current == NULL)
cout << "List is empty.";
else
{
while (current != NULL)
{
cout << current->info << '\t';
current = current->link;
}
cout << "\n\n";
}
}
p1 = head; p2 = a.head;
if (count1 != count2)
{
return 0;
}
if (count1 == count2)
{
p1 = head; p2 = a.head;
return check;
}
}