Data Structure

Q-1) To find 2nd max value in linear single linked list
#include<stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>


struct node
 { 
   int data;
   struct node*next;
  };
  struct node*lsll=NULL;
  struct node* create(int);
  int smax(struct node*);


void main()
  {
   int max,no;
   struct node*nodeptr=NULL;
   clrscr();
   printf("\t\t\t\t ---------------\n");
   printf("\t\t\t\tLogic by Kashid\n");
   printf("\t\t\t\t---------------\n");
   printf("How many nodes\n");
   scanf("%d",&no);
   nodeptr=create(no);
   max=smax(nodeptr);
   if(max=='n')
    printf("Second max not present in lsll");
  else
   printf("second max=%d",max);
  getch();
 }


 struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {newnode=(struct node*)malloc(sizeof(struct node));
  if (newnode==NULL)
  exit(1);
  printf("Enter value of %d node\n",i);
  scanf("%d",&newnode->data);
  newnode->next=NULL;
  if(temp==NULL)
   temp=first=newnode;
   else
   {
    temp->next=newnode;
    temp=newnode;
   }
  }
  return(first);
}


int smax(struct node*lsll)
{
 int smax,max;
 struct node *temp;
 temp=(struct node*)malloc(sizeof(struct node));
  if (temp==NULL)
    exit(1);
  temp=lsll;
  smax=max=temp->data;
  while(temp!=NULL)
  {
   if(temp->data>max)
    {smax=max;
    max=temp->data;
   temp=temp->next;
    }
   else if(temp->data>smax)
{ smax=temp->data;
temp=temp->next;
}
    else if((temp->data<smax) &&(smax==max))
       {  smax=temp->data;
temp=temp->next;
}


   else temp=temp->next;
   }
  if (smax==max)
   return('n');
   else
    return(smax);
 }



Q-2) To find 2nd min value in linear single linked list


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node
{ int data;
  struct node*next;
  };
  struct node*lsll=NULL;
struct node* create(int);
int smin(struct node*);
void main()
{
 int min2,no;
 struct node*nodeptr=NULL;
 clrscr();

   printf("\t\t\t\t ---------------\n");
   printf("\t\t\t\tLogic by Kashid\n");
   printf("\t\t\t\t---------------\n");

 printf("How many nodes\n");
 scanf("%d",&no);
 nodeptr=create(no);
 min2=smin(nodeptr);
 if(min2=='n')
 printf("Second min not present in lsll");
 else
 printf("second min=%d",min2);
 getch();
 }
 struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {newnode=(struct node*)malloc(sizeof(struct node));
  if (newnode==NULL)
  exit(1);
  printf("Enter value of %d node\n",i);
 scanf("%d",&newnode->data);
 newnode->next=NULL;
 if(temp==NULL)
   temp=first=newnode;
   else
  {   temp->next=newnode;
      temp=newnode;
   }
  }
  return(first);
}


int smin(struct node*lsll)
{
 int smin,min;
 struct node *temp;
 temp=(struct node*)malloc(sizeof(struct node));
  if (temp==NULL)
    exit(1);
  temp=lsll;
  smin=min=temp->data;
  while(temp!=NULL)
  {
   if(temp->data<min)
    {smin=min;
    min=temp->data;
   temp=temp->next;
    }
   else if(temp->data<smin)
{ smin=temp->data;
temp=temp->next;
}
  else if((temp->data>smin) &&(smin==min))
   { smin=temp->data;
temp=temp->next;
   }
   else temp=temp->next;
   }
  if (smin==min)
   return('n');
   else
    return(smin);
 }


Q3)To check information stored in ldll(linear double linked list) is palindome or not


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node
{ int data;
  struct node*left;
  struct node*right;
  };
  struct node*ldll=NULL;
