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


*/