Friday 30 December 2016

Reversing a string using linked list in C



Write a program to reverse the  given string using Single linked list.

#include <stdio.h>
#include <string.h>
struct node
{
    char data;
    struct node *next;
};
struct node *head=NULL,*c,*p,*r;
void create(char value)
{
    struct node * new = (struct node *)malloc(sizeof(struct node));
    new->data = value;
    new->next=NULL;
    if(head == NULL)
        head = new;
    else
    {
        c=head;
        while(c->next != NULL)
        {
            c=c->next;
        }
        c->next=new;
    }
}
display()
{
    if(head == NULL)
        printf("list is empty");
    else
        {
            c=head;
            while(c->next!=NULL)
            {
                printf("\n %c\t",c->data);
                c=c->next;
            }

                printf(" %c\n",c->data);
        }
}
main()
{
    char s1[50];
    int i;
    printf("enter a string");
    scanf("%s",s1);
    for(i=0;s1[i]!='\0';i++)
            create(s1[i]);
    c=head;p=NULL;
    while(c!=NULL)
    {
        r=p;
        p=c;
        c=c->next;
        p->next=r;
    }
    head=p;
    display();
}

Sorting data in linked list



Suppose the Personnel file of a small company contain the following data for all its employees SSN(social security number),Name and salary. A Linked list is used to store the Ask students to write a function to sort the records based on SSN?
 

#include <stdio.h>
#include <string.h>
struct node
{
    int ssn,sal;
    char name[50];
    struct node *next;
};
struct node *head=NULL,*c,*p;
void create()
{
    int value,s;
    char n[50];
    printf("enter SSN, name and sal");
    scanf("%d%s%d",&value,n, &s);
    struct node * new = (struct node *)malloc(sizeof(struct node));
    new->next=NULL;
    new->ssn=value;
    strcpy(new->name,n);
    new->sal=s;
    if(head == NULL)
        head = new;
    else
    {
        c=head;
        while(c->next != NULL)
        {
            c=c->next;
        }
        c->next=new;
    }
}
display()
{
    if(head == NULL)
        printf("list is empty");
    else
        {
            c=head;
            while(c->next!=NULL)
            {
                printf("\n%d\t%s\t%d\n",c->ssn,c->name,c->sal);
                c=c->next;
            }

                printf("\n%d\t%s\t%d\n",c->ssn,c->name,c->sal);
        }
}
main()
{
    int n,i,temp,j,t1,t2;
    char ntemp[50];
    printf("enter the number of records");
    scanf("%d",&n);
    for(i=0;i<n;i++)
            create();
        //    display();
    c=head;
    for(c=head;c->next != NULL;c=c->next)
    {
        for(p=c->next;p!=NULL;p=p->next)
        {
            if(c->ssn > p->ssn)
            {
                t1=c->ssn;t2=c->sal;strcpy(ntemp,c->name);
                c->ssn=p->ssn;c->sal=p->sal;strcpy(c->name,p->name);
                p->ssn=t1;p->sal=t2;strcpy(p->name,ntemp);
            }
           
        }
    }
    display();
}

Tuesday 27 December 2016

C program: Making a string palindrome

Develop a c code for the following function( using Pointer-function).
char Letter(char* [],n)
{
…………….
……………
…………….
}
Sample Input
4
abc
abcba
abcd
cba
Sample Output
2
0
4

2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void letter(char *s[], int n)
{
int i,l1,j,k,diff,c=0;
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
l1=strlen(s[i])-1;
for(j=0;j<l1;j++,l1--)
{
diff=*(*(s+i)+j)-*(*(s+i)+l1);
c=c+abs(diff);
*(*(s+i)+l1)=*(*(s+i)+l1)+diff;
}
printf("%d\n",c);
puts(s[i]);
}
}
main()
{
  int T,i;
  char *s[10],s1[10];
  printf("enter the number of test cases");
  scanf("%d",&T);
  for(i=0;i<T;i++)
  s[i]=(char *)malloc(sizeof(s1));
  letter(s,T);
}

Sunday 25 December 2016

C program: Number of matches won by different teams ordered by number of matches won

Consider a Structure Cricket with its members as Team 1,Team 2,Ground played,Result.Write a program to display the information in a tabular form in a Sorted order based on no of matches won by a teamEx: if matches are as follows struct Cricket match[6] ={{"IND","AUS","Pune",1}, {"PAK","IND","Nagpur",0}, {"AUS","PAK","HYD",1}, {"AUS","IND","CHE",0},{"AUS","PAK","MUM",1},{"PAK","AUS","BZA",1}};Output:
Team
No of matches won
INDIA
3
PAK
2
AUS
1

