Showing posts with label Linked List. Show all posts
Showing posts with label Linked List. Show all posts

Friday, October 18, 2013

List of Linked list programs.

1. A program for reversing each k-nodes of blocks in the program   Click here to see the code.

2. program for Palindrome using Singly LinkedList   Click here to see the code.  Click here to see the code.

3.An efficient way to write a program for checking whether it is palindrome or not  Click here to see the code.

4.Merge k sorted linked lists with total of N elements and return it as one sorted list Click here to see the code

5.Addition of two number's using singly linked list  Click here to see the code

Quicksort using singly linked list  Click here to see the code

7. Circular linked list: A (corrupt) linked list in which a node's next pointer points 
to an earlier node, so as to make a loop in the linked list Click here to see the code                                                       

Tuesday, June 25, 2013

A program for reversing each k-nodes of blocks in the program.....Tested and working

//A program for reversing each k-nodes of blocks in the program.....Tested and working
input:- 1 2 3 4 5 6 7 8 9 10 11
for k=3
output:- 3 2 1 6 5 4 9 8 7 10 11

#include<stdio.h>
#include<stdlib.h>
typedef struct Linkedlist
{
int data;
struct Linkedlist *next;
} List;

void InsertNode(List **Head,int data)
{
 List *temp,*ptr;
 temp=(List*)malloc(sizeof(List));
 temp->data=data;
 temp->next=NULL;
 if(!*Head)
  {
  *Head=temp;
  return ;
  }
  ptr=*Head;
  while(ptr->next)
  ptr=ptr->next;
  ptr->next=temp;
}
void Traverse(List *Head)
{
if(!Head)return;
printf("%d ",Head->data);
Traverse(Head->next);
}
void Kreverse(List **Head,int k)
{
List *prev,*First,*Last,*ptr,*temp;int i=1;
    First=ptr=*Head;
i=k-1;
if(k==1||!*Head)return ;
while(1)
{
  while(ptr->next&&i)
  {
  ptr=ptr->next;
  i--;
  }

  if(i)
  return;
  if(*Head==First)
  *Head=ptr;
  else
  {
   First->next=ptr;
        First=Last;
  }
  prev=Last=ptr->next;
  ptr=First;

  i=k-1;
  while(i)
  {
  temp=ptr;
  ptr=ptr->next;
  temp->next=prev;
  prev=temp;
  i--;
  }
  ptr->next=prev;
  ptr=Last;
if(!ptr)return;
i=k-1;
}
}
int main(void)
{
List *Head=NULL;
int i=0,data,n;
printf("Enter the no. of input you want to enter\n");
scanf("%d",&n);
printf("\nEnter the data\n");
while(i<n)
{
scanf("%d",&data);
InsertNode(&Head,data);
i++;
fflush(stdin);
}
printf("List is\n");
Traverse(Head);
printf("\nEnter the value for k reverse\n");
int k;
scanf("%d",&k);
printf("\nReversed list is\n");
Kreverse(&Head,k);
Traverse(Head);
return 0;
}

Sunday, June 23, 2013

Working program for Palindrome finding using Singly Linkedlist


// Working program for Palindrome using Singly LinkedList
#include <stdio.h>
#include <stdlib.h>
typedef struct Linkedlist
{
    int data;
    struct Linkedlist *next;
}List;

void Insertnode(List **head,int data)
{
List *ptr=*head,*temp;
temp=(List*)malloc(sizeof(List));
if(!temp)return;
temp->data=data;
temp->next=NULL;
if(!ptr)
*head=temp;
else
{
 while(ptr->next)
  ptr=ptr->next;
 ptr->next=temp;
 }
}

List *Partition(List **Head)
{
    if(!*Head) return;
    List *temp,*sptr=*Head,*head=*Head;
    while(head)
    {
    head=head->next;
    if(!head){temp->next=NULL; return sptr;}
 
    head=head->next;
    temp=sptr;
    sptr=sptr->next;
 
    }
temp->next=NULL;
return sptr;
}
List *Reverse(List *head)
{
List *temp,*prev=NULL,*ptr=head;
while(ptr->next)
{   temp=ptr;
    ptr=ptr->next;
    temp->next=prev;
    prev=temp;
}
ptr->next=prev;
return ptr;
}

void Matching(List *head,List *tail)
{
    if(!head||!tail)
    return;
    if(head->data==tail->data||head->data+32==tail->data)
    {head=head->next;
     tail=tail->next;
    }
    else
    {printf("\nNot a palindrome");return ;}
 
    while(head->data==tail->data)
   {
       head=head->next;
       if(head==NULL) {printf("\nPalindrome\n");return;}
       tail=tail->next;
   
   }
   printf("\nNot a palindrome\n");
}

void Palindrome(List *head)
{  List *tail=NULL;
    if(!head)return ;
    tail=Partition(&head);
    tail=Reverse(tail);
    Matching(head,tail);
}


main()
{
 List *Head=NULL;
 int i=0,data;

 while(i<7)                  // You may change if no. of character increases as per.
 {
 scanf("%c",&data);
 Insertnode(&Head,data);
 i++;
 }
Palindrome(Head);
}