You are on page 1of 15

Data Structures and Algorithms

CL210
LABORATORY MANUAL
Fall 2020

LAB 01
Design and Implementation of Link Lists and Its
Operations using C++
Engr. Muhammad Adan

USAMA ISHFAQ 19I0902


A

STUDENT NAME ROLL NO SEC

LAB ENGINEER SIGNATURE & DATE

MARKS AWARDED: /10


NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD

Prepared by: Engr. Shahid Qureshi Date: 30 January 2018


LAB: 01 Introduction to Linked Lists
1. Learning Objectives:
● To learn how to build a linked list, how to insert and delete new nodes in a linked list.

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.

● The first part contains the information of the element.


● The second part called the link field contains the address of the next node in the list.

Advantages and Disadvantages

● 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.

Basic Operations on Linked list

● 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";

cout << "\nEnter choice: ";


cin >> choice;
cout << "\n\n";

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

if (choice <0 || choice >10)


{
cout << "Invalid choice.";
}

if (choice == 1)
{

cout << "Enter number of float values to be appended in list : ";


cin >> total;

if (total >0)
{
cout << "\nEnter Values : \n\n";

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


{
cout << i+1 << ". ";
cin >> x;
list.appendNode(x);
}
cout << "\nData appended.";
}
}

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();
}

}while (choice != 0);

return 0;
}

FloatList::FloatList()
{
head = NULL;
}

FloatList::~FloatList()
{
nodeType *temp;
while(head != NULL)
{
temp = head;
head = head->link;
delete(temp);
}

cout << "List destroyed.";


}

void FloatList::appendNode(float a)
{
nodeType *newnode, *current;

newnode = new nodeType;


newnode->info = a;
newnode->link = NULL;

if (head == NULL)
head = newnode;

else
{
current = head;

while (current->link != NULL)


current = current->link;

current->link = newnode;
}
}

void FloatList::displayList()
{
nodeType *current;
current = head;

if (current == NULL)
cout << "List is empty.";

else
{
cout << "Stored List :\n\n";

while (current != NULL)


{
cout << current->info << '\t';
current = current->link;
}
}
}

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;

temp = new nodeType;


temp->info = a;
temp->link = head;
head = temp;
}

void FloatList::InsertAt(float a, int b)


{
nodeType *p, *q, *temp;

p = head;

temp = new nodeType;


temp->info = a;

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;

nodeType *p, *q;

p = head;
q = p->link;

sum = p->info;
maxima = p->info;
minima = p->info;

while (q != NULL)
{
sum = sum + q->info;

if (maxima < q->info)


{
maxima = q->info;
}

if (minima > q->info)


{
minima = q->info;
}

p = p->link;
q = q->link;
count++;
}

cout << "Total Values : " << count;


cout << "\nSum : " << sum;
cout << "\nAverage : " << sum/count;
cout << "\nMaximum Value : " << maxima;
cout << "\nMinimum Value : " << minima;

}
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;

cout << "Enter number of float values to be appended in list 1 : ";


cin >> total;

if (total >0)
{
cout << "\nEnter Values : \n\n";

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


{
cout << i+1 << ". ";
cin >> x;
list1.appendNode(x);
}
}

cout << "\n\nEnter number of float values to be appended in list 2 : ";


cin >> total;

if (total >0)
{
cout << "\nEnter Values : \n\n";
for (int i = 0; i < total; i++)
{
cout << i+1 << ". ";
cin >> x;
list2.appendNode(x);
}
}

cout << "\nList 1 :" << '\t'; list1.displayList();


cout << "\nList 2 :" << '\t'; list2.displayList();

cout << "\n";

if (list1 == list2)
{
cout << "Entered lists are equal.";
}
else
{
cout << "Entered lists are not equal.";
}

cout << "\n\n";

return 0;
}

FloatList::FloatList()
{
head = NULL;
}

void FloatList::appendNode(float a)
{
nodeType *newnode, *current;

newnode = new nodeType;


newnode->info = a;
newnode->link = NULL;

if (head == NULL)
head = newnode;

else
{
current = head;

while (current->link != NULL)


current = current->link;

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";
}
}

bool FloatList::operator==(FloatList &a)


{
int count1 = 0, count2 = 0, check = 1;

nodeType *p1, *p2;

p1 = head; p2 = a.head;

while (p1 != NULL)


{
count1++;
p1 = p1->link;
}

while (p2 != NULL)


{
count2++;
p2 = p2->link;
}

if (count1 != count2)
{
return 0;
}

if (count1 == count2)
{

p1 = head; p2 = a.head;

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


{
if (p1->info != p2->info)
{
check = 0;
}
else
{
p1 = p1->link;
p2 = p2->link;
}
}

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;

cout << "Enter number of float values to be appended in list 1 : ";


cin >> total;

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);

cout << "\n\nList 2 Created using copy constructor.\n\n";

cout << "\nList 1 :" << '\t'; list1.displayList();


cout << "\nList 2 :" << '\t'; list2.displayList();

cout << "\n";

if (list1 == list2)
{
cout << "Entered lists are equal.";
}
else
{
cout << "Entered lists are not equal.";
}

cout << "\n\n";

return 0;
}

FloatList::FloatList()
{
head = NULL;
}

void FloatList::appendNode(float a)
{
nodeType *newnode, *current;

newnode = new nodeType;


newnode->info = a;
newnode->link = NULL;

if (head == NULL)
head = newnode;

else
{
current = head;

while (current->link != NULL)


current = current->link;
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";
}
}

bool FloatList::operator==(FloatList &a)


{
int count1 = 0, count2 = 0, check = 1;

nodeType *p1, *p2;

p1 = head; p2 = a.head;

while (p1 != NULL)


{
count1++;
p1 = p1->link;
}

while (p2 != NULL)


{
count2++;
p2 = p2->link;
}

if (count1 != count2)
{
return 0;
}

if (count1 == count2)
{

p1 = head; p2 = a.head;

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


{
if (p1->info != p2->info)
{
check = 0;
}
else
{
p1 = p1->link;
p2 = p2->link;
}
}

return check;

}
}

FloatList::FloatList(const FloatList &a)


{
head = NULL;

nodeType *ptr = a.head;

while (ptr != NULL)


{
this->appendNode(ptr->info);
ptr = ptr->link;
}
}

You might also like