struct node* create(int);
int ispalindrome(struct node*);
void main()
{
 int reply,no;
 struct node*nodeptr=NULL;
 clrscr();

 printf("\t\t\t\t ---------------\n");
 printf("\t\t\t\tLogic by Kashid\n");
 printf("\t\t\t\t---------------\n");

 printf("How many nodes\n");
 scanf("%d",&no);
 nodeptr=create(no);
 reply=ispalindrome(nodeptr);
 if(reply=='n')
 printf("ldll is not palindrome");
 else
 printf("ldll is palindrome");
 getch();
 }
 struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {newnode=(struct node*)malloc(sizeof(struct node));
  if (newnode==NULL)
  exit(1);
  printf("Please Enter integer value of %d node\n",i);
 scanf("%d",&newnode->data);
 newnode->left=NULL;
 newnode->right=NULL;
 if(temp==NULL)
   temp=first=newnode;
   else
  {   temp->right=newnode;
      newnode->left=temp;
      temp=newnode;
   }
  }
  return(first);
}


int ispalindrome(struct node*ldll)
{
struct node*ltemp,*rtemp;
ltemp=(struct node*)malloc(sizeof(struct node));
rtemp=(struct node*)malloc(sizeof(struct node));
if(rtemp==NULL||ltemp==NULL)
exit(1);
ltemp=rtemp=ldll;
while(rtemp->right!=NULL)
{
 rtemp=rtemp->right;
 }




for(;ltemp->left!=rtemp;ltemp=ltemp->right,rtemp=rtemp->le


ft)
 {
   if(ltemp->data!=rtemp->data)
   break;
 }
 if(ltemp->left==rtemp)
 return('y') ;
 else
 return('n');
 }


Q4)To swap(interchange) value in linear single linked list(node 1 & 2,node 3 &4 etc..)


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node

   int data;
  struct node*next;
  };
  struct node*lsll=NULL;
struct node* create(int);
void swap(struct node*);
void print(struct node*);


void main()
{
 int no;
 struct node*nodeptr=NULL;
 clrscr();
 printf("\t\t\t\t---------------\n");
 printf("\t\t\t\tLOGIC BY KASHID\n");


 printf("\t\t\t\t---------------\n");
 printf("How many nodes do you want\n\n\n");
 scanf("%d",&no);
 nodeptr=create(no);
 printf("\n\nLSLL BEFORE SWAPPING....\n");
 printf("------------------------\n");
 print(nodeptr);
 swap(nodeptr);
 printf("\n\nLSLL AFTER SWAPPING....\n");
 printf("------------------------\n");
 print(nodeptr);
 getch();
 }


void print(struct node*first)
 {
 struct node*temp;
 int i=1;
 temp=first;
 while(temp!=NULL)
     {
     printf("NODE %d\t value = %d\n",i++,temp->data);
     temp=temp->next;
     }
  }
struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {newnode=(struct node*)malloc(sizeof(struct node));
  if (newnode==NULL)
  exit(1);
  printf("Enter value of %d node\n",i);
  printf("\n");
 scanf("%d",&newnode->data);
 printf("\n");
 newnode->next=NULL;
 if(temp==NULL)
   temp=first=newnode;
   else
  {   temp->next=newnode;
      temp=newnode;
   }
  }
  return(first);
}
void swap(struct node*first)
{
 struct node*ftemp,*stemp;
 int tdata;
 ftemp=(struct node*)malloc(sizeof(struct node));
 if (ftemp==NULL)
 exit(1);
 stemp=(struct node*)malloc(sizeof(struct node));
 if (stemp==NULL)
 exit(1);
  ftemp=first;
  stemp=ftemp->next;


 while(stemp!=NULL)
 {tdata=stemp->data;
 stemp->data=ftemp->data;
 ftemp->data=tdata;
 ftemp=stemp->next;
 stemp=ftemp->next;
 }
}


Q5)To find Distinct range of values stored in linear single linked list


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node
{ int data;
  struct node*next;
  };
  struct node*lsll=NULL;
