0% found this document useful (0 votes)
91 views9 pages

CS210 DSA Lab 05

This lab manual covers implementing a single linked list data structure in C++. The objectives are to understand the concept of a single linked list and implement it. The document provides code examples for a Node class to store data and a pointer to the next node. It also provides code for a Linked List class that implements various methods like insertion, deletion, searching and printing the linked list. The tasks involve implementing the Node class and Linked List class with the specified methods.

Uploaded by

Shehzad Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views9 pages

CS210 DSA Lab 05

This lab manual covers implementing a single linked list data structure in C++. The objectives are to understand the concept of a single linked list and implement it. The document provides code examples for a Node class to store data and a pointer to the next node. It also provides code for a Linked List class that implements various methods like insertion, deletion, searching and printing the linked list. The tasks involve implementing the Node class and Linked List class with the specified methods.

Uploaded by

Shehzad Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CS 210 – Data Structures and Algorithms Lab Manual

LAB NO. 05
SINGLE LINKED LIST
Following are the lab objectives:
Objectives
Lab

1. Understand single linked list concept


2. Implement single linked list concept

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

Lab Objectives and CLOs Mapping

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

SINGLE LINKED LIST


Singly (forward) Linked list a data structure to store data items in the form of connected nodes.
Each node contains address of next node, backward linkage is not available that is why we call it
forward linked list. These nodes are created on demand when a new data item has to be inserted
and deleted when existing data item has to be deleted.

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.

Single Linked List

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

(Paste your code here)


#include<iostream>
#include<conio.h>
using namespace std;
class Node
{
public:
Node();
void setdata(int data);
int getdata();
//void showNode();
Node* next;

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

Task 2 (100 mark)

Implement Linked List class in C++, which contains a pointer head as data member and declare
following member methods in the class;

1. Constructor: initialize data members of the class.


2. Destructor: deallocate the memory which was allocated dynamically.
3. isEmpty: check whether list is empty or not.
4. insertAtStart: insert a node at the start of linked list
5. insertAtEnd: insert a node at the end of linked list
6. insertNode: insert a node at a specified index of linked list
7. displayList: : print all the elements of a linked list
8. findNode: find a node in the linked list and return true if found, false otherwise
9. delateNode: delete a node from the linked list

(Paste your code here)


#include <iostream>
using namespace std;
class Node
{
public:
double data;
Node *next;

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

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


{
/*if (index<0 && index>NULL)
{
cout << "Index Does not Exist \n ";
}*/
currNode = currNode->next;
}
tempNode->next = currNode->next;

6
CS 210 – Data Structures and Algorithms Lab Manual

currNode = tempNode->next;
return currNode;
}

void List::DisplayList() //Display List


{
Node* currNode = head;
int n = 0;
while (currNode != NULL)
{
cout << currNode->data << endl;
currNode = currNode->next;
n++;
}
cout << "Number of nodes in the list: " << n << endl;
}

List::~List(void)
{
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
//destroy the current node
delete currNode;
currNode = nextNode;
}
}

int List::FindNode(double x) //Find List


{
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x)
{
currNode = currNode->next;
currIndex++;
}
if (currNode) return currIndex;
return 0;
}
int List::DeleteNode(double x) //Delete List
{
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x)
{
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode)
{
if (prevNode)
{
prevNode->next = currNode->next;

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.

You might also like