logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement Xor Linked List

Posted on August 28, 2013 by Manish
This C++ Program demonstrates operations on Xor Linked List.

Here is source code of the C++ Program to demonstrate XOR Linked List. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program To Implement XOR Linked List 
  3.  */
  4. #include <iostream>
  5. #include <cstdlib>
  6. using namespace std;
  7. /* 
  8.  * Node Declaration
  9.  */
  10. struct node
  11. {
  12.     int data;
  13.     struct node* npx;
  14. }*head;
  15.  
  16. /*
  17.  * Class Declaration
  18.  */
  19. class xor_list
  20. {
  21.     public:
  22.         node* XOR (struct node *a, struct node *b);
  23.         void insert(struct node **head_ref, int data);
  24.         void display (struct node *head);
  25.         xor_list()
  26.         {
  27.             head = NULL;	
  28.         }
  29. };
  30.  
  31. /*
  32.  * Main Contains Menu
  33.  */
  34. int main()
  35. {
  36.     xor_list xl;
  37.     int choice, item;
  38.     while (1)
  39.     {
  40.         cout<<"\n-------------"<<endl;
  41.         cout<<"Operations on XOR Linked List"<<endl;
  42.         cout<<"\n-------------"<<endl;
  43.         cout<<"1.Insert Element at First"<<endl;
  44.         cout<<"2.Display List"<<endl;
  45.         cout<<"3.Quit"<<endl;
  46.         cout<<"Enter your Choice: ";
  47.         cin>>choice;
  48.         switch(choice)
  49.         {
  50.         case 1:
  51.             cout<<"Enter value to be inserted: ";
  52.             cin>>item;
  53.             xl.insert(&head, item);
  54.             break;
  55.         case 2:
  56.             xl.display (head);
  57.             break;
  58.         case 3:
  59.             exit(1);
  60.             break;
  61.         default:
  62.             cout<<"Wrong Choice"<<endl;
  63.         }
  64.     }
  65.     return 0;
  66. }
  67.  
  68. /* 
  69.  * Returns XORed value of the node addressed
  70.  */
  71. node *xor_list::XOR (struct node *a, struct node *b)
  72. {
  73.     return (node*) ((unsigned int) (a) ^ (unsigned int) (b));
  74. }
  75.  
  76. /* 
  77.  * Insert Node at Beginning
  78.  */
  79. void xor_list::insert(struct node **head_ref, int data)
  80. {
  81.     node *new_node  = new (struct node);
  82.     new_node->data = data;
  83.     new_node->npx = XOR (*head_ref, NULL);
  84.     if (*head_ref != NULL)
  85.     {
  86.         node* next = XOR ((*head_ref)->npx,  NULL);
  87.         (*head_ref)->npx = XOR (new_node, next);
  88.     }
  89.     *head_ref = new_node;
  90. }
  91.  
  92. // Display List
  93. void xor_list::display (struct node *head)
  94. {
  95.     node *curr = head;
  96.     node *prev = NULL;
  97.     node *next;
  98.     cout<<"Elements of XOR Linked List: "<<endl;
  99.     while (curr != NULL)
  100.     {
  101.         cout<<curr->data<<" ";
  102.         next = XOR (prev, curr->npx);
  103.         prev = curr;
  104.         curr = next;
  105.     }
  106.     cout<<endl;
  107. }

$ g++ xor_list.cpp
$ a.out
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 1
Enter value to be inserted: 100
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 2
Elements of XOR Linked List: 
100 
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 1
Enter value to be inserted: 200
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 2
Elements of XOR Linked List: 
200 100 
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 1
Enter value to be inserted: 300
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 2
Elements of XOR Linked List: 
300 200 100 
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 1
Enter value to be inserted: 400
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 2
Elements of XOR Linked List: 
400 300 200 100 
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 1
Enter value to be inserted: 500
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 2
Elements of XOR Linked List: 
500 400 300 200 100 
 
-------------
Operations on XOR Linked List
 
-------------
1.Insert Element at First
2.Display List
3.Quit
Enter your Choice: 3
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program to Check for balanced paranthesis by using Stacks
» Next Page - C++ Program to Implement Hash Tables Chaining with List Heads
« C# Program to Illustrate Bitwise Operations
C++ Program to Implement Hash Tables Chaining with List Heads »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Stacks & Queues
  2. Java Programming Examples on Data-Structures
  3. C Programming Examples
  4. C++ Programming Examples on Data-Structures
  5. C Programming Examples on Data-Structures
  6. C Programming Examples on Trees
  7. C Programming Examples using Recursion
  8. C# Programming Examples on Data Structures
  9. C Programming Examples without using Recursion
  10. C Programming Examples on Linked List
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry