0% found this document useful (0 votes)
4 views10 pages

Dsa #02

The document provides a C++ implementation of a singly linked list with various functionalities such as insertion, deletion, searching, and updating elements. It includes a main menu for user interaction to perform operations on the linked list. The code demonstrates how to manage the list's size and clear it when needed.

Uploaded by

sa8349964
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)
4 views10 pages

Dsa #02

The document provides a C++ implementation of a singly linked list with various functionalities such as insertion, deletion, searching, and updating elements. It includes a main menu for user interaction to perform operations on the linked list. The code demonstrates how to manage the list's size and clear it when needed.

Uploaded by

sa8349964
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/ 10

[Document title] [Document subtitle]

[Draw your reader in with an engaging abstract. It is typically a short


summary of the document. When you’re ready to add your content, just
click here and start typing.]
Question #01:

Using Singly Linked List, make a list in which you have to use all the list function which can be made for
example.

Source code:

#include <iostream>

using namespace std;

class node {

public:

int obj;

node* nextnode;

void setobj(int o) {

obj = o;

int getobj() {

return obj;

void setnext(node* nn) {

nextnode = nn;

node* getnext() {

return nextnode;

};

class list {

public:

node* hn;

node* cn;

node* lcn;

node* nn;

1
int size;

list() {

hn = new node();

hn->setnext(NULL);

cn = NULL;

lcn = NULL;

size = 0;

void mainmenu() {

cout << "Assignment 2" << endl;

cout << "______________" << endl;

cout << "\t\t\t\t\t\t\t\t\t\t\t\t" << endl;

cout << "Press 1 for create the list" << endl;

cout << "Press 2 for show the list" << endl;

cout << "Press 3 for find value the list" << endl;

cout << "Press 4 for remove value the list" << endl;

cout << "Press 5 for clear the list" << endl;

cout << "Press 6 for get the element from the list" << endl;

cout << "Press 7 for update the element from the list" << endl;

cout << "Press 8 for size of the list" << endl;

cout << "Press 9 for start of the list" << endl;

cout << "Press 10 for last element of the list" << endl;

cout << "Press 11 for copy of the the list" << endl;

void insert() {

int value;

nn = new node();

cout << "Enter the value: ";

cin >> value;

2
nn->setobj(value);

if (cn != NULL) {

nn->setnext(cn->getnext());

cn->setnext(nn);

lcn = cn;

cn = nn;

} else {

nn->setnext(NULL);

hn->setnext(nn);

lcn = hn;

cn = nn;

size++;

cout<<"size of the list is="<<size<<endl;

void start() {

lcn = hn;

cn = hn->getnext();

void show() {

start();

while (cn != NULL) {

cout << cn->getobj() << endl;

cn = cn->getnext();

int find(int value) {

start();

3
while (cn !=NULL) {

if (cn->getobj() == value) {

cout << "Value found in the list" << endl;

return value;

lcn = cn;

cn = cn->getnext();

cout << "Value not found in the list" << endl;

return -1;

int remove(int value) {

if (find(value) == -1) {

cout << "Value not found in the list, so nothing to remove" << endl;

return -1;

if (lcn != hn) {

lcn->setnext(cn->getnext());

delete cn;

cn = lcn->getnext();

} else {

hn->setnext(cn->getnext());

delete cn;

cn = hn->getnext();

size--;

cout << "Value removed from the list" << endl;

cout << "Updated list: " << endl;

4
show();

return value;

void clear() {

start();

while (cn != NULL) {

lcn = cn;

cn = cn->getnext();

delete lcn; // Delete the node

hn->setnext(NULL);

size = 0;

cn = NULL;

lcn = NULL;

cout << "List cleared." << endl;

cout<<"size of the list is="<<size;

int get(){

if(cn!=NULL){

cout<<"the element is "<<cn->getobj();

return cn->getobj();

else

cout<<"list is empty";

return -1;

void update(int oldValue, int newValue) {

if (find(oldValue) == -1) {

cout << "Value not found in the list, cannot update." << endl;

5
} else {

cn->setobj(newValue);

cout << "Value updated in the list" << endl;

show();

int lenght(){

return size;

int start1() {

if (hn != NULL) {

lcn = hn;

cn = hn->getnext();

return cn->getobj();

} else {

cout << "List is empty" << endl;

return -1;

int end() {

if(cn->getnext()==NULL){

return cn->getobj();

void copy(list&l1,list&l2){

l2=l1;

l2.show();

6
};

int main() {

list l1;

int choice;

do {

l1.mainmenu();

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter the value to insert" << endl;

l1.insert();

break;

case 2:

cout << "Show the values in the list" << endl;

l1.show();

break;

case 3:

int value;

cout << "Find a value in the list" << endl;

cin>>value;

l1.find(value);

break;

case 4:

int v;

cout << "Enter the value to remove from the list" << endl;

cin >> v;

7
l1.remove(v);

break;

case 5:

l1.clear();

break;

case 6:

l1.get();

break;

case 7:

int oldValue, newValue;

cout << "Enter the old value to update: ";

cin >> oldValue;

cout << "Enter the new value: ";

cin >> newValue;

l1.update(oldValue, newValue);

break;

case 8:

cout<<"lenght of the list is="<<l1.lenght();

break;

case 9:

cout<<"first element in the list is is="<<l1.start1();

break;

case 10:

cout<<"last element in the list is is="<<l1.end();

break;

case 11:

list l2;

l1.copy(l1,l2);

break;

8
}

} while (choice != 11);

return 0;

You might also like