Program:

#include <stdio.h>
#include <string.h>
#define NM 6

struct Cricket
{
char team1[20];
char team2[20];
char ground[18];
int result;
};

struct Cricket match[NM];

struct result
{
int won;
char team[5];
}r[6];
int c=2;

linearsearch(char *s)
{
int j;
for(j=0;j<c;j++)
{
if(strcmpi(s,r[j].team )==0)
 return;
}
strcpy(r[c].team,s);
c++;
}

unique()   /* finds the number of unique teams participated in matches*/
{
int i,flag=0,j,n=0;
for(i=0;i<NM;i++)
{
printf("enter team1, team 2, ground played, result");
scanf("%s %s %s %d",match[i].team1,match[i].team2,match[i].ground,&match[i].result);
if(i==0)
{
strcpy(r[0].team ,  match[i].team1);
strcpy(r[1].team ,  match[i].team2);
}
else
{
linearsearch(match[i].team1);
linearsearch(match[i].team2);
}
}
}

int nowon(char *s)
{
int i,t1=0;
for(i=0;i<NM;i++)
{
if(strcmpi(s,match[i].team1)== 0 && match[i].result == 1)
{
t1++;
}
if(strcmpi(s,match[i].team2)== 0 && match[i].result == 0)
{
t1++;
}
}
return t1;
}
bubblesort()
{
struct result temp;
int i,j;
for(i=0;i<c-1;i++)
{
for(j=0;j<c-i-1;j++)
{
if(r[j].won <r[j+1].won)
{
temp=r[j];
r[j]=r[j+1];
r[j+1]=temp;
}
}
}
}
main()
{
int i;
unique();
for(i=0;i<c;i++)
r[i].won=nowon(r[i].team);
bubblesort();
for(i=0;i<c;i++)
printf("\n %d\t%s",r[i].won,r[i].team);

}

Wednesday 21 December 2016

Library program in which Book has multiple authors and Shelf has multiple books using Arrays of structures


Have the following structs, for Book, Author and Shelf. Book has multiple authors and Shelf has multiple books. Write a program to create a functions new_author (), new_book (), print_book (), add_author_to_book (), new_shelf (), delete_shelf (), print_shelf(), add_book_to_shelf() with required return types and parameters.


#include<stdio.h>
#include <stdlib.h>
#define NA 100
#define NB 100
#define NS 100
int ca=0,cb=0,cs=0;
struct author
{
    int aid;
    char name[20];
   
}a[NA];
struct book
{
    char title[25];
    int bid,faid[5],cab;

}b[NB];
struct shelf
{
    int sid,fbid[100],csb;
}s[NS];

void add_author()
{
    a[ca].aid=ca;
    printf("enter the name of author");
    scanf("%s",&a[ca].name);
    ca++;
}
void add_book()
{
    int temp;
    b[cb].bid=cb;
   
    printf("enter the title of the book ");
    scanf("%s",&b[cb].title);
    b[cb].cab=0;
    cb++;
}
void add_author_to_book()
{
    int t1,t2,c1;
    printf("enter the book id and author id");
    scanf("%d%d",&t1,&t2);
    c1=b[t1].cab;
    b[t1].faid[c1]=t2;
    b[t1].cab++;
}
print_author()
{
    int i;
    for(i=0;i<ca;i++)
    {
        printf("\n%d\t%s",a[i].aid,a[i].name);
    }
}
print_book()
{
    int i,j;
    for(i=0;i<cb;i++)
    {
        printf("\n%d\t%s",b[i].bid,b[i].title);
        for(j=0;j<b[i].cab;j++)
        {
            printf("\n author = %s",a[b[i].faid[j]].name);
        }
    }
}
void add_shelf()
{
    s[cs].sid=cs;
    s[cs].csb=0;
    cs++;  
}  
void add_book_to_shelf()
{
    int b1,s1;
    printf("enter shelf id and book id");
    scanf("%d%d",&s1,&b1);
    s[s1].fbid[s[s1].csb]=b1;
    s[s1].csb++;
  
}
void print_shelfid()
{   
    int i;
    for( i = 0 ; i < cs; i++ )
         printf("%d\n", s[i].sid);   
}
void print_books_in_shelf()
{
    int sid1,i;
    printf("enter shelf id");
    scanf("%d",&sid1);
    for(i=0;i<=s[sid1].csb;i++)
    {
        printf("\n%s",b[s[sid1].fbid[i]].title);
    }
}
void delete_shelf()
{
    int sid2,i;
    printf("enter shelf id");
    scanf("%d",&sid2);
    if ( sid2 >cs )
      printf("Deletion not possible.\n");
   else
   {
      for ( i = sid2 - 1 ; i < cs ; i++ )
         s[i] = s[i+1];
       cs--;
    }
}
main()
{
    int choice;
    printf("\n Press 1. add author\n2. print authors \n");
    printf("3. add book\n4. add author to book\n5. print book");
    printf(" 6. add shelf\n7. print shelf \n");
    printf("8. add book to shelf\n9. print books in shelf \n10. delete shelf\n11. exit\n");
    while(1)
    {
      printf("enter ur choice");
      scanf("%d",&choice);
      switch(choice)
      {
          case 1:    add_author();
                  break;
        case 2:    print_author();
                break;
        case 3:    add_book();
                break;
        case 4:    add_author_to_book();
                break;
        case 5: print_book();
                break;
        case 6: add_shelf();
                break;
        case 7: print_shelfid();
                break;
        case 8:    add_book_to_shelf();
                break;
        case 9:    print_books_in_shelf();
                break;
        case 10: delete_shelf();
                break;
        case 11: exit(0);
        default: printf("invalid choice");
    }
    }
}

