-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalternateNodesRecur.cpp
47 lines (47 loc) · 1013 Bytes
/
alternateNodesRecur.cpp
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
#include<iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
Node* newNode(int data){
Node *temp=new Node();
temp->data=data;
temp->next=NULL;
return temp;
}
Node* insertBegin(Node *head,int data){
if(head==NULL){
return newNode(data);
}else{
head->next=insertBegin(head->next,data);
}
return head;
}
void AlternateNode(Node *head,bool cou=true){
if(head==NULL){
return;
}
if(cou==true)
cout<<head->data<<" ";
AlternateNode(head->next,!cou);
}
void Traversal(Node *head){
if(head==NULL){
return;
}
cout<<head->data<<" ";
Traversal(head->next);
}
int main(){
Node *head=NULL;
head=insertBegin(head,2);
head=insertBegin(head,3);
head=insertBegin(head,4);
head=insertBegin(head,5);
head=insertBegin(head,6);
head=insertBegin(head,7);
Traversal(head);
cout<<"\nAlternate Node\n";
AlternateNode(head);
}