Skip to main content

C Program to insert/delete/traverse/display XOR Linked List


Here is the C Program to insert/delete/traverse/display XOR Linked List.XOR linked list is said to be memory efficient version of doubly linked list.

_________________________________________________________________________________

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *ptr;
};
struct Node* XOR(struct Node *a,struct Node *b){
return (struct node*) ((unsigned int) (a) ^ (unsigned int) (b));
}
void insertIntoXORLL(struct Node **head,int pos,int data){
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
if(*head==NULL){ //Insert first Node
newNode->data=data;
newNode->ptr=NULL;
*head=newNode;
}else{
struct Node *current=*head;
struct Node *previous=NULL,*next=NULL;
struct Node *nextNode=NULL;
if(pos<=1){
newNode->ptr=XOR(previous,*head);
nextNode=XOR((*head)->ptr,previous);
(*head)->ptr=XOR(newNode,nextNode);
*head=newNode;
}else if(pos>1){
int count=1;
while(current!=NULL && count<(pos-1)){
next=XOR(current->ptr,previous);
previous=current;
current=next;count++;
}
if(current!=NULL){
next=XOR(current->ptr,previous);
}else{next=NULL;}
if(next!=NULL){
nextNode=XOR(next->ptr,current);
next->ptr=XOR(newNode,nextNode);
}
newNode->ptr=XOR(current,next);
current->ptr=XOR(previous,newNode);
}
}
}
void deleteElement(struct Node **head,int data){
struct Node *current=*head;
struct Node *previous=NULL,*next=NULL;
struct Node *previousNode,*nextNode;
while(current!=NULL && current->data!=data){
next=XOR(current->ptr,previous);
previous=current;
current=next;
}
if(current==NULL){
printf("There is no element with data=%d",data);
}else{
if(current==(*head) && current->ptr==NULL){
free(current);
}else if(current==(*head)){
next=XOR(current->ptr,previous);
next->ptr=XOR(current,next->ptr);
(*head)=next;
free(current);
}else if(current->ptr==NULL){
previous->ptr=NULL;
free(current);
}else{
next=XOR(current->ptr,previous);
previousNode=XOR(previous->ptr,current);
previous->ptr=XOR(previousNode,next);
nextNode=XOR(next->ptr,current);
next->ptr=XOR(previous,nextNode);
free(current);
}
}
}
void displayList(struct Node **head){
struct Node *current=*head;
struct Node *next=*head;
struct Node *previous=NULL;
printf("Linked list:\n");
while(current!=NULL){
printf("%d\n",current->data);
next=XOR(current->ptr,previous);
previous=current;
current=next;
}
}
void main(){
int choice,pos=0,data;
struct Node *head=NULL;
while(1){
printf("Enter Your Choice:\n");
printf("1.Insert Node at the certain position\n");
printf("2.Delete Node at the certain position\n");
printf("3.Display list\n");
printf("4.Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter Position(starts from 1) where you want to insert:");
scanf("%d",&pos);
printf("Enter Data Value you want to insert:");
scanf("%d",&data);
insertIntoXORLL(&head,pos,data);
break;
case 2:
printf("Enter Element You want to delete:");
scanf("%d",&data);
deleteElement(&head,data);
break;
case 3:
displayList(&head);
break;
case 4:
break;
}
}

}
_________________________________________________________________________________

Comments

Popular posts from this blog

MonoLithic Vs Microservice Architecture | which Architecture should i choose ?

From last few years ,microservices are an accelerating trend . Indeed, microservices approach offers tangible benefits including an increase in scalability, flexibility, agility, and other significant advantages. Netflix, Google, Amazon, and other tech leaders have successfully switched from monolithic architecture to microservices. Meanwhile, many companies consider following this example as the most efficient way for business growth. On the contrary, the monolithic approach is a default model for creating a software application. Still, its trend is going down because building a monolithic application poses a number of challenges associated with handling a huge code base, adopting a new technology, scaling, deployment, implementing new changes and others. So is the monolithic approach outdated and should be left in the past? And is it worth shifting the whole application from a monolith to microservices ? Will developing a microservices application help you reach you...

C Program to sort an array using insertion sort

I have seen couple of students who use swap in insertion sort to sort an array.Insertion sort is an example of exchange sort ,we don't swap two elements to sort an array ,instead of this we shift elements if ith element is greater than i+1th element to sort an array.It works on simple concept:- Store the ith element in temporary variable and shift element until you find the element smaller than this element towards 0 index position.Here is the C Program to find insertion sort:- _________________________________________________________________________________ #include <stdio.h> #include <string.h> main() {     int arrSize,i;    printf("Enter Size of array:");    scanf("%d",&arrSize);    int array[arrSize];    for(i=0;i<arrSize;i++){        scanf("%d",&array[i]);    }    int temp,j;    for(i=1;i<arrSize;i++){        temp...

Long-Polling vs WebSockets vs Server-Sent Events

Long-Polling vs WebSockets vs Server-Sent Events  Long-Polling, WebSockets, and Server-Sent Events are popular communication protocols between a client like a web browser and a web server. First, let’s start with understanding what a standard HTTP web request looks like. Following are a sequence of events for regular HTTP request: Client opens a connection and requests data from the server. The server calculates the response. The server sends the response back to the client on the opened request. HTTP Protocol Ajax Polling Polling is a standard technique used by the vast majority of AJAX applications. The basic idea is that the client repeatedly polls (or requests) a server for data. The client makes a request and waits for the server to respond with data. If no data is available, an empty response is returned. Client opens a connection and requests data from the server using regular HTTP. The requested webpage sends requests to...