0% found this document useful (0 votes)
31 views

DLL Lab Report

The document discusses implementing a linked list in C++. It includes creating a Node class with data and pointer to next node. It also includes a Linkedlist class with methods to insert, remove, traverse and print nodes. These are tested in a main function by creating a linked list, inserting and deleting nodes, and printing the list.

Uploaded by

Arooj Abbas
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)
31 views

DLL Lab Report

The document discusses implementing a linked list in C++. It includes creating a Node class with data and pointer to next node. It also includes a Linkedlist class with methods to insert, remove, traverse and print nodes. These are tested in a main function by creating a linked list, inserting and deleting nodes, and printing the list.

Uploaded by

Arooj Abbas
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/ 6

DATA STRUCTURE AND ALGORITHM

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;

// Node class to represent //


a node of the linked list.
class Node {
public: int
data;
Node* next;

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

// Function to print the


// linked list. void
printList();

// Function to delete the


// node at given position
void deleteNode(int);
};

// Function to delete the // node at given


position void Linkedlist::deleteNode(int
nodeOffset)
{
Node *temp1 = head, *temp2 = NULL;
int ListLen = 0;

if (head == NULL) { cout


<< "List empty." << endl;
return;
}

// Find length of the linked-list.


while (temp1 != NULL) {
temp1 = temp1->next;
ListLen++;
}

// Check if the position to be


// deleted is greater than the length
// of the linked list. if
(ListLen < nodeOffset) {
cout << "Index out of range"
<< endl; return;
}

// Declare temp1
temp1 = head;

// Deleting the head.


if (nodeOffset == 1) {
// Update head
head = head->next;
delete temp1;
return;
}

// Traverse the list to // find


the node to be deleted. while
(nodeOffset-- > 1) {

// Update temp2
temp2 = temp1;

// Update temp1
temp1 = temp1->next;
}

// Change the next


pointer // of the previous
node. temp2->next = temp1-
>next;

// Delete the node


delete temp1;
}
// Function to insert a new node. void
Linkedlist::insertNode(int data)
{
// Create the new Node.
Node* newNode = new Node(data);

// Assign to head if
(head == NULL) {
head = newNode;
return; }

// Traverse till end of list


Node* temp = head; while
(temp->next != NULL) {

// Update temp
temp = temp->next;
}

// Insert at the last.


temp->next = newNode;
}

// Function to print the //


nodes of the linked list.
void Linkedlist::printList()
{
Node* temp = head;
// Check for empty list. if
(head == NULL) { cout <<
"List empty" << endl; return;
}

// Traverse the list. while


(temp != NULL) { cout <<
temp->data << " ";
temp = temp->next;
}
}

// Driver Code
int main() {
Linkedlist list;

// Inserting nodes
list.insertNode(1); list.insertNode(2);
list.insertNode(3); list.insertNode(4);

cout << "Elements of the list are: ";

// Print the list


list.printList();
cout << endl;

// Delete node at position 2.


list.deleteNode(2);

cout << "Elements of the list are: ";


list.printList(); cout << endl;
return 0;
}
Output:

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

cout << "Elements of the list are: ";

// Print the list


list.printList();
cout << endl;

// Delete node at position 2.


list.deleteNode(2);

cout << "Elements of the list are: ";


list.printList();
cout << endl;
return 0;
}

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

You might also like