Monday, 20 January 2014

Program : Reversing a linked list

Program : Reversing a  linked list

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

struct link
{
          int no;
          struct link *next;
};

struct link *node,*start,*pre,*current,*counter,*p;
int i;

void create_link(struct link *node);
void display(struct link *node);
struct link *reverse(struct link *next);

void main()
{
          clrscr();
          node=(struct link *)malloc(sizeof(struct link));
          p=(struct link *)malloc(sizeof(struct link));

          create_link(node);
          printf("\n List before reversing\n");
          display(node);

          getch();

          p=reverse(node);
          printf("\n List after reversing");
          display(p);

    getch();
}

void create_link(struct link *node)
{
          char ans;
          i=0;
         
         printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {

                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);

                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;

                   node->next=NULL;

                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
            printf("\n Total number of nodes : %d ", i);
}

void display(struct link *node)
{
          while(node)
          {
                   printf("\n  %d  %u " ,node->no,node);
                   node=node->next;
          }

}

struct link *reverse(struct link *node)
{
   current=node;
   pre=NULL;

   while(current->next)
    {
       counter=(struct link *)malloc(sizeof(struct link));
       counter=current->next;
      
       current->next=pre;
       pre=current;

       current=counter;

    }
    node=pre;
    return(node);

}

Program : Concatenation of two singly linked list

Program : Concatenation of two singly linked list

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

struct link
{
          int no;
          struct link *next;
};

struct link *list1,*list2,*list3;
int i;

void create_link(struct link *node);
void display(struct link *node);

void concat_link(struct link *list1,struct link *list2, struct link *list3);
void main()
{
          clrscr();
          list1=(struct link *)malloc(sizeof(struct link));
          list2=(struct link *)malloc(sizeof(struct link));
          list3=(struct link *)malloc(sizeof(struct link));
          printf("\n Create list -1 ");
                create_link(list1);

                printf("\n Create list -2 ");
                create_link(list2);

                printf("\n Output List -1\n");
                display(list1);

                printf("\n Output List -2\n");
                display(list2);

                concat_link(list1,list2,list3);

                printf("\n List after concatenation \n");
                display(list3);

               getch();
}

