/*The Following Piece Of Code Adds an element at specified position in
a circular linked list.
AND
Removes an element from the specified Position.
AN INTERESTING THING: NO NEED TO MAKE SEPERATE PIECE OF CODE FOR INSERTION AND DELETION
FROM THE BEGINNING AND AT THE END,AS THESE ARE SPECIAL CASE OF ADDING
AND REMOVING FROM SPECIFIED POSITION, THINK HOW!!!!!!!!
*/
#include<stdio.h>
#include<conio.h>
struct linklist
{
int data;
struct linklist *next;
};
typedef struct linklist node;
/*---------------Set Of Helping Functions----------------*/
node *create_node(int val,node *address)
{
node *p;
p=(node*)malloc(sizeof(node));
p->data=val;
p->next=address;
return p;
}
int get_length(node *head)/*Returns the number of elements in the list*/
{
node *t_head=head;
int count=0;
if(head!=NULL)
{
count++;
t_head=t_head->next;
while(t_head!=head)
{
t_head=t_head->next;
count++;
}
}
return count;
}
node *get_ith_node(node *head,int i)
{
if(i<=get_length(head))
{
while(i>1)
{
head=head->next;
i--;
}
return head;
}
else
return NULL;
}
/*---------------------------------------------------------------------------*/
/*Module for Insertion At a specified Position*/
node *inposition_c(node *head,int val,int pos)
{
int no_of_elements;
node *p,*prev;
if(head==NULL)
{
if(pos==1)
{
head=create_node(val,NULL);
head->next=head;
}
else
printf("No linked list exists\n");
}
else
{
no_of_elements=get_length(head);
if(pos<=no_of_elements+1) /*Can you think why position <= no_of_elements + 1?? Suppose there are 3 elements in the list, then a node can be added at 4th position*/
{
prev=get_ith_node(head,pos-1);/*Getting the node previous to the target node*/
if(pos==1)
{
p=get_ith_node(head,no_of_elements); /*Extracting the last node*/
head=create_node(val,head); /*Creating a new node at the beginning*/
p->next=head; /*Making the last node to point to the new head*/
}
else
prev->next=create_node(val,prev->next);
}
else
printf("Desired number of nodes do not exists\n");
}
return head;
}
/*Module for Deletion From a specified Position*/
node *del_from_pos_c(node *head,int pos)
{
node *prev,*start=head;
int no_of_elements;
if(head!=NULL)
{
no_of_elements=get_length(head);
if(pos==1)
{
if(no_of_elements==1)
head=NULL;
else
{
prev=get_ith_node(head,no_of_elements);/*Using prev as temporary variable, to point to the last node*/
head=head->next; /*Advancing the head*/
prev->next=head; /*Making the last node point to the advanced head*/
}
free(start);/*Node Tu Aazaad Hai*/
}
else
{
if(pos<=no_of_elements)
{
prev=get_ith_node(head,pos-1); /*Extracting the node previous to the target node*/
start=prev->next; /*Making the start point to the target node*/
prev->next=start->next; /*Adjusting the pointer*/
}
else
printf("Desired number of nodes do not exists\n");
free(start);/*Node Tu Aazaad Hai*/
}
}
return head;
}
/*Module for Printing the list contents*/
void print(node *head)
{
node *t_head=head;
if(head!=NULL)
{
printf("\nContents\n");
printf("%d\n",t_head->data);
t_head=t_head->next;
while(t_head!=head)
{
printf("%d\n",t_head->data);
t_head=t_head->next;
}
}
else
printf("The list is empty\n");
}
int main()
{
node *head=NULL;
//clrscr();
head=inposition_c(head,-2,1);
head=inposition_c(head,-10,3);
head=inposition_c(head,-12,1);
head=inposition_c(head,0,3);
head=inposition_c(head,19,2);
print(head);
head=del_from_pos_c(head,3);
print(head);
head=del_from_pos_c(head,1);
print(head);
head=del_from_pos_c(head,2);
print(head);
head=del_from_pos_c(head,1);
print(head);
getch();
}
0 comments:
Post a Comment