Thursday, September 15, 2016

C code to remove duplicate from a linked list

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

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node));
temp1=start;
if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->data=lData;
temp->next=NULL;

}
}
void display()
{
struct node *temp1=start;
if(temp1 == NULL )
printf("Linked list is empty\n");
else
{
while(temp1->next != NULL )
{
printf("Final Element is [%d]\n",temp1->data);
temp1=temp1->next;
}
printf("Final Element is [%d]\n",temp1->data);
}
}
void remove_dup()
{
struct node *temp=start;
struct node *temp1=start->next;
struct node *temp2=start->next;
struct node *prev=NULL;
while(temp !=NULL)
{
prev=temp;
while(temp1 != NULL)
{
if(temp->data == temp1->data)
{
printf("going to remove [%d] [%d]\n",temp->data,prev->data);
prev->next=temp1->next;
temp2=temp1;
temp1=temp1->next;
free(temp2);
}
else
{
prev=temp1;
temp1=temp1->next;
}
}
temp=temp->next;
if(temp != NULL)
{
temp1=temp->next;
}
}
free(temp1);
}
int main()
{
int choice;
while(1)
{
printf(" 1. For Insert into link list\n 2. remove duplicates \n 3. Display link list\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
remove_dup();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Code to search an element in a link list

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node *));
temp1=start;

if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->data=lData;
temp->next=NULL;
}
}
void display()
{
struct node *temp1=start;
if(temp1 == NULL )
printf("Linked list is empty\n");
else
{
while(temp1->next != NULL )
{
printf("Final Element is [%d]\n",temp1->data);
temp1=temp1->next;
}
printf("Final Element is [%d]\n",temp1->data);
}
}
void search(int data)
{
struct node *temp=start;
while(temp->next !=NULL )
{
if(temp->data == data )
{
printf("Element Found [%d]\n",temp->data);
break;
}
else
temp=temp->next;
}
if(temp->next == NULL )
{
printf("Element [%d] not found \n",data);
}
}

int main()
{
int choice;
int data;
while(1)
{
printf(" 1. For Insert into link list\n 2. Search Element\n 3. Display link list\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
printf("Enter element to search:\t");
scanf("%d",&data);
search(data);
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Free nodes of a linked list in C

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

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node *));
temp1=start;

if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->data=lData;
temp->next=NULL;

}
}
void display()
{
struct node *temp1=start;
if(temp1 == NULL )
printf("Linked list is empty\n");
else
{
while(temp1->next != NULL )
{
printf("Final Element is [%d]\n",temp1->data);
temp1=temp1->next;
}
printf("Final Element is [%d]\n",temp1->data);
}
}
/*void free_nodes()
{
struct node *temp1=start;
struct node *temp2;
if(start == NULL )
printf("Linked list is empty\n");
else
{
while(start->next != NULL )
{
temp2=start;
start=start->next;
free(temp2);
}
free(start);
start=NULL;
}
}*/

void free_nodes()
{
struct node *temp1=start;
struct node *temp2;
if(start == NULL )
printf("Linked list is empty\n");
else
{
for(temp2=start;temp2!=NULL;temp2=temp1)
{
temp1=temp2->next;
free(temp2);
}
start=NULL;
}
}
int calculate_length()
{
struct node *temp1=start;
int count=0;
if(temp ==  NULL )
return 0;
else
{
while(temp1->next != NULL )
{
temp1=temp1->next;
count++;
}
count++;
}
return count;
}
int main()
{
int choice;
while(1)
{
printf(" 1. For Insert into link list\n 2. For Free Nodes \n 3. Display link list\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
free_nodes();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Create and check a loop in a linked list in C

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

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;
int gLoopCounter;
void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node *));
temp1=start;

if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
gLoopCounter++;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
gLoopCounter++;
if(gLoopCounter == 3)
{
temp1->next=temp;
temp->data=lData;
temp->next=temp1;
}
else
{
temp1->next=temp;
temp->data=lData;
temp->next=NULL;
}
}
}