void create_link(struct link *node)
{
          char ans;
          i=0;

          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {

                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   node->next=NULL;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
            printf("\n Total number of nodes : %d ", i);
}

void display(struct link *node)
{

          while(node->next)
          {
                   printf("\n  %d",node->no);
                   node=node->next;
          }

}
void concat_link(struct link *list1,struct link *list2, struct link *list3)
{
 // copy first list
 while(list1->next!=NULL)
 {
       list3->no=list1->no;
       list1=list1->next;
       list3->next=(struct link *)malloc(sizeof(struct link));
       list3=list3->next;
 }

// copy second list

while(list2->next!=NULL)
 {
       list3->no=list2->no;
       list2=list2->next;
       list3->next=(struct link *)malloc(sizeof(struct link));
       list3=list3->next;
 }

list3->next=NULL;

}

Character Array(String) Programs

 Character Array(String) Programs

Program -1  : Array of characters
// compile time initialization

#include<conio.h>
#include<stdio.h>

void main()
{
char nm[]={ 's','h','i','v','\0'};
char name[]="Parvati";
          int i;
          clrscr();
          printf("\n Name : %s",nm);
          printf("\n Name1 : %s",name);
          for(i=0;i<5;i++)
          {
                   printf("\n  %5c",nm[i]);
          }
          getch();
}


Program -2  : Write a program to create triangle of given string
/* character triangle
S
S h
S h i
S h i v
*/
#include<conio.h>
#include<stdio.h>

void main()
{
          char nm[20];
          int i,j;
          clrscr();
          printf("\n Enter your name : ");
          gets(nm);
          for(i=0;nm[i]!=NULL;i++)
          {
                   for(j=0;j<=i;j++)
                   {
                             printf(" %c",nm[j]);
                   }
                   printf("\n");
          }
          getch();
}

Program -3  : Write a program to calculate length of String without using any Library function.

#include<conio.h>
#include<stdio.h>

void main()
{
          char nm[100];
          int i,l=0;
          clrscr();
          printf("\n Enter nay string : ");
          gets(nm);

          // find length
          for(i=0;nm[i]!=NULL;i++)
          {
                   l++;
          }
          printf("\n Length = %d",l);
          // print each character
          for(i=0;i<l;i++)
          {
                   printf("\n %c",nm[i]);

          }

          getch();
}


Program -4  : Write a program to count no. of vowels and consonant from given string.

#include<conio.h>
#include<stdio.h>
#include<ctype.h>

void main()
{
          char nm[20];
          int i,v=0,c=0;
          clrscr();

          printf("\n Enter any string : " );
          gets(nm);

          for(i=0;nm[i]!=NULL;i++)
          {
          if(isalpha(nm[i]))
            {
            if(nm[i]=='a' || nm[i]=='e' || nm[i]=='i' || nm[i]=='o' || nm[i]=='u'|| nm[i]=='A' || nm[i]=='E' || nm[i]=='I' || nm[i]=='O' || nm[i]=='U')
                             v++;
           else
                             c++;
          }
          }
          printf("\n Vowels = %d",v);
          printf("\n Consonents = %d",c);
          getch();
}



Program 5  : Write a program to copy one string to other string without using liberary function.

#include<conio.h>
#include<stdio.h>

void main()
{
          char s1[30],s2[30];
          int l=0,i;
          clrscr();
          printf("\n Enter any string : ");
          gets(s1);
          while(s1[l]!='\0')
          {
                   l++;
          }
          for(i=0;i<=l;i++)
          {
                   s2[i]=s1[i];
          }
          s2[i+1]='\0';
          printf("\n Copy = %s",s2);
          getch();
}


Program-6 :  Write a program to count total number of words from given string and display each word in new line.

 Ex :  I like C Language 
I
Like
C
Language
Total words = 4

#include<conio.h>
#include<stdio.h>

void main()
{
          char s[100];
          int l=0,i,c=1;
          clrscr();
          printf("\n Enter any string : ");
          gets(s);
          while(s[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=0;i<l;i++)
          {
                   printf("%c",s[i]);
                   if(s[i]==' '&& s[i+1]!=' ')
                   {
                             c++;
                             printf("\n");
                   }
          }
printf("\n Total No. Of Words = %d",c);
          getch();
}


Program-7 :  Write a program to reverse given string.
Ex :
Original string  : Shiv
Reverse String : vihS

#include<conio.h>
#include<stdio.h>

void main()
{
          char s1[30],s2[30];
          int l=0,i,j=0;
          clrscr();
          printf("\n Enter any string : ");
          gets(s1);
          while(s1[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=l-1;i>=0;i--)
          {
                   s2[j]=s1[i];
                   j++;
          }
          s2[j]='\0';
printf("\n Original String = %s",s1);
printf("\n Reverse String  = %s",s2);

getch();
}


Program-8 :  Write a program to check whether given string is palindrome or not.
Ex:
String : nayan
This is palindrome

#include<conio.h>
#include<stdio.h>

void main()
{
          char s1[30],s2[30];
          int l=0,i,j=0,flag=0;
          clrscr();
          printf("\n Enter any string : ");
          gets(s1);
          while(s1[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=l-1;i>=0;i--)
          {
                   s2[j]=s1[i];
                   j++;
          }
          s2[j]='\0';
          printf("\n Original String = %s",s1);
          printf("\n Reverse String  = %s",s2);

// logic to check palindrome string

          for(i=0;i<l;i++)
          {
                   if(s1[i]!=s2[i])
                   {
                             flag=1;
                             break;
                   }
          }
          if(flag==1)
                   printf("\n String is not palindrome");
          else
                   printf("\n String is palindrome");
          getch();
}



Program-9 :  Write a program to convert all lowercase letter  to uppercase letter of given string.

#include<conio.h>
#include<stdio.h>

void main()
{
          char s[30];
          int l=0,i,c=1;
          clrscr();
          printf("\n Enter any string : ");
          gets(s);
          while(s[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=0;i<l;i++)
          {
                   if(s[i]>='a' && s[i]<='z')
                   {
                             s[i]=s[i]-32;
                   }
          }
          printf("\n Name = %s",s);
          getch();
}


Program-10 :  Write a program to convert all uppercase letter to lowercase
And  lowercase letter to uppercase of the given string.

#include<conio.h>
#include<stdio.h>

void main()
{
          char s[30];
          int l=0,i,c=1;
          clrscr();
          printf("\fn Enter any string : ");
          gets(s);
          while(s[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=0;i<l;i++)
          {
                   if(s[i]>='a' && s[i]<='z')
                   {
                             s[i]=s[i]-32;
                   }
                   else if(s[i]>='A' && s[i]<='Z')
                   {
                             s[i]=s[i]+32;
                   }
                   else
                   {}
          }
          printf("\n Name = %s",s);
          getch();
}

Posted By :Ruchita Pandya

Saturday, 16 March 2013

Program - Shell sort


Program - Shell sort

// Shell  sort

#include<stdio.h>
#include<conio.h>

void shell_sort(int[],int);
void output(int [],int);

void main()
{
     int values[50],n,i,j,temp;
     clrscr();

     printf("How Many Elements You Want To Enter ?  :");
     scanf("%d",&n);

     for(i=0;i<n;i++)
       {
 printf("\n Enter any number : ");
 scanf("%d",&values[i]);
       }

       printf("\n List before sorting");
       output(values,n);
             
       shell_sort(values,n);

       printf("\n list after sorting ");
       output(values,n);
       getch();
}
void output(int values[],int n)
{
int i;
for(i=0;i<n;i++)
{
 printf("\n  %d",values[i]);
}

}
void shell_sort(int array[],int size)
{
  int temp,gap,i,swap;

  gap=size/2;

  do
   {

     do
       {
        swap=0;
  
  
      for(i=0;i<size-gap;i++)
        {

if(array[i]>array[i+gap])
           {
                   temp=array[i];
                   array[i]=array[i+gap];
                   array[i+gap]=temp;
                   swap=1;
           }

    }
}while(swap);
  }while(gap=gap/2);
}

Program - Insertion Sort


Program - Insertion Sort 

// Insertion Sort

#include<stdio.h>
#include<conio.h>

void insertion_sort(int[],int);
void output(int[],int);

void main()
{
          int values[50],i,n;
          clrscr();

          printf("\n How many elements you want to Enter ? : ");
          scanf("%d",&n);

          for(i=1;i<=n;i++)
          {
                   printf("\n Enter any number : ");
                   scanf("%d",&values[i]);
          }

          printf("\n List before sorting");
          output(values,n);

          insertion_sort(values,n);
          printf("\n List after sorting");
          output(values,n);
          getch();
}

void output(int l[],int n)
{
          int i;

          for(i=1;i<=n;i++)
          {
                   printf("\n  %d",l[i]);
          }
}





void insertion_sort(int l[],int n)
{
          int temp,i,pointer;
          l[0]=0;
          for(i=1;i<=n;i++)
          {
            temp=l[i];
            pointer=i-1;

            while(temp<l[pointer])
             {
                   l[pointer+1]=l[pointer];
                   pointer--;
             }
             l[pointer+1]=temp;
          }
}

Program - Priority Queue

Program - Priority Queue
// Priority queue

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

#define MAX 5

// Structure for data

struct data
{
  int val,p,o;
}d[MAX];

 int front=-1,rear=-1;

// Insert operation
void  insert(struct data d1)
{
   struct data temp;
   int i,j;

   if(rear==MAX-1)
  {
    printf("\n Priority Queue is Full...");
    getch();
  }
  else
  {
    rear++;
    d[rear]=d1;
    if(front==-1)
      front=0;
    for(i=front;i<=rear;i++) // arrange priority 
    {
      for(j=i+1;j<=rear;j++)
      {
if(d[i].p > d[j].p)
{
 temp=d[i];
 d[i]=d[j];
 d[j]=temp;
}
else
{
 if(d[i].p==d[j].p) // if two job has same priority
 {
   if(d[i].o > d[j].o) // arrange as per order from
   {
     temp=d[i];
     d[i]=d[j];
     d[j]=temp;
   }
 }
}
      }
    }
  }
}
// Delete operation
void  deletion()
{
  struct data d1;
  if(front==-1)
    printf("\n Priority Queue is Empty...");
  else
  {
    printf("-------------------------------\n");
    printf("\n Value:    %d",d[front].val);
    printf("\n Priority: %d",d[front].p);
    printf("\n Order:    %d",d[front].o);
    printf("\n -------------------------------");
    if(front==rear)
      front=rear=-1;
    else
      front++;
  }
}

// Display operation
void  display()
{
   int i;

  if(front==-1)
    printf("\n Priority Queue is Empty...");
  else
  {
    for(i=front;i<=rear;i++)
    {
      printf("\n -------------------------------");
      printf("\n Object :  %d",i+1);;
      printf("\n Value  :  %d",d[i].val);
      printf("\n Priority: %d",d[i].p);
      printf("\n Order  :  %d",d[i].o);
      printf("\n -------------------------------");
    }
  }
}

// main() section
void main()
{
  struct data d1;
  // Executable section
  do
  {
    int ch;
    clrscr();
    // Print menu
    printf("\n-------------------");
    printf("\n     M E N U             ");
    printf("\n-------------------");
    printf("\n1 Insertion");
    printf("\n2 Deletion");
    printf("\n3 Display");
    printf("\n4 Exit");
    printf("\n-------------------");
    printf("\nEnter Choice... ");
    scanf("%d",&ch);
    switch(ch)
    {
       // Insertion case
       case 1:
printf("\n\t\tEnter Value:- ");
scanf("%d",&d1.val);
printf("\n\t\tEnter Priority:- ");
scanf("%d",&d1.p);
printf("\n\t\tEnter Order:- ");
scanf("%d",&d1.o);
insert(d1);
break;
       // Deletion case
       case 2:
deletion();
getch();
break;
       // Display case
       case 3:
display();
getch();
break;
       // Exit case
       case 4:
exit(0);
       // Default case
       default:
printf("\nInvalid Choice...");
getch();
    }
  }while(1);

  getch();
}

Posted By : Ruchita Pandya