forked from kanika2107/Advanced-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor_ll.c
49 lines (48 loc) · 802 Bytes
/
xor_ll.c
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
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node* npx;
};
struct node* Xor(struct node* a,struct node* b)
{
return (struct node*)((unsigned int)(a)^(unsigned int)(b));
}
void insert(struct node** head,int val)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->npx=Xor(NULL,(*head));
if((*head)!=NULL)
{
struct node* next=Xor((*head)->npx,NULL);
(*head)->npx=Xor(temp,next);
}
(*head)=temp;
}
void print(struct node* head)
{
struct node* prev=NULL;
while(head!=NULL)
{
cout << head->data << " ";
struct node* nextptr=Xor(prev,head->npx);
prev=head;
head=nextptr;
}
}
int main()
{
struct node* head=NULL;
int n;
cin >> n;
while(n--)
{
int a;
cin >> a;
insert(&head,a);
}
print(head);
return 0;
}