Friday, 24 November 2017

C++ Program to Implement Xor Linked List


Code:

#include   iostream
#include   cstdlib
using namespace std;
/* 
 * Node Declaration
 */
struct node
{
    int data;
    struct node* npx;
}*head;

/*
 * Class Declaration
 */
class xor_list
{
    public:
        node* XOR (struct node *a, struct node *b);
        void insert(struct node **head_ref, int data);
        void display (struct node *head);
        xor_list()
        {
            head = NULL;
        }
};

/*
 * Main Contains Menu
 */
int main()
{
    xor_list xl;
    int choice, item;
    while (1)
    {
        cout<<"\n-------------"<
        cout<<"Operations on XOR Linked List"<
        cout<<"\n-------------"<
        cout<<"1.Insert Element at First"<
        cout<<"2.Display List"<
        cout<<"3.Quit"<
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be inserted: ";
            cin>>item;
            xl.insert(&head, item);
            break;
        case 2:
            xl.display (head);
            break;
        case 3:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}

/* 
 * Returns XORed value of the node addressed
 */
node *xor_list::XOR (struct node *a, struct node *b)
{
    return (node*) ((unsigned int) (a) ^ (unsigned int) (b));
}

/* 
 * Insert Node at Beginning
 */
void xor_list::insert(struct node **head_ref, int data)
{
    node *new_node  = new (struct node);
    new_node->data = data;
    new_node->npx = XOR (*head_ref, NULL);
    if (*head_ref != NULL)
    {
        node* next = XOR ((*head_ref)->npx,  NULL);
        (*head_ref)->npx = XOR (new_node, next);
    }
    *head_ref = new_node;
}

// Display List
void xor_list::display (struct node *head)
{
    node *curr = head;
    node *prev = NULL;
    node *next;
    cout<<"Elements of XOR Linked List: "<
    while (curr != NULL)
    {
        cout<data<<" ";
        next = XOR (prev, curr->npx);
        prev = curr;
        curr = next;
    }
    cout<
}


Output:

-------------
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


More C++ Programs:



















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...