Friday 26 September 2014

Check string ,if palindrome using Queue

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#define que_size 10
int front=-1;
int rear=-1;
char q[que_size];
void insert(char c)
{
         if(rear==que_size-1)
    {
         printf("\n\nOverflow Occurs!!");
         getch();
         return;
     }
          if(front==-1)
          front=front+1;
          rear =rear+1;
          q[rear]=c;
}
void display()
{
          int i;
          if(front==-1)
     {
          printf("\n\nQueue is empty !!");
     }
         else
     {
         printf("\n\nString is : ");
         for(i=front;i<=rear;i++)
      {
         printf("%c",q[i]);
      }
    }
}
void check()
{
         int f;
         while(front!=rear)
    {
         if(q[front]==q[rear])
      {
         f=1;
         front++;
         rear--;
      }
        else
     {
        f=0;
        break;
     }
  }
        if(f==1)
        printf("\nString is a palindrome !!");
        else
        printf("\nString is not a palindrome!!");
}
void main()
{
       int i;
       char s[10];
        clrscr();
        printf("\nEnter string : ");
        gets(s);
        for(i=0;i<=strlen(s)-1;i++)
    {
        insert(s[i]);
    }
        if(rear!=que_size-1)
   {
       display();
       check();
   }
      getch();
}

Output:






Monday 22 September 2014

Deleting alternate nodes of linked list

//Program to delete alternate nodes of linked list

#include<stdio.h>
#include<conio.h>
typedef struct node node;
struct node{
                     int data;
                     node *link;
                   };
node *start=NULL;
void insert()
{
        node *ptr=(node*)malloc(sizeof(node));
        int item;
        printf("Enter data : ");
        scanf("%d",&ptr->data);
        ptr->link=start;
        start=ptr;
}
void display()
{
        node *ptr=start;
        printf("\nList is : ");
        while(ptr!=NULL)
   {
        printf("%d  ",ptr->data);
        ptr=ptr->link;
   }
}
void alternate()
{
        node *temp,*ptr,*a,*p;
        temp=start;
        ptr=start->link;
        while(temp!=NULL)
    {
        temp->link=ptr->link;
        a=ptr;
        ptr=ptr->link->link;
        temp=temp->link;
        free(a);
    }
        p=start;
        printf("\nAlternate nodes are : ");
        while(p!=NULL)
   {
       printf("%d  ",p->data);
       p=p->link;
   }
}
void main()
{
      int n,i;
      clrscr();
      printf("\nEnter number of nodes in list : ");
      scanf("%d",&n);
      for(i=0;i<n;i++)
   {
      insert();
   }
      display();
      alternate();
      getch();
}

Output:



Print Middle element of linked list


 //Program to print middle element of linked list without using integer variables. 

#include<stdio.h>
#include<conio.h>
typedef struct node node;
struct node{
                     int data;
                     node *link;
                   };
node *start=NULL;
void insert()
{
       node *ptr=(node*)malloc(sizeof(node));
       int item;
       printf("Enter data : ");
       scanf("%d",&ptr->data);
       ptr->link=start;
       start=ptr;
}
void display()
{
       node *ptr=start;
       printf("\nList is : ");
       while(ptr!=NULL)
   {
       printf("%d  ",ptr->data);
       ptr=ptr->link;
   }
}
void print_middle()
{
       node *ptr,*temp;
       ptr=start;
       temp=start->link;
       while(temp->link!=NULL)
   {
       ptr=ptr->link;
       temp=temp->link->link;
   }
       printf("\nMiddle element is : %d",ptr->data);
}
void main()
{
       int n,i;
       clrscr();
       printf("\nEnter number of nodes in list : ");
       scanf("%d",&n);
       for(i=0;i<n;i++)
   {
       insert();
   }
       display();
       print_middle();
       getch();
}


 Output: