Friday, 31 October 2014

Single Linked List - inserting a node at the end

Aim:
To insert a node at the end of the single linked list.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdib.h>
struct slinkedlist
{
   int data;
   struct slinkedlist *next;
}
typedef struct slinkedlist node;
node *start = NULL;

node* getnode ()
  {
    node *newnode;
   newnode = (node*)malloc(sizeof(node));
printf("\nEnter a data");
scanf("%d",&newnode->data);
newnode->next = NULL;
rerun newnode;
  }

void creatlist (int n)
  {
    int i ;
    node *newnode;
    node *temp; 
for ( i=0; i<n; i++)
    {
       newnode = getnode ();
    if ( start == NULL)
      {
         start = newnode;
      }
    else
       {
         temp = start;
     while( temp->next != NULL)
         temp = temp->next;
         temp->next = newnode;
        }
  }
}

void insert_beg ()
{
node *newnode;
node *temp;
newnode = getnode ();
  if ( start == NULL)
    {
     start = newnode;
    }
else
{
  newnode->next = start;
  start = newnode;
}
}

void insert_end ()
{  
    int i ;
    node *newnode;
    node *temp; 
       newnode = getnode ();
    if ( start == NULL)
      {
         start = newnode;
      }
    else
       {
         temp = start;
     while( temp->next != NULL)
         temp = temp->next;
         temp->next = newnode;
        }
}

void main()
  {
    int n;
clrscr ();
  if ( start == NULL )
{
    printf("\n Enter Number of nodes to form single linked list:");
    scanf("%d",&n);
  creatlist ( int n);
}
else
printf("\n List is already created:");
printf("\n Enter 1 to insert at beginning Or Enter 2 to insert at end: ");
scanf("%d",&num)
  if( num == 1)
{
   insert_beg();
   printf("\n a node is inserted at beginning");
  }
else  if( num == 2)
{
   insert_end();
   printf("\n a node is inserted at end");
  }
else
{
printf("\n Entered a wrong choice");
}
}

Wednesday, 29 October 2014

Palindrome ( with built-in functions )

Aim:
To check given string is palindrome or not.

Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main ()
  {
     char str[80],revstr[80];
     clrscr ( );
printf("\nEnter a string:");
   scanf("%s",&str)
   strcpy(revstr,str);
   strrev(revstr);
if(!strcmp(str,revstr))
   printf("\nGiven string '%s' is Palindrome",str);
else
   printf("\nGiven string '%s' is not Palindrome",str);
}

Output:
Enter a string: medem
Given string 'medem' is Palindrome

Single Linked List - Inserting a node at the beginning

Aim:
To create a linked list and insert a node at beginning.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdib.h>

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

node* getnode()
  {
    node *newnode;
    newnode = (node*)malloc(sizeof(node));
  printf("\n Enter data:");
  scanf("%d",&newnode->data);
newnode->next = NULL;
return newnode;
}

void createlist( int n )
  {
     int n;
     node *newnode;
     node *temp;
for( i=0;i<n;i++)
   {
      newnode = getnode ();
if( start == NULL)
   {
      start = newnode ;
    }
else
   {
      temp = start ;
while( temp->next != NULL )
    temp = temp->next;
    temp->next = newnode;
   }
}
}

void insert_beg ();
{  node *newnode;
    newnode = getnode() ;
   if ( start == NULL)
      {
         start = newnode;
      }
   else
      {
         newnode->next = start;
          start = newnode;
      }
  }     

void main ()
{
    int n,num;
    if( start == NULL)
{
   printf("\n Enter Number of nodes to creatlinked list:");
   scanf("%d",&newnode->data);
creatlist( n );
}
  else
printf("\n Linked list is created");

printf("\n Enter 1 to insert a node at beginning:");
scanf("%d",&num);
if( num == 1)
      insert_beg ();
else
   printf("\nYou not intrested to insert a node at beginning:");

}
      

Single Linked list - Createl ist

Aim:
To create a single linked list with specified nodes.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdib.h>
struct slinkedlist
   {
      int data;
      struct slinked list* next;
  }
typedef struct slinked list* node;
node* start = NULL;

node* getnode()
  {
     node* newnode;
newnode =(node*)malloc(sizeof(node));
    printf("\nEnter a data:");
    scanf("%d",&newnode->data);
       newnode->next = NULL;
return newnode;
   }

void creatlist(int n)
  {
    int i;
    node* newnode;
    node* temp;
for( i=0;i<n;i++)
    {
       newnode = getnode();
if(start = NULL)
    {
       start = newnode;
     }
else
    {
       temp = start;
    while( temp->next != NULL)
    temp = temp->next;
    temp->next = newnode;
    }
  }
}
void main()
  {
    int n;
  if( Start = NULL)
    {
         printf("\n Enter Number of nodes to creat linked list:");
         scanf("%d",&n);
        creatlist(n);
     }
else
   printf("\n List is created");
}

  

Sub-sting in Main string

Aim:
To find sub-string in a given string.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int count=0,count2=0,i,j,flag;
   char str[80],search[20];
clrscr();
printf("\n Enter a string:");
gets(str);
printf("\n Enter a sub-string:");
gets(search);
  while( str[count] != '\0')
    count++;
  while( search[count2] != '\0')
    count2++;
for(i=0;i<=count1-count2;i++)
  {
    for(j=i;j<i+count2;j++)
      {
        flag =1;
        if( str[i] != search[j-i] )
          {
            flag = 0;
            break;
          }
      }
  if( flag == 1)
      break;
}
  if( flag == 1)
printf("\n SEARCH SUCCESSFUL");
  else
printf("\n SEARCH NOT SUCCESSFUL");
}

Palindrome ( without built-in functions )

Aim:
To check given string is Palindrome or not.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
  {
     char str[80],revstr[80];
     int i,length=0,flag=0;
     clrscr ();
printf("\n Enter a string:");
gets (str);
for( i=0;string[i] != '\0';i++)
   {
     length++;
   }
printf("\nLength of the string \'%s'\ is : %d",str,length);
for(i=length-1;i>=0;i--)
   {
     revstr[length-i-1] = str[i];
   }
for(i=0;i<length;i++)
  {
     if(revstr[i] == str[i])
           flag = 1;
     else
          flag = 0;
    }
   if ( flag == 1)
printf("\n%s is a palindrome",str);
   else
      printf("\n%s is not a palindrome",str);
}

Tuesday, 28 October 2014

String insertion at specified location

Aim:
To insert a substring into a string.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char str[80],substr[20];
   char X,newstr[80];
   int pos,count1=0,count2=0,
   int i=0,t=0,l;
   clrscr ();
printf("Enter a string:");
gets(str);
printf("Enter a substring:");
gets(substr);
while( str[i] != '\0' )
    {
       count1++;
       i++;
    }
while( substr[i] != '\0' )
    {
       count2++;
       i++;
    }
printf("\nEnter the position to insert the substring");
scanf("%d",&pos);
strcpy(newstr,str);
l = pos+count2;
for( i=pos;i<=count1+count2;i++)
  {
    X = newstr[i];
    if(t < count2)
     {
       str[i] = substr[t];
          t = t+1;
      }
      str[l] = X;
      l++;
   }
printf("\nNewstring is: %s",str);
}

Program:
Enter a string: hydai
Enter a substring: un
Enter the position to insert the substring: 3
Newstring is hyundai