struct node* create(int);
void print(struct node*);
void range(struct node*);
void main()
{
int nodes;
 struct node*nodeptr=NULL;
 clrscr();

printf("\t\t\t\t ---------------\n");
printf("\t\t\t\tLogic by Kashid\n");
printf("\t\t\t\t---------------\n");

 printf("How many nodes do you want in your lsll\n");
 scanf("%d",&nodes);
 nodeptr=create(nodes);
 print(nodeptr);


 range(nodeptr);
 getch();
 }
 struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {newnode=(struct node*)malloc(sizeof(struct node));
  if (newnode==NULL)
  exit(1);
  printf("Enter value of %d node\n",i);
 scanf("%d",&newnode->data);
 newnode->next=NULL;
 if(temp==NULL)
   temp=first=newnode;
   else
  {   temp->next=newnode;
      temp=newnode;
   }
  }
  return(first);
}


void range(struct node*lsll)
{
 int minval,maxval;
 struct node *temp;
 temp=(struct node*)malloc(sizeof(struct node));
  if (temp==NULL)
    exit(1);
  temp=lsll;
  maxval=minval=temp->data;
  while(temp!=NULL)
  {
   if(temp->data>maxval)
    {maxval=temp->data;
   temp=temp->next;
    }
   else if(temp->data<minval)
{ minval=temp->data;
temp=temp->next;
}
    else temp=temp->next;
   }
 if(minval==maxval)
 printf("Both minimum & maximum value are same=


%d",minval);
 else
    {
      printf("Range of values in lsll\n");
      printf("-----------------------\n");
      printf("minimum value   maximum value\n\n");
      printf("\t%d\t%d",minval,maxval);
    }
}


void print(struct node *first)
{struct node*temp;
int i=1;
temp=first;
printf("PRINTING NODES & VALUES OF LSLL\n");
printf(" nodes\t values\n");
printf(" --------------\n");


while(temp!=NULL)
        { printf(" %d\t %d\n",i++,temp->data);
         temp=temp->next;
         }
}


Q6) A void to convert array of n integers into linear double linked list


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node
{ int data;
  struct node*left;
  struct node*right;
  };
  struct node*ldll=NULL;
struct node* arratoldll(int*,int);
void print(struct node*);
void main()
{
 int no,i,a[80];
 struct node*nodeptr=NULL;
 clrscr();
 printf("A Program by Kashid\n");
 printf("-------------------\n\n\n\n\n");
 printf("How many elements\n");
 scanf("%d",&no);


 for(i=0;i<no;i++)
 {
  printf("Enter %d  element of array\n",i+1);
  scanf("%d",&a[i]);
  }
  printf("\n\n\n Printing Array Elements from array....\n");
  printf(" --------------------------------------\n");
  for(i=0;i<no;i++)
  printf("%d element of array = %d\n",i+1,a[i]);
  nodeptr=arratoldll(a,no);
  print(nodeptr);
  getch();
  }


 struct node* arratoldll(int *a,int n)
 {int i=0;
 struct node*first=NULL,*newnode,*temp=NULL;


for(;i<n;i++)
{newnode=(struct node*)malloc(sizeof(struct node));
 if(newnode==NULL)
 exit(1);
 newnode->data=*(a+i);
 newnode->left=NULL;
 newnode->right=NULL;
 if(temp==NULL)
  first=temp=newnode;
  else
  {
   temp->right=newnode;
   newnode->left=temp;
   temp=newnode;
   }
 }
 return(first);
}
void print(struct node*ldll)
{
 struct node*temp;
 int cntnode=1;
 temp=ldll;
 printf("\n\n\n\nPrinting array elements from LDLL...\n");
 printf("------------------------------------\n");


 while(temp!=NULL)
 { printf("node %d\t value = %d\n",cntnode++,temp->data);
   temp=temp->right;
   }
}


Q8)To find Sum total of values stored in Odd numbered nodes in lsll of integers


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node
{ int data;
  struct node*next;
  };
  struct node*lsll=NULL;
struct node* create(int);
int sum(struct node*);
void print(struct node*);
void main()
{
 int sumofnodes,no;
 struct node*nodeptr=NULL;
 clrscr();
 printf("\t\t\t\t ---------------\n");
 printf("\t\t\t\tLogic by Kashid\n");
 printf("\t\t\t\t---------------\n");
 printf("\n\nHow many nodes do you want in your lsll\n");
 scanf("%d",&no);
 nodeptr=create(no);
 print(nodeptr);
sumofnodes=sum(nodeptr);
 printf("\n\nSum total of odd nodes in lsll\n");
 printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t%d",sumofnodes);
 getch();
 }
