C code for removing duplicate nodes in linked list


linked list implementation in C
//removing duplicate elements without sorting
//using two loops
#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();
void deletee();
void reverse();
void removeDup();
struct node
{
int data;
struct node *next;
};
struct node *start;

int main()
{
printf("\n\n");
start=NULL;//starting with empty list
insert(10);
insert(10);
insert(10);
insert(15);
insert(15);
insert(15);
insert(23);
insert(15);
insert(23);
insert(15);
insert(77);
insert(99);
insert(99);
insert(99);
printf("\ndisplay() called:\n");
display();
printf("\n\n");

printf("\nremoveDup() called:\n");
removeDup();
display();
printf("\n\n");

printf("\ndeletee() called\n");
deletee();
display();
printf("\n\n");

printf("\nreverse() called\n");
reverse();
display();
printf("\n\n");

return 0;
}

void insert(int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}
void display()
{
struct node *ptr=start;
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
void deletee()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=start;
start=start->next;
if(temp!=NULL)
{
free(temp);
}
}

void reverse()
{
struct node *next=(struct node*)malloc(sizeof(struct node));
struct node *result=(struct node*)malloc(sizeof(struct node));
struct node *current=(struct node*)malloc(sizeof(struct node));
next=NULL;
result=NULL;
current=start;
while(current!=NULL)
{
next=current->next;
current->next=result;
result=current;
current=next;
}
start=result;
}


void removeDup()
{
struct node *current=(struct node*)malloc(sizeof(struct node));
current=start;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=NULL;
struct node *ptr1, *ptr2;
ptr1=start;
while(ptr1!=NULL )//&& ptr1->next!=NULL)
{
ptr2=ptr1;
while(ptr2->next!=NULL)//->next!=NULL)
{
if(ptr1->data==ptr2->next->data)
{
temp=ptr2->next;
ptr2->next=ptr2->next->next;
free(temp);
}
else
{
ptr2=ptr2->next;
}
}
ptr1=ptr1->next;
}
}
/*

display() called:
99 99 99 77 15 23 15 23 15 15 15 10 10 10


removeDup() called:
99 77 15 23 10


deletee() called
77 15 23 10


reverse() called
10 23 15 77


*/

C code for finding merge point of two linked lists


//finding the merge point of two lists
//list1 contains 10,15,30
//list 2 contains 3,6,9,15,30 such that
//15 in both the lists are stored in same location ie merge point
/*ALGO:
-count the list1 say c1
-count the list2 say c2
-absoulte difference say d
-traverse the longer list by d nodes
-then traverse both the lists simueltaeously
-when two nodes gets equal in address that is point of merge
*/
#include<stdio.h>
#include<stdlib.h>

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


void display(struct node *head1);
int countNode(struct node *head1);
int mergePoint(struct node *head1,struct node *head2,int d);
int main()
{
struct node *head1=(struct node *)malloc(sizeof(struct node));
head1->data=10;
head1->next=NULL;

struct node *head2=(struct node *)malloc(sizeof(struct node));
head2->data=3;
head2->next=NULL;

struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=6;
head2->next=newnode;

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=9;
head2->next->next=newnode;

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=15;
head2->next->next->next=newnode;
head1->next=newnode;
printf("head2->next->next->next=newnode=%p\n",head2->next->next->next);
printf("head1->next=%p\n",head1->next);

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=30;
head1->next->next=newnode;
//head2->next->next->next->next=newnode;
head1->next->next->next=NULL;

display(head1);
printf("\n");
display(head2);
printf("\n");
int c1,c2,d;
c1=countNode(head1);
printf("\n");
c2=countNode(head2);
printf("\n");
if(c1>c2)
{
d=c1-c2;
printf("d=c1=%d\n",d);
}
else
{
d=c2-c1;
printf("d=c2=%d\n",d);
}
int t=0;
t=mergePoint(head1,head2,d);
printf("Merege point is at node:%d\n",t);
return 0;
}
//function to display the nodes
void display(struct node *h)
{
struct node *current=h;
while(current!=NULL)
{
printf("%d ",current->data);
current=current->next;
}

}
//function to count the nodes
int countNode(struct node *h)
{
int count=0;
struct node *current=h;
while(current!=NULL)
{
count=count+1;
current=current->next;
}
printf("size of list1=%d\n",count);

return count;
}
//function to give the merge point
int mergePoint(struct node *head1,struct node *head2,int d)
{
int i;
struct node *current1=head1;
struct node *current2=head2;
for(i=0;i<d;i++)
{
printf("i=%d,current2->data=%d\n",i,current2->data);
current2=current2->next;
}
while(current1!=NULL && current2!=NULL)
{
printf("while=\n");
if(current1==current2)
{
printf("\n\ncurrent1->data====%d\n\n",current1->data);
return current1->data;
}
current1=current1->next;
current2=current2->next;

}

}