Circular Array Loop detection C program


You are given an array of positive and negative integers. if a number n at an index is positive, then move forward n steps. Conversely, if its negative, move backwards n steps. Determine if there is a loop in this array. For Example given the array [2,-1,1,2,2] index 0 maps to index 2,1 maps to 0,2 maps to 3 and so on. There is a loop in this array because 0 maps to 2,2 maps to 3,3 maps to 0.(Use the modulo operator)
Solution 1:
#include<stdio.h>
    #include<stdlib.h>
    main()
    {
       
        int a[10],n,i,k,x,c;
        printf("enter the size of array");
        scanf("%d",&n);
        printf("enter the elements");
        for(i=0;i<n;i++)
scanf("%d",&a[i]);
        x=k=0;
        c=0;
        while(c<n) //we can have maximum of n jumps
        {
        //printf("\t%d\t",k);
  if(a[k]==0)
  {
printf("element value cannot be zero");
exit(0);
}
else if(a[k]>0)
            {
 k=(k+a[k])%n;
}
else if(a[k]<0)
            {
            k=(k+a[k])%n;
            if(k<0)
  k=n+k;
            }
            if(x==k)
            {
              printf("loop exists");
                exit(0);
}
            c++;
}
}

(OR)

Below this recursive implementation, you have simple code
#include<stdio.h>
#include<stdlib.h>
void fun(int ,int ,int );
int b[10][10];
main()
    {
       
        int a[10],n,i,k,x;
        printf("enter the size of array");
        scanf("%d",&n);
        printf("enter the elements");
        for(i=0;i<n;i++)
        scanf("%d",&a[i]);
        for(i=0;i<n;i++)
        {
            k=i;
            if(a[k]==0)
                  {
                    printf("element value cannot be zero");
                    exit(0);
                }
                else if(a[k]>0)
                {
                  k=(k+a[k])%n;
                  //printf("\n%d",k);
                }
                  else if(a[k]<0)
                {
                    k=(k+a[k])%n;
                    if(k<0) k=n+k;
                    //printf("\n%d",k);
                }
                b[i][0]=i;
                b[i][1]=k;
              
        }
              
        k=0;
        for(i=0;i<n;i++)
    {
         x=b[i][0];
         fun(i,k,x);
    }
}
void fun(int i,int k,int x)
{
        for(k=k+1; k<5;k++)
        {
                if(b[i][1]==b[k][0])
                {
                        if(x==b[k][1])
                        {
                                printf("loop exists");
                                exit(0);
                        }
                        else
                         fun(k,1,x);
                }
        }
}

(OR)


#include<stdio.h>
#include<stdlib.h>
main()
{

    int a[10],n,i,k,x,j,c,t;
    printf("enter the size of array");
    scanf("%d",&n);
    printf("enter the elements");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        x=i;k=i;
          c=0;
          while(c<n) //Atmost we can have maximum of n jumps
          {
            if(a[k]==0) 
            {
                printf("element value cannot be zero");
                exit(0);
            }
            else if(a[k]>0)
            {
              k=(k+a[k])%n;
            }
            else if(a[k]<0)
            {
                k=(k+a[k])%n; //if k+a[k] is negative then (k+a[k])%n is also negative
                if(k<0) k=n+k;
/* if the index k is negative we have to loop back to last element in the array.
This can be achieved by doing subtracting from n and 
since k is negative, n+k will be do the subtraction from n*/
             }
            if(x==k)
            {
                printf("loop exists");
                exit(0);
            }

            c++;
        }
    }
}