Move all occurrences of an element to end in a linked list

Suppose we are inserted the following (7,3,2,7,4,1,7) node in a sequence.

Here given code implementation process.

``````//C Program
//Move given occurrence to the end of linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
int data;
struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
//insert Node element
void insert(struct Node**head,int value){
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
node->data=value;
node->next=NULL;
}else{
//find last node
while(temp->next!=NULL){
temp=temp->next;
}
//add node at last possition
temp->next=node;
}
}
}
//Display element of Node
void display(struct Node*temp){

if(temp==NULL){
}
while(temp!=NULL){
printf("%d  ",temp->data);
temp=temp->next;
}
}
//Move all occurrence of given element into the end of linked list position
void move_nodes(struct Node**head,int element){

}else{
while(temp!=NULL){
current=temp;
temp=temp->next;

if(current->data==element){
//find move element
//Move the front node element to the end position
}
if(prev!=NULL){
prev->next=temp;
}
current->next=nodes;
nodes=current;

}else{
prev=current;
}

}

//all same nodes are exist in linked list
}
else if(prev!=NULL && nodes !=NULL){
//add occurrence to end
prev->next=nodes;

}else{
//when given occurrences element are not found
printf("occurrences element are not found : %d\n",element );
}

}

}
int main(){
//create node pointer

int occurrence=7;
//insert element of linked list

printf("Before move all occurrences %d to end\n",occurrence);
//display all node

printf("\nAfter move all occurrences %d to end\n",occurrence);
return 0;
}``````

Output

``````Before move all occurrences 7 to end
7  1  2  7  3  2  7
After move all occurrences 7 to end
1  2  3  2  7  7  7 ``````
``````//C++ Program
//Move given occurrence to the end of linked list
#include<iostream>
using namespace std;

//create structure
struct Node{
int data;
struct Node*next;
};
public:
void insert(int);
void display();
void move_nodes(int);
};
}
//insert node at end of linked list
//Create dynamic node
struct Node*node=new Node;
if(node==NULL){
cout<<"Memory overflow\n";
}else{
node->data=value;
node->next=NULL;
//base condition
}else{
while(temp->next!=NULL){
temp=temp->next;
}
//add newly node at last
temp->next=node;
}
}
}
//display all node value in linked list
}
else{
cout<<"Linked List : ";
while(temp!=NULL){
//print node value
cout<<temp->data<<" ";
temp=temp->next;
}
}

}
//Move all occurrence of given element into the end of linked list position
void LinkedList::  move_nodes(int element){

}else{
*prev=NULL,
*nodes=NULL,
*current=NULL;
while(temp!=NULL){
current=temp;
temp=temp->next;

if(current->data==element){
//find move element
//Move the front node element to the end position
}
if(prev!=NULL){
prev->next=temp;
}
current->next=nodes;
nodes=current;

}else{
prev=current;
}

}

//all same nodes are exist in linked list
}
else if(prev!=NULL && nodes !=NULL){
//add occurrence to end
prev->next=nodes;

}else{
//when given occurrences element are not found
cout<<"occurrences element are not found : "<<element<<endl;
}

}

}
int main(){

//create object
int occurrence=7;
//insert element of linked list
obj.insert(7);
obj.insert(1);
obj.insert(2);
obj.insert(7);
obj.insert(3);
obj.insert(2);
obj.insert(7);

cout<<"Before move all occurrences "<<occurrence<<"to end"<<endl;
//display all node
obj.display();
cout<<"\nAfter move all occurrences "<<occurrence<<"to end"<<endl;
obj.move_nodes(occurrence);
obj.display();
}``````

Output

``````Before move all occurrences 7to end
Linked List : 7 1 2 7 3 2 7
After move all occurrences 7to end
Linked List : 1 2 3 2 7 7 7``````
``````//Java Program
//Move given occurrence to the end of linked list
class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}

public class LinkedList {

//Class constructors
}
//insert element
public void insert(int value) {
//Create  node
Node node = new Node(value);

if (head == null) head = node;
else {
Node temp = head;
//find last node
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}

}
//Display all Linked List elements
public void display() {
if (head != null) {
System.out.print("Linked List Element :");
Node temp = head;
while (temp != null) {
System.out.print("  " + temp.data);
temp = temp.next;
}
} else {
}
}
//Move all occurrence of given element into the end of linked list position
public void moveNodes(int element) {

if (head == null) {
} else {
Node temp = head,
prev = null,
nodes = null,
current = null;
while (temp != null) {
current = temp;
temp = temp.next;

if (current.data == element) {
//find move element
if (head == current) {
//Move the front node element to the end position
}
if (prev != null) {
prev.next = temp;
}
current.next = nodes;
nodes = current;

} else {
prev = current;
}

}

if (head == null && nodes != null) {
//all same nodes are exist in linked list
} else if (prev != null && nodes != null) {
//add occurrence to end
prev.next = nodes;

} else {
//when given occurrences element are not found
System.out.println("occurrences element are not found : " + element);
}

}

}
public static void main(String[] args) {

int occurrence = 7;
//insert element of linked list
obj.insert(7);
obj.insert(1);
obj.insert(2);
obj.insert(7);
obj.insert(3);
obj.insert(2);
obj.insert(7);

System.out.println("Before move all occurrences " + occurrence + "  to end");
//display all node
obj.display();

System.out.println("\nAfter move all occurrences " + occurrence + "  to end");
obj.moveNodes(occurrence);
obj.display();
}
}``````

Output

``````Before move all occurrences 7  to end
Linked List Element :  7  1  2  7  3  2  7
After move all occurrences 7  to end
Linked List Element :  1  2  3  2  7  7  7``````
``````#Python Program
#Move given occurrence to the end of linked list
class Node:
def __init__(self,data):
self.data=data
self.next=None