/*OUTPUT:
head2->next->next->next=newnode=0x9dc7048
head1->next=0x9dc7048
10 15 30
3 6 9 15 30
size of list1=3

size of list1=5

d=c2=2
i=0,current2->data=3
i=1,current2->data=6
while=
while=
current->data====15
Merege point is at node:15*/

C code for sorted linked list ie inserting in sorted fashion


sorted linked list latest
//insert as first elt
//insert before first elt
//inserting at middle
//inserting at last
//deleting the given node first,middle,last


#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;

void insert(int d)
{
node *newnode=(node *)malloc(sizeof( node));
newnode->data=d;
newnode->next=NULL;

//insert as first elt
if(start==NULL)
{

//start->next=newnode;
start=newnode;
}
//insert before first elt
if(newnode->data < start->data)
{
newnode->next=start;
start=newnode;
return;
}
//inserting at middle
node* prev; node *ptr;
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=prev->next)
{
if(newnode->data < ptr->data)
{
prev->next=newnode;
newnode->next=ptr;
return;
}
}
//inserting at last
if(ptr==NULL)
{
prev->next=newnode;
newnode->next=NULL;//must
return;
}
}

void display()
{
node *ptr=start;
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;//must else infinite loop
}
printf("\n");
}

//deleting the given node
void deleteNode()
{
int key;
printf("Enter elt to delete:\n");
scanf("%d",&key);
//deleting the first node
if(key==start->data)
{
node *temp;
temp=start;
start=start->next;
free(temp);
return;
}
//deleting middle node
node *ptr; node *prev;
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=prev->next)
{
if(ptr->data==key)
{
prev->next=ptr->next;
free( ptr);
return;
}
}
//deleteing last elt
if(ptr==NULL)
{
printf("Ket not present in the LL.\n");
return;
}
if(start==NULL)
{
printf("No elt to del in the LL.\n");
return;
}
}

int main()
{
insert(10);
insert(4);
insert(8);
insert(9);
insert(6);
insert(7);
insert(25);
display();
deleteNode();
display();
deleteNode();
display();
deleteNode();
display();
return 0;
}
/*
4 6 7 8 9 10 25
Enter elt to delete:
7
4 6 8 9 10 25
Enter elt to delete:
4
6 8 9 10 25
Enter elt to delete:
25
6 8 9 10*/

C code for reversing linked list recursively


//linked list implementation in C
//display, reverse recursively
//recursive function to reverse a list
#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();

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

int main()
{
start=NULL;//starting with empty list
insert(10);//first element
insert(15);
insert(11);
insert(99);//last element

struct node *current1=(struct node*)malloc(sizeof(struct node));
current1=start;
printf("\nOriginal List was:\n");
display();
printf("\nRecursively reversed List is:\n");
revRec(current1);

return 0;
}
void insert(int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}


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




