Sinle LL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Untitled3 file:///home/pvg/Downloads/Untitled3(1).

html

In [ ]: #include<iostream>
using namespace std;

class node{
public:
int data;
node *next;
// pointer of node type
//as this pointer will point to the next node

node(int data=0){
this ->data=data;
this ->next=NULL;
}
};

void print(node * &head){


node *temp=head;
while(temp!=NULL){
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL";
cout<<endl;
}
//we have taken refrence to directely manupulate the data and not the copy
void InsertAtHead(node * &head){
int d;
cout<<"enter data : ";
cin>>d;
//create a new node
//node name temp is created
node *temp=new node(d);
temp->next=head;
head=temp;
}

void InsertAtEnd(node * &head){


int d;
cout<<"enter data : ";
cin>>d;
//new node is created which we will insert
node *temp=new node (d);
node *temp1=head;
while(temp1->next!=NULL){
temp1=temp1->next;

}
temp1->next=temp;
temp->next=NULL;
}

void Insert(node * &head1){


int position;
cout<<"enter position of insertion : ";
cin>>position;
int d;

1 of 6 23/11/23, 11:30
Untitled3 file:///home/pvg/Downloads/Untitled3(1).html

cout<<"enter data : ";


cin>>d;

node *temp=new node(d);


node *temp1=head1;
node *temp2=head1;
// cout<<"enter position : ";
// cin>>position;
// cout<<"enter data : ";
// cin>>d;
for(int i=0;i<position-1;i++){
temp1=temp1->next;
}
for(int i=0;i<position;i++){
temp2=temp2->next;
}

temp1->next=temp;
temp->next=temp2;
print(head1);

void deleteNode(node *&head){


int count=0;
int position;
cout<<"enter position of node which you want to delete : ";
cin>>position;
node *temp=head;
node *temp1=head;
while(count!=position){
temp=temp->next;
count++;
}
count=0;
while(count!=(position-1)){
temp1=temp1->next;
count++;
}
temp1->next=temp->next;
temp=nullptr;
print(head);
}
void updateNode(node *&head){
int count=0;
int position;
cout<<"enter position of updation : ";
cin>>position;
int d;
cout<<"enter new data : ";
cin>>d;
node *temp=head;

while(count<position){
temp=temp->next;
count++;
}
temp->data=d;
print(head);

2 of 6 23/11/23, 11:30
Untitled3 file:///home/pvg/Downloads/Untitled3(1).html

void countnodes(node * &head){


node *temp=head;
int count=0;
while(temp!=NULL){
count=count+1;
temp=temp->next;
}
cout<<"count is : "<<count<<endl;
}

void join(node *&head1,node *&head2){


//joining 2 linked lists
node *temp=head1;
while(temp->next !=NULL){
temp=temp->next;
}
temp->next=head2;

//printing the concat linked list


print(head1);
}

int main(){
/*
created an object of class node with name node1
and assigned a data value 10 to it .
created int the heap memory
*/
//initilizing the linked list
int a,b;
cout<<"enter the first element of the first linked list : ";
cin>>a;
cout<<"enter the first element of the another linked list : ";
cin>>b;

// using constructor
node *node1=new node(a);
node *node2=new node(b);
node *head1=node1;
node *head2=node2;
cout<<"\n*******---MENU---*******\n";
cout<<"0.exit\n1.insert at head\n2.insert at end\n3.insert inbetween\n4.updation o
int ch;
while(ch!=0){
cout<<"enter choice : ";
cin>>ch;

if(ch==1){
int z;
cout<<"\nin which linked list you want to insert ? \n";
cout<<"1.first\n2.second\n";
cin>>z;
if(z==1){InsertAtHead(head1);}
if(z==2){InsertAtHead(head2);}
}

3 of 6 23/11/23, 11:30
Untitled3 file:///home/pvg/Downloads/Untitled3(1).html

else if(ch==2){
int z;
cout<<"\nin which linked list you want to insert ? \n";
cout<<"1.first\n2.second\n";
cin>>z;
if(z==1){InsertAtEnd(head1);}
if(z==2){InsertAtEnd(head2);}
}
else if(ch==3){
int z;
cout<<"\nin which linked list you want to insert ? \n";
cout<<"1.first\n2.second\n";
cin>>z;
if(z==1){Insert(head1);}
if(z==2){Insert(head2);}
}
else if(ch==4){
int z;
cout<<"\nin which linked list ? \n";
cout<<"1.first\n2.second\n";
cin>>z;
if(z==1){updateNode(head1);}
if(z==2){updateNode(head2);}
}
else if(ch==5){
int z;
cout<<"\nin which linked list ? \n";
cout<<"1.first\n2.second\n";
cin>>z;
if(z==1){deleteNode(head1);}
if(z==2){deleteNode(head2);}
}
else if(ch==6){
int z;
cout<<"\nin which linked list ? \n";
cout<<"1.first\n2.second\n";
cin>>z;
if(z==1){print(head1);}
if(z==2){print(head2);}
}
else if(ch==7){
join(head1,head2);
}
else{
cout<<"pls select correct option (you had one job)!";
}
}
cout<<"thank you for using this program !!";

return 0;
}

/*output:
enter the first element of the first linked list : 11
enter the first element of the another linked list : 23

*******---MENU---*******
0.exit
1.insert at head

4 of 6 23/11/23, 11:30
Untitled3 file:///home/pvg/Downloads/Untitled3(1).html

2.insert at end
3.insert inbetween
4.updation of node
5.deletion of node
6.print linked list
7.Concatenate two lists
enter choice : 1

in which linked list you want to insert ?


1.first
2.second
1
enter data : 23
enter choice : 1

in which linked list you want to insert ?


1.first
2.second
2
enter data : 45
enter choice : 2

in which linked list you want to insert ?


1.first
2.second
1
enter data : 56
enter choice : 2

in which linked list you want to insert ?


1.first
2.second
2
enter data : 67
enter choice : 2

in which linked list you want to insert ?


1.first
2.second
2
enter data : 56
enter choice : 3

in which linked list you want to insert ?


1.first
2.second
1
enter position of insertion : 1
enter data : 44
23->44->11->56->NULL
enter choice : 3

in which linked list you want to insert ?


1.first
2.second
2
enter position of insertion : 1
enter data : 45
45->45->23->67->56->NULL
enter choice : 4

5 of 6 23/11/23, 11:30
Untitled3 file:///home/pvg/Downloads/Untitled3(1).html

in which linked list ?


1.first
2.second
1
enter position of updation : 0
enter new data : 34
34->44->11->56->NULL
enter choice : 4

in which linked list ?


1.first
2.second
2
enter position of updation : 0
enter new data : 29
29->45->23->67->56->NULL
enter choice : 5

in which linked list ?


1.first
2.second
1
enter position of node which you want to delete : 1
34->11->56->NULL
enter choice : 5

in which linked list ?


1.first
2.second
2
enter position of node which you want to delete : 1
29->23->67->56->NULL
enter choice : 6

in which linked list ?


1.first
2.second
1
34->11->56->NULL
enter choice : 6

in which linked list ?


1.first
2.second
2
29->23->67->56->NULL
enter choice : 7
34->11->56->29->23->67->56->NULL
enter choice : 0
pls select correct option (you had one job)!thank you for using this program

6 of 6 23/11/23, 11:30

You might also like