Monday, 18 February 2013

Some operations on doubly linked list...
1.Reversal

void reverse(struct node **ps)
{
struct node *temp,*temp2;
temp=*ps;
if(temp->next==NULL)
return;
while(temp!=NULL)
{
temp2=temp;
temp=temp->next;
temp2->next=temp2->prev;
temp2->prev=temp;
}
*ps=temp2;
}

2.Leave k nodes and then delete k nodes repeatedly..

void del(struct node **ps,int k)
{
struct node *temp,*temp1,*temp2;
int i;
temp=*ps;
while(temp!=NULL)
{
i=0;
while(i!=k)
{
temp=temp->next;
i++;
if(temp==NULL)
break;
}
if(i==k)
{
for(i=0;i<k;i++)
{
if(temp==NULL)
break;
temp1=temp;
temp=temp->next;
if(temp1->next!=NULL)
(temp1->next)->prev=temp1->prev;
(temp1->prev)->next=temp1->next;
free(temp1);
}
}
}
}

Now its time to move on to doubly linked list.It has two pointers in every node-one points to the next node and the other points to the previous node.
Simple code to insert and display elements in a doubly linked list-

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *prev;
struct node *next;
};

void append(struct node **,int);
void display(struct node *);
int main()
{
struct node *start=NULL;
append(&start,10);
append(&start,25);
append(&start,5);
append(&start,16);
append(&start,22);
append(&start,24);
append(&start,8);
append(&start,31);
append(&start,12);
printf("Original list\n");
display(start);
return 0;
}

void append(struct node **ps,int x)
{
struct node *temp,*temp2;
temp2=(struct node *)malloc(sizeof(struct node));
temp2->data=x;
temp2->next=NULL;
if(*ps==NULL)
{
*ps=temp2;
temp2->prev=NULL;
return;
}
temp=*ps;
while(temp->next!=NULL)
temp=temp->next;
temp->next=temp2;
temp2->prev=temp;
}

void display(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
Some other operations over circular linked list...:)
1.Reversal of circular linked list

void reverse(struct node **ps)
{
struct node *temp,*temp1,*temp2;
temp1=*ps;
temp=*ps;
while(temp1->next!=*ps)
temp1=temp1->next;
do
{
temp2=temp1;
temp1=temp;
temp=temp->next;
temp1->next=temp2;
}while(temp!=*ps);
*ps=temp1;
}

2.Appending elements in sorted order

void append(struct node **ps,int x)
{
struct node *temp,*temp1,*prev;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=x;
if(*ps==NULL)
{
*ps=temp1;
(*ps)->next=*ps;
return;
}
temp=*ps;
if((*ps)->data>x)
{
temp1->next=*ps;
while(temp->next!=*ps)
temp=temp->next;
temp->next=temp1;
*ps=temp1;
return;
}
while(temp->next!=*ps && temp->data<x)
{
prev=temp;
temp=temp->next;
}
if(temp->data<x)
{
temp->next=temp1;
temp1->next=*ps;
}
else
{
prev->next=temp1;
temp1->next=temp;
}
}

3.Leaving the k locations and deleting next k locations repeatedly.
Note-Here only one node will remain at last whose data value will be returned.

int del(struct node **ps, int k)
{
struct node *temp,*temp1,*prev;
int i,j;
temp=*ps;
prev=temp;
while((*ps)->next!=*ps)
{
for(i=0;i<k;i++)
{
prev=temp;
temp=temp->next;
}
for(j=0;j<k;j++)
{
if((*ps)->next==*ps)
break;
temp1=temp;
temp=temp->next;
prev->next=temp1->next;
if(temp1==*ps)
*ps=temp1->next;
free(temp1);
}
if(j!=k)
break;
}
return ((*ps)->data);
}

4.Josephus problem where we have to delete every xth node

int josephus(struct node **ps,int x)
{
struct node *temp,*temp1,*prev;
temp=*ps;
int i;
while((*ps)->next!=*ps)
{
i=0;
while(i!=x)
{
prev=temp;
temp=temp->next;
i++;
}
temp1=temp;
prev->next=temp->next;
temp=temp->next;
if(temp1==*ps)
*ps=temp1->next;
free(temp1);
}
return (*ps)->data;
}