void revRec(struct node *current1)
{
if(current1==NULL)//here we can not take current1 and initialized with start otherwise always same conditiuon
return;

else
revRec(current1->next);

printf("current1=%d and current1->data=%d \n",current1,current1->data);

}
/*
Original List was:
ptr=160849976 and ptr->data=99
ptr=160849960 and ptr->data=11
ptr=160849944 and ptr->data=15
ptr=160849928 and ptr->data=10

Recursively reversed List is:
current1=160849928 and current1->data=10
current1=160849944 and current1->data=15
current1=160849960 and current1->data=11
current1=160849976 and current1->data=99
*/
/*
In stack:
10 was stored in lowest address then 15 then 11 and last 99 in higgest addres
when printrd in revRec
printed last entered element ie 99 then 11 then 15 then 10 last
*/

C Code for sorting linked list elements nodes


//linked list implementation in C
//insert at beginning
//displaing elements
//sort the elements in ascending order
#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();
void sortAscend();

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

int main()
{
start=NULL;//starting with empty list
insert(10);//first element
insert(15);
insert(12);
insert(1);
insert(99);//last element

printf("\nOriginal List was:\n");
display();
sortAscend();

return 0;
}
void insert(int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}


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


void sortAscend()
{
struct node *ptr1;
struct node *ptr2;
int temp;
if(start==NULL)
{
printf("List is empty.\n");
}
else
{
for (ptr1=start; ptr1; ptr1=ptr1->next)
{
for (ptr2=ptr1->next; ptr2; ptr2=ptr2->next)
{
if (ptr1->data > ptr2->data)
{
temp = ptr1->data;
ptr1->data = ptr2->data;
ptr2->data = temp;
}
}
}
printf("\nSorted List is:\n");
for (ptr1=start; ptr1!=NULL; ptr1=ptr1->next)
{
printf("%d\t", ptr1->data);
}
}
printf("\n");
}

/*
Original List was:
99 1 12 15 10

Sorted List is:
1 10 12 15 99
*/

C Code for reversing linked list using head


//implementing linked list using head
//printing recursive
//printing reverse recursive

#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};

void insert(struct node **,int );
//void display(struct node *head)
//void displayReverse(struct node *head);

int main()
{
printf("\n\n");
struct node *head=NULL;
insert(&head,10);
insert(&head,30);
insert(&head,19);
insert(&head,12);
insert(&head,99);

display(head);
printf("\n\n");

displayReverse(head);
printf("\n\n");

delete(&head);
printf("\n\n");

//display(head);
//printf("\n\n");
return 0;
}




void insert(struct node **head,int d)
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
if(*head==NULL)
{
*head=newnode;
(*head)->next=NULL;
}
else
{
newnode->next=*head;
*head=newnode;
}
}

void display(struct node *head)
{
//printf("List in actual order is:\n");
printf("%d ",head->data);
if(head->next==NULL)
{
return;
}
else
{
display(head->next);
}
}

void displayReverse(struct node *head)
{
//printf("%d ",head->data);
if(head==NULL)
return;
else
displayReverse(head->next);
//printf("List in reversed order is:\n");
printf("%d ",head->data);
}


void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
/*
99 12 19 30 10
10 30 19 12 99 */

C code for inserting at middle, deleting at middle


/*Linked list first last middle insert without create function search and delete */
#include<stdio.h>
#include<malloc.h>

struct node
{
int data;
struct node *next;
};
struct node *first=NULL;
struct node *ptr=NULL;
struct node *prev=NULL;
struct node *last=NULL;


struct node *newnode=NULL;

int val,pos,count=0,i;

void insert_first();
void insert_last();
void insert_pos();
void display();
int main()
{
display();
insert_first();
insert_first();
insert_first();
display();
printf("\n");
insert_last();
insert_last();
insert_last();
display();
insert_pos();
insert_pos();
display();
printf("\n");
return 0;
}

void insert_first()
{
printf("enter a number to insert at first:\n");
scanf("%d",&val);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;

if(first==NULL)
{
first=newnode;
first->next=NULL;
}
else
{
newnode->next=first;
first=newnode;
}
}


