DLL Lab Report
DLL Lab Report
Lab Report no 3 :
Arooj Abbas
0044-Bsc-Engg-22
Submitted To :
Engr. Ghulam Jillani
EXPERIMENT NO 3
Practice the Concept of Linked List Objective:
➢ To be able to understand and implement linked list in C++.
➢ To familiarize students with the concept of linked lists and implement a singly linked list.
Lab Task:
T1.
Implement a class node with the following members: a.
A character array to store roll number.
b. A variable to store marks.
c. A pointer to next node in the list
d. Get and set methods
#include <iostream>
Using namespace std;
Class Node { Private:
Char rollNumber[20];
Int marks; Node*
nextNode;
Public:
Node(const char* rollNum, int marks) {
Strcpy(rollNumber, rollNum);
This->marks = marks; nextNode
= nullptr;
}
Const char* getRollNumber() {
Return rollNumber;
}
Void setRollNumber(const char* rollNum) {
Strcpy (rollNumber, rollNum);
}
Int getMarks() {
Return marks;
}
Void setMarks(int newMarks) {
Marks = newMarks;
}
Node* getNextNode() {
Return nextNode;
}
Void setNextNode(Node* newNode) { nextNode
= newNode;
}
};
Int main() {
Node studentNode(“ENGG-0047”,446);
Cout << “Roll Number: “ << studentNode.getRollNumber() << endl;
Cout << “Marks: “ << studentNode.getMarks() << endl;
Return 0;
}
}
Output
Explanation :
In this implement,the node class has private member roll number marks and next which
represents the roll number marks and a pointer add to the next node in the list respectively. the class
also provide public setter and getter methods for each
member:setrollnumber,setmarks,setnext,getrollnumber,getmarks,and getnext.The setters are used to
assign values to the private members,while tge getters are used to retreive the values from the private
members
Implement the list class of nodes. Your implementation should have the following
members.
a. A reference to contain the first node.
b. Constructor, destructor
c. Insert
d. Remove
e. Traverse
f. Find
g. Sort.
#include <iostream>
using namespace std;
// Default constructor
Node()
{
data = 0;
next = NULL;
}
// Parameterised Constructor
Node(int data)
{ this->data =
data; this->next =
NULL;
}
};
// Linked list class to //
implement a linked list.
class Linkedlist {
Node* head;
public: // Default
constructor
Linkedlist() { head = NULL; }
// Function to insert a
// node at the end of the
// linked list. void
insertNode(int);
// Declare temp1
temp1 = head;
// Update temp2
temp2 = temp1;
// Update temp1
temp1 = temp1->next;
}
// Assign to head if
(head == NULL) {
head = newNode;
return; }
// Update temp
temp = temp->next;
}
// Driver Code
int main() {
Linkedlist list;
// Inserting nodes
list.insertNode(1); list.insertNode(2);
list.insertNode(3); list.insertNode(4);
Explanation :
In C++ the linked list can be represented with a class and a Node class separately, which has two
members, namely data and a next pointer which points to the next node.
• InsertNode: In this article, insertion is done at the end of the list. Follow the steps to insert a
node in the linked listLet’s say, 4 is to be inserted on the existing linked list, i.e., 1 -> 2 ->
3.The resultant linked list will be 1 -> 2 -> 3 -> 4.
• To insert a new node traverse till the end of the list until NULL node is found.
• Create a new Node, and link the new node to the last node of the linked list.
DeleteNode: In this article, deletion is done using the index of the node. Follow the steps to delete a
node:
• If the node to be deleted is the head node, store the head in temp variable. Then update head
as head->next. Delete temp.
Write down a main function to test the above implementation of linked list methods? int
main()
{
Linkedlist list;
// Inserting nodes
list.insertNode(1); list.insertNode(2);
list.insertNode(3); list.insertNode(4);
Explanation :
In C++ the linked list can be represented with a class and a Node class separately, which has
two members, namely data and a next pointer which points to the next node.
• InsertNode: In this article, insertion is done at the end of the list. Follow the steps to insert a
node in the linked listLet’s say, 4 is to be inserted on the existing linked list, i.e., 1 -> 2 ->
3.The resultant linked list will be 1 -> 2 -> 3 -> 4.
• To insert a new node traverse till the end of the list until NULL node is found.
• Create a new Node, and link the new node to the last node of the linked list.
DeleteNode: In this article, deletion is done using the index of the node. Follow the steps to delete a
node:
• If the node to be deleted is the head node, store the head in temp variable. Then update head
as head->next. Delete temp.
• If the index of the node to be deleted is greater than the length of the list then return from the
function.
• Traverse till the node to be deleted. Delete the node, and link the previous node to the next
node of the deleted node..