#include
using namespace std;
class node{
public:
int data;
node*next;
//constructor of the node; name of the constructor should be the same of class name
node(int d){
data=d;
next=NULL;
}
};
// class linkedList{
// nodehead;
// nodetail;
// public:
// linkedList(){
// head=NULL;
// tail=NULL;
// }
// //here we can do watever we want by making function;
// void insert
// };
//pasing a pointer variable by reference
void insertAtHead(node*&head,int data){
node*n=new node(data); //forms the new node
n->next=head; //now new node points to the head(points to the NULL)
head=n; //now the head node points the n
}
int length(node*head){
int len=0;
while(head!=NULL){
head=head->next; //moves to the next node
len++;
}
return len;
}
void insertAtTail(node*&head,int data){
if(head==NULL){
head=new node(data);
return;
}
node*tail=head;//it means that let node*tail starts from head
while(tail->next!=NULL){
tail=tail->next; //last node of the linked list
}
tail->next=new node(data);//insert the node at the last node
return ;
}
void insertInMiddle(node*&head,int data,int p){
if(head==NULL || p==0){ //if there is no node present
insertAtHead(head ,data); //then it will be first node
}
else if(p>length(head)){ //if the length of linkedd list is greater then the positio to be inserted
insertAtTail(head,data);
}
else{
//insert in middle of linked list
int jump=1; //as minimum jump is 1
node*temp=head; //temp starts from the node
while(jump<=p-1){ //as if u want to insert at the 3 position u need 2 jumps
temp=temp->next;
jump++;
}
node*n=new node(data); //forms new node
n->next=temp->next; //new node points the node of (temp node)
temp->next=n; //temp node points to the new node(represented by n)
}
}
void reverse(node*&head){
nodeC=head;
nodeP=NULL;
node*N;
while(C!=NULL){
//save the next node
N=C->next;
//make the current node point to prev
C->next=P;
//now current becomes previous
P=C;
//now current becomes N
C=N;
}
head=P;
}
void buildList(node*&head){
int data;
cin>>data;
while(data!=-1){
insertAtTail(head,data);
cin>>data;
}
}
void print(nodehead){
// nodetemp=head;
while(head!=NULL){
cout<data<<"–>";
head=head->next;
}
cout<<“null”<<endl;
}
istream& operator>>(istream &is,node*&head){
buildList(head);
return is;
}
ostream& operator<<(ostream &os,node*head){
print(head);
return os;
}
int main(){
nodehead=NULL;
nodehead2=NULL;
cin>>head;
reverse(head);
cout<<head<<endl;
reverse(head);
cin>>head2;
cout<<head2<<endl;
return 0;
}
why this is reversing only the first linked list
not the second one