void display()
{
if(first==NULL)
{
printf("No element to display.\n");
}
else
{
/*for(ptr=first;ptr!=NULL;ptr=ptr->next)
{
printf("%d\t",ptr->data);
}*/
ptr=first;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
}
}



void insert_last()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter a number to insert at last:\n");
scanf("%d",&newnode->data);
newnode->next=NULL;

if(first==NULL)
{
first=newnode;
}

else
{
ptr=first;
while(ptr->next!=NULL)//ptr-next is must as segflt
{
ptr=ptr->next;
}
ptr->next=newnode;
newnode->next=NULL;
}
}



void insert_pos()
{
int pos, val, cnt = 0, i;
printf("\nEnter the position:\n ");
scanf("%d", &pos);

printf("\nEnter the value for the Node:\n");
scanf("%d", &val);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=val;
ptr = first;
while (ptr != NULL)
{
ptr = ptr->next;
cnt++;
}
if (pos == 1)
{
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
newnode->next=first;
first=newnode;
}
printf("\nInserted\n");
}
else if (pos>1 && pos<=cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
newnode->next = ptr;
printf("\nData inserted.\n");
}

else
{
printf("\nPosition is out of range\n");
}
}
/*
No element to display.
enter a number to insert at first:
1
enter a number to insert at first:
2
enter a number to insert at first:
3
3 2 1
enter a number to insert at last:
4
enter a number to insert at last:
5
enter a number to insert at last:
6
3 2 1 4 5 6
Enter the position:
2

Enter the value for the Node:
22

Data inserted.

Enter the position:
1

Enter the value for the Node:
11

Inserted
11 3 22 2 1 4 5 6
*/

C code for implementing tree operations traverse


tree implementation in c
#include<stdio.h>
#include<malloc.h>
void insert(int);
void traverse();
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
int main()
{
root=NULL;
insert('B');
insert(10);
insert(5);
insert(15);
insert(12);
insert(2);
traverse();
return 0;
}

void insert(int d)
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
newnode->left=NULL;
newnode->right=NULL;

struct node *ptr=root;
if(root==NULL)
{
root=newnode;
//newnode->left=NULL;
//newnode->right=NULL;
}

else
{
while(ptr!=NULL)
{
if(d<ptr->data)
{
if(ptr->left==NULL)
{
ptr->left=newnode;
ptr=NULL;
}
else
{
ptr=ptr->left;
}
}
else
{
//if(d>ptr->data)
//{
if(ptr->right==NULL)
{
ptr->right=newnode;
ptr=NULL;
}
else
{
ptr=ptr->right;
}
}
}
}//else first ends
}//insert ends


void inorder(struct node *ptr);
void preorder(struct node *ptr);
void postorder(struct node *ptr);
void traverse()
{
printf("\nInorder\n");
inorder(root);
printf("\nPre order\n");

preorder(root);
printf("\nPost Order\n");
postorder(root);
printf("\n\n");
}//traverse ends here


void inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
printf("%d ",ptr->data);
inorder(ptr->right);
}
}

void preorder(struct node *ptr)
{
if(ptr!=NULL)
{
printf("%d ",ptr->data);
preorder(ptr->left);
preorder(ptr->right);
}
}
void postorder(struct node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
printf("%d ",ptr->data);
}
}
/*
Inorder
2 5 10 12 15 66
Pre order
66 10 5 2 15 12
Post Order
2 5 12 15 10 66
*/

C code for Linked List Operations at beginning


linked list implementation in C
//insert at beginning,delete at beginning
//display, reverse

#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();
void deletee();
void reverse();
struct node
{
int data;
struct node *next;
};
struct node *start;

int main()
{
printf("\n\n");
start=NULL;//starting with empty list
insert(10);
insert(10);
insert(15);
insert(10);
insert(11);
insert(15);
insert(99);
printf("\ndisplay() called:\n");
display();

printf("\ndeletee() called:\n");
deletee();
display();

printf("\nreverse() called:\n");
reverse();
display();
printf("\n\n");
return 0;
}

