CS210 DSA Lab 05
CS210 DSA Lab 05
LAB NO. 05
SINGLE LINKED LIST
Following are the lab objectives:
Objectives
Lab
ABDULLAH
Student ID 2636
Student Name
Obtained
Marks Comments
Marks
Task 1 10
Task 2 100
Total
110
Marks
Lab Instructor
Date: 11-10-2019
1
CS 210 – Data Structures and Algorithms Lab Manual
CLOs
Lab Objectives
a b C
1
2
Instructions
This is individual Lab work/task.
Complete this lab work within lab timing.
Discussion with peers is not allowed.
You can consult any book, notes & Internet.
Copy paste from Internet will give you negative marks.
Lab work is divided into small tasks, complete all tasks sequentially.
Show solution of each lab task to your Lab Instructor.
In-Lab Exercises/Tasks
Write your code at provided space after each question
You need to upload code for all tasks at Google Class.
2
CS 210 – Data Structures and Algorithms Lab Manual
SingleLinkedList is basically a wrapper class which contains two pointers which hold the
starting and ending addresses of the forward linked list and implementation of different
operations which have to be performed on linked list, e.g. insert a node in linked list, delete a
node form linked list, search a particular node from linked list, print complete records of linked
list, check whether linked list is empty or not, etc. A logical view of forward linked list is shown
in following figure.
Node
Data Next Data Next Data Next Data Next Data Next NULL
Operations:
Insert a node
Start / Head Delete a node End / Tail
pointer Print linked list, etc. pointer
LAB TASKS
Task 1 (10 mark)
Implement Node class in C++, which can store an integer data and a pointer to next node. Make
sure you define constructor of the class to initialize data member(s).
3
CS 210 – Data Structures and Algorithms Lab Manual
private:
int data;
};
void Node::showNode()
{
cout << data << endl;
}
Node::Node() //ByDefault constructor
{
data = 0;
}
void Node::setdata(int data)
{
this->data = data;
}
int Node::getdata()
{
return this->data;
}
int main()
{
Node abc;
int bc;
cout << "Enter values: ";
cin >> bc;
abc.setdata(bc);
//abc.showNode();
system("pause");
return 0;
4
CS 210 – Data Structures and Algorithms Lab Manual
Implement Linked List class in C++, which contains a pointer head as data member and declare
following member methods in the class;
};
class List
{
public:
List(void) // constructor
{
head = NULL;
}
~List(void); // destructor
bool IsEmpty();
Node* InsertNode(int index, double x); //Insert Node
void InseretatStart(double x);
void InseretatEnd(double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);
private:
Node* head;
};
bool List::IsEmpty()
{
5
CS 210 – Data Structures and Algorithms Lab Manual
if (head == NULL)
{
return true;
}
else
{
return false;
}
}
void List::InseretatStart(double x)
{
Node* tempNode = new Node;
tempNode->data = x;
if (head = NULL)
{
head = tempNode;
}
else
{
tempNode->next = head;
head = tempNode;
}
}
void List::InseretatEnd(double x)
{
Node* tempNode = new Node;
Node* currentNode = new Node;
tempNode->data = x;
if (head = NULL)
{
head = tempNode;
}
else
{
currentNode = head;
while (currentNode != NULL)
{
currentNode = currentNode->next;
}
currentNode->next = tempNode;
}
}
Node* List::InsertNode(int index, double x)
{
Node* currNode = new Node;
Node* tempNode = new Node;
currNode->next = head;
tempNode->data = x;
6
CS 210 – Data Structures and Algorithms Lab Manual
currNode = tempNode->next;
return currNode;
}
List::~List(void)
{
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
//destroy the current node
delete currNode;
currNode = nextNode;
}
}
7
CS 210 – Data Structures and Algorithms Lab Manual
delete currNode;
}
else
{
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
int main(void)
{
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if (list.FindNode(5.0) > 0) cout << "5.0 found" << endl;
else cout << "5.0 not found" << endl;
if (list.FindNode(4.5) > 0) cout << "4.5 found" << endl;
else cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
list.InseretatStart(10.1);
list.DisplayList();
list.InsertNode(1, 2);
list.IsEmpty();
system("pause");
return 0;
}
8
CS 210 – Data Structures and Algorithms Lab Manual
EXTRA TASKS
Task
1. Write a member method for class liked list, which prints the elements of linked list in
reverse order.
2. Write a member method for class liked list, which count odd number of elements present
int the linked list.