void checkLoop()
{
struct node *temp=start;
while(1)
{
if(temp->next->next == temp)
{
printf("Loop Found at node with data [%d]\n",temp->next->data);
break;
}
else
temp=temp->next;
}
}
int main()
{
int choice;
printf("note -------------Enter only 3 nodes\n");
while(1)
{
printf(" 1. Insert and create loop at 3rd node \n 2. Check for loop \n 3. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
checkLoop();
break;
case 3:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Reverse a link list in C without using pointers

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int sData;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert(int lValue)
{
struct node *temp1;
temp1=malloc(sizeof(struct node));
temp=start;
if(NULL == start)
{
start=temp1;
temp1->sData=lValue;
temp1->next=NULL;
}
else
{
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=temp1;
temp1->sData=lValue;
temp1->next=NULL;
}
}
void display()
{
struct node *temp=start;
if(NULL == temp)
{
printf("Link list is empty\n");
}
else
{
while(temp->next !=NULL)
{
printf("Element is [%d]\n",temp->sData);
temp=temp->next;
}
printf("Last Element is [%d]\n",temp->sData);

}
}
int linkedlist_len()
{
struct node *temp=start;
int len=0;
if(NULL == start)
{
return len;
}
else
{
while(temp->next !=NULL)
{
len++;
temp=temp->next;
}
len++;
}
return len;
}
void reverse()
{
int i=0;
int *arr;
int llength=0;
llength=linkedlist_len();
if(start == NULL )
{
printf("Link list is empty\n");
}
else
{
struct node *temp=start;
arr=malloc(llength*sizeof(int ));
for(i=0;i<llength;i++)
{
arr[i]=temp->sData;
temp=temp->next;
}
temp=start;
for(i=llength-1;i>=0;i--)
{
temp->sData=arr[i];
temp=temp->next;
}
free(arr);
}
}


int main()
{
int choice;
int lData;
while(1)
{
printf(" 1. For Insert into link list\n 2. Display link list \n 3. Reverse a link list Without Using Pointers\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Element:\t");
scanf("%d",&lData);
insert(lData);
break;
case 2:
display();
break;
case 3:
reverse();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Wednesday, September 14, 2016

Atoi Implementation in C

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char lStr[]="-1234";
int lNum = 0 , i = 0 ;
int lMinus = 1;
if(lStr[0] == '-')
{
lMinus=-1;
}
printf("String Before Conversion [%s]\n",lStr);
for (i=0 ;lStr[i] != '\0';i++)
{
if(lStr[i] == '-')
{
continue;
}
else
lNum=lNum * 10 + lStr[i] - 48;
}
lNum=lNum*lMinus;
printf("String After Conversion [%d]\n",lNum);

return 0;

}

memmove implementation code in C

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

void mem_move_foo(void *dest, void *src, int total_bytes)
{
int i = 0;
char *lSrcStr=(char *) src;
char *lDestStr=(char *) dest;

char *lTempStr;

lTempStr= malloc(total_bytes*sizeof(char*));

for(i=0 ;i < total_bytes ; i++)
  {
lTempStr[i] = lSrcStr[i];
}

for(i=0;i<total_bytes;i++)
{
lDestStr[i]=lTempStr[i];
}

free(lTempStr);
}


int main()
{
char srcStr[]="HelloSrc";

printf("String before :: [%s]\n",srcStr );

mem_move_foo(srcStr+5,srcStr,strlen(srcStr)+1);

printf("String after ::  [%s]\n",srcStr );


return 0;
}

Code to Sort a singly link list in C

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int sData;
struct node *next;

};
struct node *start=NULL;
struct node *temp=NULL;

void insert(int lValue)
{
struct node *temp1;
temp1=malloc(sizeof(struct node *));

temp=start;
if(start == NULL )
{
start=temp1;
temp1->sData=lValue;
temp1->next=NULL;
}
else
{
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=temp1;
temp1->sData=lValue;
temp1->next=NULL;
}
}
void display()
{
struct node *temp=start;
if( NULL == start)
{
printf("Link list is empty\n");
}
else
{
while(temp->next != NULL )
{
printf(" Element is  [%d]\n",temp->sData);
temp=temp->next;
}
printf("Last Element is  [%d]\n",temp->sData);
}
}
void sort()
{
struct node *temp,*temp1;
if(NULL == start)
{
printf("Cant Sort: Link List is empty\n");
}
else
{
temp1=start;
while(temp1->next !=NULL)
{
temp=start;
while(temp->next !=NULL)
{
if(temp->sData > temp->next->sData)
{
int temp1=temp->sData;
temp->sData=temp->next->sData;
temp->next->sData=temp1;
}
temp=temp->next;
}
temp1=temp1->next;
}
}
}

int main()
{
int choice;
int lData;
while(1)
{
printf("1. Insert into Linked list\n 2. Display Link List\n 3. Sort Linked List\n 4. Exit\n Enter Your Choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Element:\t");
scanf("%d",&lData);
insert(lData);
break;
case 2:
display();
break;
case 3:
sort();
break;
case 4:
exit(1);
break;
default:
break;
}
}
return 0;
}

Monday, September 12, 2016

Code to Reverse A Singly Link List in C

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

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node *));
temp1=start;

if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->data=lData;
temp->next=NULL;
}
}
void display()
{
struct node *temp1=start;
if(temp == NULL )
printf("Linked list is empty\n");
else
{
while(temp1->next != NULL )
{
printf("Final Element is [%d]\n",temp1->data);
temp1=temp1->next;
}
printf("Final Element is [%d]\n",temp1->data);
}
}
void reverse()
{
struct node *prev=NULL;
struct node *current=start ;
struct node *temp;
while(current != NULL)
{
temp=current->next;
current->next=prev;
prev=current;
current=temp;
}
start=prev;
}
int main()
{
int choice;
while(1)
{
printf(" 1. For Insert into link list\n 2. Display link list\n 3. Reverse a link list\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
reverse();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Linked list Delete Middle Element Code

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

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node *));
temp1=start;

if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->data=lData;
temp->next=NULL;
}
}
void display()
{
struct node *temp1=start;
if(temp == NULL )
printf("Linked list is empty\n");
else
{
while(temp1->next != NULL )
{
printf("Final Element is [%d]\n",temp1->data);
temp1=temp1->next;
}
printf("Final Element is [%d]\n",temp1->data);
}
}
int calculate_length()
{
struct node *temp1=start;
int count=0;
if(temp ==  NULL )
return 0;
else
{
while(temp1->next != NULL )
{
temp1=temp1->next;
count++;
}
count++;
}
return count;
}
void delete_middle()
{
struct node *temp1=start;
int count =1;
int length=0;
int middle=-1;
length=calculate_length();
if(length%2 == 0 )
{
middle=length/2;
}
else
{
middle=(length/2)+1;
}
while(1)
{
printf("count [%d] middle [%d]\n",count,middle);
if(count == middle - 1)
{
struct node *del=temp->next;
temp1->next=temp1->next->next;
free(del);
break;
}
else
temp1=temp1->next;
count++;
}
}
int main()
{
int choice;
while(1)
{
printf(" 1. For Insert into link list\n 2. For Delete Middle\n 3. Display link list\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete_middle();
printf("delete middle\n");
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}