void print(struct node*lsll)
 {
 struct node*temp;
 int node=1;
 temp=lsll;
 if(temp==NULL)
 exit(1);
  printf("\n\n\nLSLL IS PRINTED AS..\n");
  printf("\nNODES\tVALUES\n");
  while(temp!=NULL)
    {
      printf("%d\t%d\n",node++,temp->data);
     temp=temp->next;
    }
 }


 struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {
    newnode=(struct node*)malloc(sizeof(struct node));
   if (newnode==NULL)
   exit(1);
   printf("Enter value of %d node\n",i);
   scanf("%d",&newnode->data);
   newnode->next=NULL;
   if(temp==NULL)
    temp=first=newnode;
    else
       {     temp->next=newnode;
             temp=newnode;
       }
  }
  return(first);
}
int sum(struct node*lsll)
{
 int odsum=0,node=1;
 struct node *temp;
   temp=lsll;
   if(temp==NULL)
  {
   printf("NULL LSLL\n");
   return(0);
   }
   for(;temp!=NULL;temp=temp->next,node++)
    if(node%2!=0)
    odsum+=temp->data;


      return(odsum);
 }


Q11)to find Sum total of values stored in Even numbered nodes in lsll of integers


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node
{ int data;
  struct node*next;
  };
  struct node*lsll=NULL;
struct node* create(int);
int sum(struct node*);
void print(struct node*);
void main()
{
 int sumofnodes,no;
 struct node*nodeptr=NULL;
 clrscr();
 printf("\t\t\t\t ---------------\n");
 printf("\t\t\t\tLogic by Kashid\n");
 printf("\t\t\t\t---------------\n");
 printf("\n\nHow many nodes do you want in your lsll\n");
 scanf("%d",&no);
 nodeptr=create(no);
 print(nodeptr);
sumofnodes=sum(nodeptr);
 printf("\n\nSum total of Even nodes in lsll\n");
 printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t%d",sumofnodes);
 getch();
 }
int sum(struct node*lsll)
{
 int evensum=0,node=1;
 struct node *temp;
   temp=lsll;
   if(temp==NULL)
  {
   printf("NULL LSLL\n");
   return(0);
   }
   for(;temp!=NULL;temp=temp->next,node++)
    if(node%2==0)
    evensum+=temp->data;
     return(evensum);












struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {
       newnode=(struct node*)malloc(sizeof(struct node));
       if (newnode==NULL)
         exit(1);
       printf("Enter value of %d node\n",i);
       scanf("%d",&newnode->data);
       newnode->next=NULL;
 if(temp==NULL)
       temp=first=newnode;
 else
         {   temp->next=newnode;
              temp=newnode;
         }
  }
  return(first);
}


 void print(struct node*lsll)
 {
 struct node*temp;
 int node=1;
 temp=lsll;
 if(temp==NULL)
 exit(1);
  printf("\n\n\nLSLL IS PRINTED AS..\n");
  printf("\nNODES\tVALUES\n");
  while(temp!=NULL)
         {
            printf("%d\t%d\n",node++,temp->data);
            temp=temp->next;
         }
 }


Q9)To left shift ldll of characters


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
#include<kashid.h>
struct node
{char data;
struct node*left,*right;
}
  struct node*ldll=NULL;
