Dsa #02
Dsa #02
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>
class node {
public:
int obj;
node* nextnode;
void setobj(int o) {
obj = o;
int getobj() {
return obj;
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 << "Press 3 for find value the list" << endl;
cout << "Press 4 for remove value 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 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();
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++;
void start() {
lcn = hn;
cn = hn->getnext();
void show() {
start();
cn = cn->getnext();
start();
3
while (cn !=NULL) {
if (cn->getobj() == value) {
return value;
lcn = cn;
cn = cn->getnext();
return -1;
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--;
4
show();
return value;
void clear() {
start();
lcn = cn;
cn = cn->getnext();
hn->setnext(NULL);
size = 0;
cn = NULL;
lcn = NULL;
int get(){
if(cn!=NULL){
return cn->getobj();
else
cout<<"list is empty";
return -1;
if (find(oldValue) == -1) {
cout << "Value not found in the list, cannot update." << endl;
5
} else {
cn->setobj(newValue);
show();
int lenght(){
return size;
int start1() {
if (hn != NULL) {
lcn = hn;
cn = hn->getnext();
return cn->getobj();
} else {
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();
switch (choice) {
case 1:
l1.insert();
break;
case 2:
l1.show();
break;
case 3:
int value;
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:
l1.update(oldValue, newValue);
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
list l2;
l1.copy(l1,l2);
break;
8
}
return 0;