void insert(int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}
void display()
{
struct node *ptr=start;
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
void deletee()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=start;
start=start->next;
if(temp!=NULL)
{
free(temp);
}
}

void reverse()
{
struct node *next=(struct node*)malloc(sizeof(struct node));
struct node *result=(struct node*)malloc(sizeof(struct node));
struct node *current=(struct node*)malloc(sizeof(struct node));
next=NULL;
result=NULL;
current=start;
while(current!=NULL)
{
next=current->next;
current->next=result;
result=current;
current=next;
}
start=result;
}

/*
display() called:
99 15 11 10 15 10 10
deletee() called:
15 11 10 15 10 10
reverse() called:
10 10 15 10 11 15
*/

C code for deleting duplicate words in a sentence

C Program to delete all duplicate words in a sentence
//C code for removing repeated words of among multiple strings

#include <stdio.h>

#include <string.h>

void main()

{

    char a[100], b[20][20];
    int i, j = 0, k = 0, n, m;
    printf("enter a string\n");
    scanf("%[^\n]s", a);
    for (i = 0;a[i] != '\0';i++)
    {
        if (a[i] == ' ')
        {
            b[k][j] = '\0';
            k++;
            j = 0;
        }
        else
        {
            b[k][j] = a[i];
            j++;
        }
    }
    b[k][j] = '\0';
    for (i = 0;i <= k;i++)
    {
        for (j = i + 1;j <= k;j++)
        {
            if (strcmp(b[i], b[j]) == 0)
            {
                for (m = j;m <= k;m++)
                    strcpy(b[m], b[m + 1]);
                k--;
            }
        }
    }
 for (n = 0;n <= k;n++)
    {
        printf("%s ", b[n]);
    }
}

/*

enter a string:

india is great is countrin in india asia great

india is great countrin in asia


*/

C Code for deleting repeated characters in a sentence

C Program to delete duplicate characters in a string/sentence
//C code for removing repeated characters of a string/sentence

#include<stdio.h>

#include<string.h>

int main()

{

char a[200];

int i,j,k,n;


printf("Enter a few chars and strings:\n");

gets(a);

printf("\nEntered string is :%s \n",a);


printf("\nUpdated string without repeated characters is\n");

n=strlen(a);

for(i=0;i<n;i++)

{

   for(j=i+1;j<n;)//mind it no icrement
   {
      if(a[j]==a[i])
      {
         for(k=j;k<n;k++)
{

             a[k]=a[k+1];
}

          n--;//else array will contain 0 in deleted duplicate value
      }
      else
         j++;
   }
}


for(i=0;i<n;i++)

    printf("%c",a[i]);
printf("\n");

}

/*

Enter a few chars and strings:

india is great aa bb cc dd ee ff gg hh ii jj kk


Entered string is :india is great aa bb cc dd ee ff gg hh ii jj kk


Updated string without repeated characters is


inda sgretbcfhjk

*/

C code for deleting duplicate elements in an array

C Program to delete duplicate elements in an array
//C code for removing repeated values of an array


#include<stdio.h>

int main()

{

int a[20],i,j,k,n;


printf("\nnEnter array size : ");

scanf("%d",&n);


printf("\nnAccept Numbers : ",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);



printf("\nnOriginal array is : ");

for(i=0;i<n;i++)

printf(" %d",a[i]);


printf("\nnUpdated array is  : ");

for(i=0;i<n;i++)

{

   for(j=i+1;j<n;)
   {
      if(a[j]==a[i])
      {
         for(k=j;k<n;k++)
{

             a[k]=a[k+1];
}

          n--;//else array will contain 0 in deleted duplicate value
      }
      else
         j++;
   }
}


for(i=0;i<n;i++)

    printf("%d \t",a[i]);
printf("\n");

}

/*

array size : 5

Numbers : 22

33

44

44

22

array is :  22 33 44 44 22

array is  : 22         33      44


*/