0% found this document useful (0 votes)
18 views5 pages

Computerassignment C++

The document contains C++ code for a linked list implementation with various functions including insertion, rearranging nodes by moving half of the list, and separating even and odd nodes. It defines a 'node' class and includes functions to manipulate the linked list, print its contents, and calculate its length. The main function demonstrates the usage of these functions by inserting values, rearranging the list, and printing the results.

Uploaded by

Amrit
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)
18 views5 pages

Computerassignment C++

The document contains C++ code for a linked list implementation with various functions including insertion, rearranging nodes by moving half of the list, and separating even and odd nodes. It defines a 'node' class and includes functions to manipulate the linked list, print its contents, and calculate its length. The main function demonstrates the usage of these functions by inserting values, rearranging the list, and printing the results.

Uploaded by

Amrit
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/ 5

#include<iostream>

using namespace std;


class node
{ AMRIT RAJ 24659
public:
int data; COMPUTER ASSIGNMENT
node* next;
node(int data)
{
this->next=NULL;
this->data=data;

}
};
void insertat(node* &head,node* &tail,int value)
{if (head==NULL )
{
node* node1=new node(value);
head=node1;
tail=node1;
return;
}
if(value>tail->data)
{
node* node2=new node(value);
tail->next=node2;
tail=node2;
return;
}
node* temp=head;
node* temp1=NULL;
while(temp!=NULL)
{
if(value<temp->data)
{
if(temp1==NULL)
{
node* node3=new node(value);
node3->next=temp;
head=node3;
return;}
else{
node* node3=new node(value);
temp1->next=node3;
node3->next=temp;
return;
}
}temp1=temp;
temp=temp->next;

}}
void movehalf(node* &head){
node* temp=head;
node* tail=head;
int counter=0;
while(temp!=nullptr){
temp=temp->next;
counter++;
}
while(tail->next!=nullptr){
tail=tail->next;
}

cout<<"number of nodes: "<<counter<<endl;


node* temp1=head;
int mid=counter/2;
int count=1;
while(count != mid){
temp1=temp1->next;
count++;
}
tail->next=head;
node* n=temp1->next;
head=n;
temp1->next=NULL;
tail=temp1;
}
void movefront(node* &head) {
if (head == nullptr || head->next == nullptr) {
// If the list is empty or has only one node, no rearrangement
needed
return;
}

node* evenHead = nullptr;


node* evenTail = nullptr;
node* oddHead = nullptr;
node* oddTail = nullptr;

node* curr = head;

while (curr != nullptr) {


if (curr->data % 2 == 0) {
// Even node
if (evenHead == nullptr) {
evenHead = curr;
evenTail = curr;
} else {
evenTail->next = curr;
evenTail = curr;
}
} else {
// Odd node
if (oddHead == nullptr) {
oddHead = curr;
oddTail = curr;
} else {
oddTail->next = curr;
oddTail = curr;
}
}
curr = curr->next;
}

if (evenHead != nullptr) {

evenTail->next = oddHead;
head = evenHead;
if (oddTail != nullptr) {

oddTail->next = nullptr;
}
} else {

head = oddHead;
}
}

void print(node* &head)

{
node* temp=head;
while(temp!=0)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int len(node* &head)

{int c=0;
node* temp=head;
while(temp!=0)
{
c=c+1;
temp=temp->next;
}
return c;
}
int main()
{
node* head=NULL;
node* tail=NULL;
insertat(head,tail,4);
insertat(head,tail,7);
insertat(head,tail,45);
insertat(head,tail,22);
insertat(head,tail,5);

print(head);
cout<<endl;
movehalf(head);
print(head);
cout<<endl;
movefront(head);/*this is moving the elements of the lastest link
list as after executing movehalf() funtion original link list elemnt
have been changed*/
print(head);

return 0;
}
SREENSHOT OF THE OUTPUT

You might also like