Batch - B Practical Test Question

Batch - B Practical Test Question
Batch - B Practical Test Question

Palindrome using Linked List

//Developed By : HIT KUMAR BHALODIA  ICT Sem-3 (Marwadi University)

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

struct Element
{
 char ch;
 struct Element *next;
};

struct Element *front=NULL,*temp=NULL,*rear=NULL;

void InsertCharacter(char);
void Display_String();
void chek_palindrome();
void delete_FromLast();
void delete_FromFirst();

int main()
{
    char ch;
    int len_Str;

    printf("\nEnter Length of he Linked List Character : ");
    scanf("%d",&len_Str);

    while(len_Str>0)
    {
        printf("\nEnter Character : ");
        fflush(stdin);
        scanf("%c",&ch);
        InsertCharacter(ch);
        len_Str--;
    }
    Display_String();
    chek_palindrome();
    return 0;
}

void InsertCharacter(char ch)
{
 temp = (struct Element *)malloc(sizeof( struct Element)*1);
 temp->ch=ch;  //here write & cuz we want address
 temp -> next = NULL;
 if(front==NULL)
    {
     front = temp;
     rear = temp;
    }
    else
    {
      rear->next = temp;
      rear= rear->next;
    }
}
void Display_String()
{
    temp = front;

        printf("\nString  Is : ");
        while(temp !=NULL )
        {
            printf(" %c ",temp-> ch);
            temp = temp -> next;
        }

    printf("\n\n\n");
}
void chek_palindrome()
{
      if(front==NULL&&rear==NULL)
     {
          printf ("!!!Pls Enter String!!!!");
     }

    if(front->ch==rear->ch&&front!=rear&&front->next!=rear)
    {
        delete_FromFirst();
        delete_FromLast();
        chek_palindrome();
    }
    else if(front->ch==rear->ch&&front->next!=rear&&front->next!=NULL)
    {
        delete_FromFirst();
        delete_FromLast();
        chek_palindrome();
    }
    else if(front==rear)
    {
        printf("\n!!!Palindrome!!");
    }
    else if(front->next==rear&&front->ch==rear->ch)
    {
        printf("!!Palindrome!!");
    }
    else
    {
        printf("!!Not Palindrome!!");
    }
}
void delete_FromLast()
{
 if(front==NULL)
 {
     printf("Queue UnderFlow!!");
 }
 else if(front->next==NULL)
 {
     temp=front;
     free(temp);
     front=rear=NULL;

 }
 else
 {
     temp=front;
     while(temp->next!=rear)
     {
         temp=temp->next;
     }

     rear=temp;
     temp=temp->next;
     rear->next=NULL;
     free(temp);
 }
}

void delete_FromFirst()
{
if(front==NULL)
  {
      printf("\nUnderFlow !!!");
  }
  else
  {
    temp=front;
    front = front->next;
    free(temp);
  }
}

Comments