def __init__(self):
#Assign default value

#insert new node to linked list
def insert(self,data):
node=Node(data)
node.next=None
else:
while temp.next!=None:
temp=temp.next
temp.next=node

def display(self):
return

print("Linked List Elements : ")
while(temp!=None):
print(temp.data,end=" ")
temp=temp.next

#Move all occurrence of given element into the end of linked list position
def moveNodes(self, element):

else:
prev=None
nodes=None
current=None
while(temp!=None):
current=temp
temp=temp.next

if(current.data==element):
#find move element
#Move the front node element to the end position

if(prev!=None):
prev.next=temp

current.next=nodes
nodes=current

else:
prev=current

#all same nodes are exist in linked list

elif(prev!=None and  nodes !=None):
#add occurrence to end
prev.next=nodes

else:
#when given occurrences element are not found
print("occurrences element are not found : ",element )

def main():
#Create Object of class Linked
occurrence=7
#insert element of linked list
obj.insert(7)
obj.insert(1)
obj.insert(2)
obj.insert(7)
obj.insert(3)
obj.insert(2)
obj.insert(7)

print("Before move all occurrences ",occurrence,"  to end")
#display all node
obj.display()

print("\nAfter move all occurrences ",occurrence,"  to end")
obj.moveNodes(occurrence)
obj.display()
if __name__=="__main__":
main()``````

Output

``````Before move all occurrences  7   to end
Linked List Elements :
7 1 2 7 3 2 7
After move all occurrences  7   to end
Linked List Elements :
1 2 3 2 7 ``````
``````//C# Program
//Move given occurrence to the end of linked list
using System;
//node class
public class Node{
public  int data;
public  Node next;
}
class Program
{
public Program(){
}
//insert node of linked list
public void insert(int data){
Node newNode=new Node();
newNode.data=data;
newNode.next=null;
else{
//get last node
while(temp.next!=null){
temp=temp.next;
}
temp.next=newNode;
}
}
//display linked list nodes value
public void display(){
Console.Write("Empty List");
}else{
while(temp!=null){
Console.Write("  {0}",temp.data);
temp=temp.next;
}

}
}
//Move all occurrence of given element into the end of linked list position
public void moveNodes(int element){

}else{
prev=null,
nodes=null,
current=null;
while(temp!=null){
current=temp;
temp=temp.next;

if(current.data==element){
//find move element
//Move the front node element to the end position
}
if(prev!=null){
prev.next=temp;
}
current.next=nodes;
nodes=current;

}else{
prev=current;
}

}

if( head==null && nodes!=null){
//all same nodes are exist in linked list
}
else if(prev!=null && nodes !=null){
//add occurrence to end
prev.next=nodes;

}else{
//when given occurrences element are not found
Console.WriteLine("occurrences element are not found :{0} ",element );
}

}

}
static void Main()
{
Program obj=new Program();
int occurrence=7;
//insert element of linked list
obj.insert(7);
obj.insert(1);
obj.insert(2);
obj.insert(7);
obj.insert(3);
obj.insert(2);
obj.insert(7);

Console.WriteLine("Before move all occurrences {0} to end",occurrence);
//display all node
obj.display();

Console.WriteLine("\nAfter move all occurrences {0} to end",occurrence);
obj.moveNodes(occurrence);
obj.display();
}
}
``````

Output

``````Before move all occurrences 7 to end
7  1  2  7  3  2  7
After move all occurrences 7 to end
1  2  3  2  7  7  7
``````
``````<?php
//Php program
//Move given occurrence to the end of linked list
class Node
{
public \$data;
public \$next;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}

function __construct()
{
}
/*
* Append the Given data value at end of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
\$newNode=new Node(\$data);
{
}else{
//find last node of linked list
while(\$temp->next!=NULL){
\$temp=\$temp->next;
}
//add new node to last of linked list
\$temp->next=\$newNode;
}
}
//Display all inserted node in linked list
function display()
{
{
echo "Empty Linked List";
}
else{
echo "<br>Linked List :";
while(\$temp!=NULL)
{
//display node value
echo "  ".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}
//Move all occurrence of given element into the end of linked list position
function moveNodes(\$element)
{

{
echo "<br>Empty linked list";
}
else
{
\$prev=NULL;
\$nodes=NULL;
\$current=NULL;
while(\$temp!=NULL)
{
\$current=\$temp;
\$temp=\$temp->next;

if(\$current->data==\$element)
{
//find move element
{
//Move the front node element to the end position
}
if(\$prev!=NULL){
\$prev->next=\$temp;
}
\$current->next=\$nodes;
\$nodes=\$current;

}else{
\$prev=\$current;
}

}

//all same nodes are exist in linked list
}
else if(\$prev!=NULL && \$nodes !=NULL){
//add occurrence to end
\$prev->next=\$nodes;

}else{
//when given occurrences element are not found
echo "occurrences element are not found : ".\$element ;
}

}

}
}
function main(){
//Make a object of LinkedList class
\$occurrence=7;
/*
*Insert following nodes in linked list
*/

\$obj->insert(7);
\$obj->insert(1);
\$obj->insert(2);
\$obj->insert(7);
\$obj->insert(3);
\$obj->insert(2);
\$obj->insert(7);

echo "Before move all occurrences ".\$occurrence."  to end";
//display all node
\$obj->display();

echo "<br>Before move all occurrences ".\$occurrence."  to end";
\$obj->moveNodes(\$occurrence);
\$obj->display();
}
main();
?>
``````

Output

``````Before move all occurrences 7 to end
Linked List : 7 1 2 7 3 2 7
Before move all occurrences 7 to end
Linked List : 1 2 3 2 7 7 7``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.