0% found this document useful (0 votes)
58 views14 pages

Group C Assign 19 Pranav Aher

The document is a student assignment on implementing operations on a singly linked list in C++. It includes code to: 1) Create a Node struct and LinkedList class for the list. 2) Implement functions to insert nodes, search, remove nodes, and display the list. 3) Main function tests the functions by creating a list and performing sample operations on it.

Uploaded by

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

Group C Assign 19 Pranav Aher

The document is a student assignment on implementing operations on a singly linked list in C++. It includes code to: 1) Create a Node struct and LinkedList class for the list. 2) Implement functions to insert nodes, search, remove nodes, and display the list. 3) Main function tests the functions by creating a list and performing sample operations on it.

Uploaded by

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

Group C

Assignment No 8

Name: Pranav Sanjay Aher


Roll no.: B01
Subject: DSL
Group: C Roll No: B01
Assignment No: 8 Date:

Title:
Write C++ program to implement Singly Linked List and operations on it-

a) Add and delete node


b) Compute total number of nodes
c) Traverse List
d) Display list in reverse order using recursion
e) Concatenate two lists

Marks: Sign:
Input:
/*
#include <iostream>
using namespace std;
int main()
{
int *ptr = NULL; // Pointer initialized with null
ptr = new int; // Request memory for the variable
*ptr = 12345; // Store value at allocated address
cout << "Value of Pointer Variable *ptr : " << *ptr << endl;
cout << "address of Pointer Variable *ptr : " << ptr;
delete ptr; // free up the memory.
return 0;
}
*/

//Dynamic Memory Allocation for Objects


#include <iostream>
using namespace std;
class Test
{
int maths;
public:
Test()
{
cout << "Constructor called..." <<endl;
}
~Test()
{
cout << "Destructor called!!" <<endl;
}
};
int main( )
{

Test* t = new Test;


delete t;
return 0;
}

/*
// Making a node struct containing a data int and a pointer
// to another node
struct Node {
int data;
Node *next;
};

class LinkedList
{
// Head pointer
Node* head;

public:
// default constructor. Initializing head pointer
LinkedList()
{
head = NULL;
}

// inserting elements (At start of the list)


void insert(int val)
{
// make a new node
Node* new_node = new Node;
new_node->data = val;
new_node->next = NULL;

// If list is empty, make the new node, the head


if (head == NULL)
head = new_node;
// else, make the new_node the head and its next, the previous
// head
else
{
new_node->next = head;
head = new_node;
}
}

// loop over the list. return true if element found


bool search(int val)
{
Node* temp = head;
while(temp != NULL)
{
if (temp->data == val)
return true;
temp = temp->next;
}
return false;
}

void remove(int val)


{
// If the head is to be deleted
if (head->data == val)
{
delete head;
head = head->next;
return;
}

// If there is only one element in the list


if (head->next == NULL)
{
// If the head is to be deleted. Assign null to the head
if (head->data == val)
{
delete head;
head = NULL;
return;
}
// else print, value not found
cout << "Value not found!" << endl;
return;
}

// Else loop over the list and search for the node to delete
Node* temp = head;
while(temp->next!= NULL)
{
// When node is found, delete the node and modify the pointers
if (temp->next->data == val)
{
Node* temp_ptr = temp->next->next;
delete temp->next;
temp->next = temp_ptr;
return;
}
temp = temp->next;
}

// Else, the value was neve in the list


cout << "Value not found" << endl;
}

void display()
{
Node* temp = head;
while(temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {

LinkedList l;
// inserting elements
l.insert(6);
l.insert(9);
l.insert(1);
l.insert(3);
l.insert(7);
cout << "Current Linked List: ";
l.display();

cout << "Deleting 1: ";


l.remove(1);
l.display();
*/

Output:

1. Add Member
2. Display Members
3. Display total number of members of club
4. Delete Member
5. Concatenate two lists
6. Exit
Enter your choice : 1

Enter PRN of member : 763728

Enter Name of member : pranav

President Added to Club


1. Add Member
2. Display Members
3. Display total number of members of club
4. Delete Member
5. Concatenate two lists
6. Exit
Enter your choice : 2

Pinnacle Club Members :


PRN MEMBER NAME
763728 pranav - President
1. Add Member
2. Display Members
3. Display total number of members of club
4. Delete Member
5. Concatenate two lists
6. Exit
Enter your choice : 3

Total members of Pinnacle club : 1


1. Add Member
2. Display Members
3. Display total number of members of club
4. Delete Member
5. Concatenate two lists
6. Exit
Enter your choice :

You might also like