-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternateSplit.cpp
More file actions
50 lines (49 loc) · 1018 Bytes
/
alternateSplit.cpp
File metadata and controls
50 lines (49 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void push(Node **head,int new_data){
Node *temp=new Node();
temp->data=new_data;
temp->next=(*head);
(*head)=temp;
}
void move(Node *a,Node *b){
if(a==NULL || b==NULL)
return;
if(a->next!=NULL)
a->next=a->next->next;
if(b->next!=NULL)
b->next=b->next->next;
move(a->next,b->next);
}
void split(Node *head,Node **a_ref,Node **b_ref){
Node *curr=head;
*a_ref=curr;
*b_ref=curr->next;
move(*a_ref,*b_ref);
}
void display(Node *head){
if(head==NULL)
return;
display(head->next);
cout<<head->data<<" ";
}
int main(){
Node *head=NULL;
Node *a=NULL,*b=NULL;
push(&head,1);
push(&head,2);
push(&head,3);
push(&head,4);
push(&head,5);
push(&head,6);
push(&head,7);
split(head,&a,&b);
cout<<"A: ";
display(a);
cout<<"\nB: ";
display(b);
}