struct node* create(int);
void leftshift(struct node*,int);
void print(struct node*,int);
void main()
{
 int node,shift=0;
 struct node*nodeptr=NULL;
 clrscr();
 printf(“\t\t\t\t--------------------------\n”);
 printf(“\t\t\t\tLOGIC BY KASHID\n”);
 printf(“\t\t\t\t--------------------------\n”);
 printf("\n\nHOW MANY NODES YOU WANT IN LDLL\n");
 scanf("%d",&node);
 nodeptr=create(node);
 printf("\n\n\n\n");
 print(nodeptr,shift);
 printf("\n\nHOW MANY LEFT SHIFT?\n\n");
 scanf("%d",&shift);
 printf("\n\n");
 leftshift(nodeptr,shift);
 getch();
 }
 struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  { newnode=(struct node*)malloc(sizeof(struct node));
     printf("\n\nEnter value of %d node\n\n",i);
     fflush(stdin);
     scanf("%c",&newnode->data);
     newnode->left=NULL;
      newnode->right=NULL;
   if(temp==NULL)
        temp=first=newnode;
   else
     {   temp->right=newnode;
          newnode->left=temp;
         temp=newnode;
      }
    }
  return(first);
}
void leftshift(struct node*ldll,int count)
{
 struct node*first,*last,*temp;
 int shift=1;
 last=first=ldll;
 while(last->right!=NULL)
  {
   last=last->right;
  }
  while(!(shift>count))
  {
   last->right=first;
   first->left=last;
   temp=first->right;
   temp->left=NULL;
   first->right=NULL;
   last=first;
   first=temp;
   print(first,shift);
   shift++;
   }
}


 void print(struct node*lsll,int shift)
 {
 struct node*temp;
 temp=lsll;
 if(temp==NULL)
 exit(1);


   while(temp!=NULL)
    {
     printf("%c\t",temp->data);
     temp=temp->right;
    }
    if(shift==0)
    printf("-->ORIGINAL LDLL BEFORE SHIFT\n\n");
    else
    printf("---->LDLL AFTER %d LEFT SHIFT\n\n",shift);
 }


Q10)To right shift ldll of characters


#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<stdlib.h>
struct node
{ char data;
  struct node*left,*right;
  };
  struct node*ldll=NULL;
struct node* create(int);
void rightshift(struct node*,int);
void print(struct node*,int);
void main()
{
 int node,shift=0;
 struct node*nodeptr=NULL;
 clrscr();
 printf("\t\t\t\t---------------\n");
 printf("\t\t\t\tLOGIC BY KASHID\n");
 printf("\t\t\t\t---------------\n");
 printf("\n\nHOW MANY NODES YOU WANT IN LDLL\n");
 scanf("%d",&node);
 nodeptr=create(node);
 printf("\n\n\n\n");
 print(nodeptr,shift);
 printf("\n\nHOW MANY RIGHT SHIFT?\n\n");
 scanf("%d",&shift);
 printf("\n\n");
 rightshift(nodeptr,shift);
 getch();
 }
void print(struct node*lsll,int shift)
 {
 struct node*temp;
 temp=lsll;
 if(temp==NULL)
 exit(1);


   while(temp!=NULL)
    {
     printf("%c\t",temp->data);
     temp=temp->right;
    }
    if(shift==0)
    printf("-->ORIGINAL LDLL BEFORE SHIFT\n\n");
    else
    printf("--->LDLL AFTER %d RIGHT SHIFT\n\n",shift);
 }




struct node* create(int no)
 {
  struct node*temp=NULL,*first=NULL,*newnode;
  int i;
  for(i=1;i<=no;i++)
  {
  newnode=(struct node*)malloc(sizeof(struct node));
  printf("\n\nEnter value of %d node\n\n",i);
  fflush(stdin);
  scanf("%c",&newnode->data);


  newnode->left=NULL;
 newnode->right=NULL;
 if(temp==NULL)
   temp=first=newnode;
   else
  {   temp->right=newnode;
      newnode->left=temp;
      temp=newnode;
   }
  }
  return(first);
}


void rightshift(struct node*ldll,int count)
{
 struct node*first,*last,*temp;
 int shift=1;
 last=first=ldll;
 while(last->right!=NULL)
  {
   last=last->right;
  }
  while(!(shift>count))
  {
   first->left=last;
   last->right=first;
   first=last;
   last=last->left;
   last->right=NULL;
   first->left=NULL;
   print(first,shift);
   shift++;
   }
}
Note: All the programs displayed here are developed based on my own Logic in C-prog.
Have a Question ? Need Help in College Assignments, Need Code that is not available here? Just leave a comment & get your code instantly.

Tips to Enhance Your Blog