After working on singly linked list lets move on to circular linked list.It is very much similar to linear linked list except that here the last node contains the address of the start node again instead of NULL value.
So,lets code...:)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void append(struct node **,int);
void display(struct node *);
int main()
{
struct node *start=NULL;
append(&start,10);
append(&start,25);
append(&start,5);
append(&start,16);
append(&start,22);
append(&start,24);
append(&start,8);
append(&start,31);
append(&start,12);
printf("Original list\n");
display(start);
return 0;
}

void append(struct node **ps,int x)
{
struct node *temp,*temp1;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=x;
if(*ps==NULL)
{
*ps=temp1;
(*ps)->next=*ps;
return;
}
temp=*ps;
while(temp->next!=*ps)
temp=temp->next;
temp->next=temp1;
temp1->next=*ps;
}

void display(struct node *ps)
{
struct node *temp;
temp=ps;
while(temp->next!=ps)
{
printf("%d\n",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
The code simply is to add some nodes and display them in a circular linked list.

Saturday, 16 February 2013

Some other operations on linked list...Enjoy.;)
1.To display the linked list from back..
void display_back(struct node *p)
{
struct node *temp,*temp1;
if(p->next==NULL)
{
printf("%d\n",p->data);
return;
}
temp=p;
while(temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
printf("%d\n",temp->data);
display_back(p);
}

2.To compare two given linked list..

int cmp(struct node *p,struct node *q)
{
while(p!=NULL || q!=NULL)
{
if(p->data!=q->data)
return 0;
else
{
p=p->next;
q=q->next;
}
}
if(p!=NULL || q!=NULL)
return 0;
else
return 1;
}

3.To swap the Kth node from the start with the Kth node from the last...

void swap(struct node *ps,int k)
{
struct node *p,*q,*r;
p=ps;
q=ps;
r=ps;
int i=1;
while(i<k && p!=NULL)
{
p=p->next;
q=q->next;
i++;
}
if(i!=k || p==NULL)
{
printf("less no of nodes\n");
return;
}
while(q->next!=NULL)
{
q=q->next;
r=r->next;
}
int temp;
temp=p->data;
p->data=r->data;
r->data=temp;
}

4.To find the mid of linked list..

int find_mid(struct node *p)
{
struct node *temp,*temp1;
temp=p;
temp1=p;
int i;
for(i=1;temp->next!=NULL;i++,temp=temp->next)
{
if(i%2==0)
temp1=temp1->next;
}
return temp1->data;
}

5.To create a copy of linked list..

void cpy(struct node *p,struct node **q)
{
struct node *temp,*temp1;
while(p!=NULL)
 {
temp=(struct node *)malloc(sizeof(struct node));
temp->data=p->data;
temp->next=NULL;
if(*q==NULL)
*q=temp;
else
{
temp1=*q;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
}
p=p->next;
 }
}
Types of deletion in linked list:
1. From beginning
2. From end
3. From any given position
4. Leave 1st int locations and delete next int...
Here is the code for all four...
1. void del_beg(struct node **ps)
{
struct node *temp;
temp=*ps;
*ps=temp->next;
free(temp);
}

2. void del_end(struct node **ps)
{
struct node *temp,*temp1;
if((*ps)->next==NULL)
{
*ps=NULL;
return;
}
temp=*ps;
while(temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
free(temp);
}

3. void del_pos(struct node **ps,int pos)
{
struct node *temp,*temp1;
int i=1;
temp=*ps;
if(pos==1)
{
*ps=(*ps)->next;
free(temp);
return;
}
while(i<pos && temp)
{
temp1=temp;
temp=temp->next;
i++;
}
if(temp)
{
temp1->next=temp->next;
free(temp);
}
else
printf("Less no of nodes present than the position given.\n");
}

4.void del(struct node **ps,int n,int m)
{
struct node *temp=*ps,*temp1,*temp2;
int i;
while(temp)
{
for(i=1;i<=n;i++)
{
if(temp==NULL)
break;
temp1=temp;
temp=temp->next;
}
if(i==n+1)
{
for(i=1;i<=m;i++)
{
if(temp==NULL)
break;
temp1->next=temp->next;
temp2=temp;
temp=temp->next;
free(temp2);
}
}
}

}
How can we reverse a singly linked list??Reversal here means we have to reverse the links not the values.
So here is the code:

void reverse(struct node **ps)
{
struct node *temp,*temp1,*temp2;
temp=*ps;
temp2=NULL;
while(temp!=NULL)
{
temp1=temp2;
temp2=temp;
temp=temp->next;
temp2->next=temp1;
}
*ps=temp2;
}

Here as we see the value of start will be changed.So we have used pointer to pointer.This function can be called from main as: reverse(&start);

After doing some basic append and display in linked list lets try to insert nodes in sorted order in the linked list.Here's the code for that.The code is very much similar to the 1st code except that here in append function we check  the value to be inserted should be on which position in linked list so that when we display the linked list it remains sorted.
//Lets do something more challenging...
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

void append_sort(struct node **,int);
void display(struct node *);
int main()
{
struct node *start=NULL;
append_sort(&start,15);
append_sort(&start,10);
append_sort(&start,25);
append_sort(&start,5);
append_sort(&start,16);
append_sort(&start,22);
append_sort(&start,24);
append_sort(&start,8);
append_sort(&start,31);
append_sort(&start,12);
display(start);
return 0;
}

void append_sort(struct node **ps,int x)
{
struct node *temp,*temp1,*q;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=x;
if(*ps==NULL)
{
*ps=temp1;
(*ps)->next=NULL;
return;
}
temp=*ps;
if(temp->data>x)
{
*ps=temp1;
(*ps)->next=temp;
return;
}
while(temp->next!=NULL && temp->data<x)
{
q=temp;
temp=temp->next;
}
if(temp->data<x)
{
temp1->next=NULL;
temp->next=temp1;
}
else
{
q->next=temp1;
temp1->next=temp;
}
}

void display(struct node *ps)
{
while(ps!=NULL)
{
printf("%d\n",ps->data);
ps=ps->next;
}
}

Hey guys...After thinking so much i thought why not to start with the basics of DS 1st.So,here is the 1st code about a singly linked list.The code is simply illustrating how we can add and display some nodes in a singly linked list.I hope the beginners mainly confuse in this code when they see the symbol **.That's preety common.** is nothing but pointer to a pointer.I assume that you have knowledge about pointers.
Pointers basically point to some memory location.
Eg.-int a=10;
      int *p=&a;
Here p stores the address of variable a ie some hexadecimal value or something.
*p refers to the value stored at a ie 10.
Now if we want to store the address of pointer p then we can't declare the pointer as
int *q=&p; //Wrong...
We will have to declare as
int **q=&p;
So i think  now you may be clear about **.If we wish to change the value pointed by a pointer then we will have to pass the address of that pointer as pointer to pointer argument.That is what we are doing here.
Here we want to change the value pointed by the start pointer of struct node via the append function in the 1st calling when there is no node added in the linked list.See code of append funcn below:

       if(*ps==NULL)
{
*ps=temp1;
return;
}

So,We pass the address of start pointer in the function append.In display funcn we don't  want to change anything but just traverse the linked list.So,we just pass the start pointer.

//My first linked list code...It's just a beggining...:)
#include<stdio.h>
#include<stdlib.h>

//A linked list node has two parts.Data part contains the value to be stored in linked list and The next pointer points to the next node of linked list.If there is nothing to point to the next pointer holds NULL.
struct node
{
int data;
struct node *next;
};

void append(struct node **,int);
void display(struct node *);
int main()
{
struct node *start=NULL;
append(&start,15);
append(&start,10);
append(&start,25);
append(&start,5);
append(&start,16);
append(&start,22);
append(&start,24);
append(&start,8);
append(&start,31);
append(&start,12);
display(start);
return 0;
}

void append(struct node **ps,int x)
{
struct node *temp,*temp1;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=x;
temp1->next=NULL;
if(*ps==NULL)
{
*ps=temp1;
return;
}
temp=*ps;
while(temp->next!=NULL)
temp=temp->next;
temp->next=temp1;
}

void display(struct node *ps)
{
while(ps!=NULL)
{
printf("%d\n",ps->data);
ps=ps->next;
}
}

Tuesday, 12 February 2013

Are you interested in learning new coding tips and tricks???
I am also...:)
I am just a beginner in the coding arena so don't expect complex algos and tricks from me.But i will surely try to provide informations which i find new (maybe only for me and a few others but not for many other intellectuals) and helpful in improving the coding skills.I will also keep posting about data structure codes and how to understand them easily.Feel free to comment and query on any of the posts and errors in any code or to clarify any doubt. It will help to improve me and my blog also and will be useful for other viewers also. You can also send suggestions on my email Id. It will be a matter  of great pleasure for me to respond back to you.Thanks in advance.;)