You are on page 1of 12

Name: Mujtaba

Rollno:18L1360

DSA LAB (03)Report


Experiment #03

Singly Linked List

Introduction:
A singly linked list is a collection of components, called nodes. Every node
(except the last node) contains the address of the next node. Thus, every node in a
linked list has two components: one to store the relevant information (that is, data)
and one to store the address, called the link, of the next node in the list. The
address of the first node in the list is stored in a separate location, called the head
or first. Last node to list points to NULL.
Properties:
 Singly linked list can store data in non-contiguous locations.
 Insertion and deletion of values is efficient with respect to Time and
Space Complexity.
 Dynamic structure (Memory Allocated at run-time).
 Nodes can only be accessed sequentially. That means, we cannot jump to
a particular node directly.
 There is no way to go back from one node to previous one. Only forward
traversal is possible.

Objective:
 The objective of this lab is to implement Linear Singly Linked List.

Measurement:
Task:
#include<iostream>
using namespace std;
template <typename T>
class Node
{
private:
T data;
Node* link;
public:
Node();
Node(T element);
void setData(T pval);
T getData();
Node* GetNext()
{

return link;

}
void setNext(Node* x);
};
template <typename T>
Node<T>::Node()
{
data = 0;
link = NULL;
}
template <typename T>
Node<T>::Node(T element)
{
data = element;
link = NULL;
}
template <typename T>
void Node<T>::setData(T pval)
{
data = pval;
}
template <typename T>
T Node<T>::getData()
{
return data;
}
template <typename T>
void Node<T>::setNext(Node* x)
{
link = x;
}
template <typename T>
class List
{
private:
Node<T>* first;
public:
List();
void Insert(Node<T>* pBefore, Node<T>* pNew);
void printlist();
void Delete(Node<T>* pToBeDeleted);
void insert1(Node<T>* pBefore, Node<T>* pNew, Node<T>* pAfter);
};
template <typename T>
List<T>::List()
{
first = NULL;
}
template <typename T>
void List<T>::Insert(Node<T>* pBefore, Node<T>* pNew)
{
if (first == NULL)
{
first = pNew;
}
else if (first != NULL && pBefore == NULL)
{
pNew->setNext(first);
first = pNew;
}
else {
pNew->setNext(pBefore->GetNext());
pBefore->setNext(pNew);
}
}
template <typename T>
void List<T>::printlist()
{
Node<T>* temp;
temp = first;
int i = 0;
while (temp != NULL)
{
cout << "Node " << i + 1 << " have Value: " << temp->getData() <<
endl;
temp = temp->GetNext();
i++;
}
}
template <typename T>
void List<T>::Delete(Node<T>* pToBeDeleted)
{
Node<T>* temp;
temp = first;
if (first == pToBeDeleted)
{
first = pToBeDeleted->GetNext();
pToBeDeleted->setNext(NULL);
delete pToBeDeleted;
}
else
{
while (temp->GetNext() != pToBeDeleted)
{
temp = temp->GetNext();
}
temp->setNext(pToBeDeleted->GetNext());
pToBeDeleted->setNext(NULL);
delete pToBeDeleted;
}
}
template <typename T>
void List<T>::insert1(Node<T> * pBefore, Node<T> * pNew, Node<T> *
pAfter)
{

if (pBefore == NULL && pAfter == NULL)


{
cout << "Both Nodes Are Not exist " << endl;
}
else
{

if (pBefore != NULL && pAfter == NULL)


{
if (pBefore->GetNext() == NULL)
{
pBefore->setNext(pNew);
pNew->setNext(NULL);
}

}
else if (pBefore == NULL && pAfter != NULL)
{
if (pAfter == first)
{
pNew->setNext(first);
first = pNew;
}
else
{
cout << "Entered Node is Wrong " <<
endl;;
}
}
else
{
if (pBefore->GetNext() == pAfter)
{

pBefore->setNext(pNew);
pNew->setNext(pAfter);
}
else
{
cout << "These Nodes Are Node
adjacent: " << endl;
}
}
}

int main()
{
Node<int>* a, * b, * c, * d, * e,*nn;
a = new Node<int>(1);
b = new Node<int>(2);
c = new Node<int>(3);
d = new Node<int>(4);
e = new Node<int>(5);
nn = new Node<int>(400);
List<int>* list;
list = new List<int>();
list->Insert(0, a);
list->Insert(a, b);
list->Insert(b, c);
list->Insert(c, d);
list->Insert(d, e);
list->printlist();
/*list->Delete(a);
cout << "\nAfter deleting first node" << endl;
list->printlist();
// PLEASE ADD CALL TO MEMBER FUNCTION WHICH WOULD
DIFFER SECTION-WISE e.g. list->reverse etc.
//cout << "\nAfter calling function" << endl;
//list->printList();
list->Delete(e);
cout << "\nAfter deleting last node" << endl;
list->printlist();*/

/*cout << endl;


list->insert1(a, nn,c );
list->printlist();*/
system("pause");
return 0;
}

Issues:
No issue found during lab

Applications:
 Implementation of stacks and queues.
 Implementation of graphs : Adjacency list representation of graphs is most
popular which is uses linked list to store adjacent vertices.
 Dynamic memory allocation : We use linked list of free blocks.
 Maintaining directory of names.

Conclusions:
At last we get our desired results effectively and meet our objective.

Post Lab:

Code:
#include<iostream>
using namespace std;
template <typename T>
class Node
{
private:
T data;
Node* link;
public:
Node();
Node(T element);
void setData(T pval);
T getData();
Node* GetNext()
{
{
return link;
}
}
void setNext(Node* x);
};
template <typename T>
Node<T>::Node()
{
data = 0;
link = NULL;
}
template <typename T>
Node<T>::Node(T element)
{
data = element;
link = NULL;
}
template <typename T>
void Node<T>::setData(T pval)
{
data = pval;
}
template <typename T>
T Node<T>::getData()
{
return data;
}
template <typename T>
void Node<T>::setNext(Node* x)
{
link = x;
}

template <typename T>


class SCList
{
private:
Node<T>* last;
public:
SCList();
void Insert(Node<T>* pBefore, Node<T>* pNew);
void printlist();
void Delete(Node<T>* pToBeDeleted);

};
template <typename T>
SCList<T>::SCList()
{
last = NULL;
}

template <typename T>


void SCList<T>::Insert(Node<T>* pBefore, Node<T>* pNew)
{
if (last == NULL)
{
last = pNew;
last->setNext(last);
}
else if (last != NULL && pBefore == last)
{
pNew->setNext(last->GetNext());
last->setNext(pNew);
last = pNew;
}
else {
pNew->setNext(pBefore->GetNext());
pBefore->setNext(pNew);
}
}
template <typename T>

void SCList<T>::printlist()
{
if (last != NULL)
{
Node<T>* temp;
Node<T>* first;

first = last->GetNext();
temp = first->GetNext();
int i = 0;
cout << "Node " << "1 " << "have value: " << last->GetNext()-
>getData() << endl;
while (temp != first)
{
cout << "Node " << i + 2 << " have Value: " << temp-
>getData() << endl;
temp = temp->GetNext();
i++;
}
}
else
{
cout << "Sorry List have no Nodes Yet: " << endl;
}
}
template <typename T>

void SCList<T>::Delete(Node<T>* pToBeDeleted)


{
Node<T>* temp;
temp = last->GetNext();
if (pToBeDeleted != NULL && last != NULL)
{

while (temp->GetNext() != pToBeDeleted)


{
temp = temp->GetNext();
}

if (last == pToBeDeleted)
{
temp->setNext(pToBeDeleted->GetNext());
last = temp;
delete pToBeDeleted;
}
else
{
temp->setNext(pToBeDeleted->GetNext());
delete pToBeDeleted;
}
}
else {
cout << "Node Does Not Exist In Link List:" << endl;
}
}

int main()
{
Node<int>* a, * b, * c, * d, * e, * f, * g, * h, * i, * j, * aa, * bb, * cc, * dd, *
ee;
a = new Node<int>(100);
b = new Node<int>(200);
c = new Node<int>(300);
d = new Node<int>(400);
e = new Node<int>(500);
f = new Node<int>(600);
g = new Node<int>(700);
h = new Node<int>(800);
i = new Node<int>(900);
j = new Node<int>(1000);
aa = new Node<int>(1100);
bb = new Node<int>(1200);
cc = new Node<int>(1300);
dd = new Node<int>(1400);
ee = new Node<int>(1500);
SCList<int>* list;
list = new SCList<int>();
list->Insert(0, a);
list->Insert(a, b);
list->Insert(b, c);
list->Insert(c, d);
list->Insert(d, e);
list->Insert(e, f);
list->Insert(f, g);
list->Insert(g, h);
list->Insert(h, i);
list->Insert(i, j);
list->Insert(j, aa);
list->Insert(aa, bb);
list->Insert(bb, cc);
list->Insert(cc, dd);
list->Insert(dd, ee);

list->Delete(a);
list->Delete(ee);
list->Delete(dd);
list->printlist();

cout << endl;

system("pause");
return 0;
}

You might also like