The Programming Project

Sunday, March 10, 2013

C Program #14


/* Given k list of integers M1, M2, M3,.........., Mk. Write a program to produce one complete sorted list of integers combining these k list */
#include<stdio.h>
#include<malloc.h>
struct list
                        {
                        int item;
                        struct list *next;
                        };
typedef struct list node;
node* bsort(node*,int);
int main()
{
node *root[20],*p,*q;
int *list_size,K,i,j,SIZE=0;
printf("\n Enter the number of lists to be formed:");
scanf("%d",&K);
list_size=(int *)malloc(K*sizeof(int));
for(i=1;i<=K;i++)
            {
            printf("\n Enter the size of list %d# ",i);
            scanf("%d",(list_size+i));
            SIZE+=*(list_size+i);
            root[i]=(node *)malloc(sizeof(node));
            q=root[i];
            printf("\n Enter the elements of list %d# ",i);
            scanf("%d",&q->item);
            for(j=1;j<*(list_size+i);j++)
                        {
                        q->next=(node *)malloc(sizeof(node));
                        q=q->next;
                        scanf("%d",&q->item);
      }
            q->next=NULL;
   printf("\n");
            }
for(i=1;i<=K;i++)
            {
            q=root[i];
            printf("\n Elements of List M[%d]: ",i);
                        while(q!=NULL)
                                    {printf("%d ",q->item);
                                                q=q->next;
                                    }
            printf("\n");
            }
for(i=1;i<=K;i++)
            root[i]=bsort(root[i],*(list_size+i));
for(i=1;i<=K;i++)
            {
            q=root[i];
            printf("\n Elements of List M[%d] after sorting: ",i);
                        while(q!=NULL)
                                    {printf("%d ",q->item);
                                                q=q->next;
                                    }
            printf("\n");
            }
q=root[1];
for(i=1;i<K;i++)
            {
            while(root[i]->next!=NULL)
                                    root[i]=root[i]->next;
                        root[i]->next=root[i+1];
                        root[i]=root[i]->next;
            }
q=bsort(q,SIZE);
printf("\n Final List after merging:\n");
while(q!=NULL)
                        {
                        printf(" %d ",q->item);
                        q=q->next;
                        }

return 0;
}
node* bsort(node *head,int size)
            {
            int i,j,hold;
            node *tmp;
            tmp=head;
            for(i=0;i<size-1;i++)
                        {
                        for(j=0;j<size-i-1;j++)
                                    {
                                    if(tmp->item<tmp->next->item)
                                                {
                                                hold=tmp->item;
                                                tmp->item=tmp->next->item;
                                                tmp->next->item=hold;
                                                }
                                    tmp=tmp->next;
                                    }
                        tmp=head;
                        }
            return(head);
            }

C Program #13


/* Given a large linked list, L of integers such that L=(L0,L1,L2,..............,Ln). Write a program to the program into K list M1, M2, M3,.........., Mk such that
M1= L0,L0+k,L0+2k,..............
M1= L1,L1+k,L1+2k,..............
And so on */

#include<stdio.h>
#include<malloc.h>
struct list
                        {
                        int item;
                        struct list *next;
                        };
typedef struct list node;
int element(node *,int);
int main()
{
node *root[20],*p,*q;
int list_size=0,K,i,j;
root[0]=(node *)malloc(sizeof(node));
p=root[0];
printf("\n Enter the elements of the main list(enter -999 to exit):");
scanf("%d",&root[0]->item);
            while(1)
            {
             if(p->item==-999)
                        break;
             p->next=(node *)malloc(sizeof(node));
             p=p->next;
             scanf("%d",&p->item);
            }
p->next=NULL;
printf("\n Enter the elements of the list:");
p=root[0];
while(p->next!=NULL)
            {
            printf("%d ",p->item);
            p=p->next;
            list_size++;
            }
printf("\n Enter the number of lists to be formed:");
scanf("%d",&K);
for(i=1;i<=K;i++)
            {
            root[i]=(node *)malloc(sizeof(node));
            q=root[i];
            for(j=i;j<=list_size;j++)
                        {
                        q->item=element(root[0],j);
      q->next=(node *)malloc(sizeof(node));
      q=q->next;
                        //printf("%d ",q->item);
                        j=j+K-1;
                        }
            q->next=NULL;
            }
for(i=1;i<=K;i++)
            {
            printf("\n Elements of List M[%d]:\n",i);
                        while(root[i]->next!=NULL)
                                    {printf("%d ",root[i]->item);
                                                root[i]=root[i]->next;
                                    }
            printf("\n");
            }
return 0;
}
int element(node *data,int j)
            {
            int i;
            for(i=1;i<=j-1;i++)
                        data=data->next;
            return(data->item);
            }

C Program #12


/*A program that will search a line of given text for a particular pattern or substring. If the pattern is found it is to be replaced by another given pattern*/
#include<stdio.h>
#include<string.h>
int replace(char *,char[],char[],int,int,int,int);
int main()
{
char line[200],srch[15],rep[15];
int i,j,l_line,l_srch,l_rep,pos;
printf("\n Enter the line:");
gets(line);
l_line=strlen(line);
printf("\n Enter the pattern to be replaced:");
gets(srch);
l_srch=strlen(srch);
printf("\n Enter the pattern to be place:");
gets(rep);
l_rep=strlen(rep);
for(i=0;i<=(l_line-l_srch);i++)
            {
                        for(j=0;j<l_srch;j++)
                                    if(line[i+j]==srch[j])
                                                continue;
                                    else
                                                break;
                                    if(j==l_srch)
                                                {
                                                pos=i;
                                                l_line=replace(line,srch,rep,pos,l_line,l_srch,l_rep);
                                                }

            }
printf("\n String after replacement:\n");
puts(line);
return 0;
}
int replace(char *line,char srch[],char rep[],int pos,int l_line,int l_srch,int l_rep)
{
int i,m=0;
            if(l_rep-l_srch>=0)
                        {
                        for(i=l_line-1;i>=(pos+l_srch);i--)
                                                            {
                                                            line[i+l_rep-l_srch]=line[i];
                                                            }
                        line[l_line+l_rep-l_srch]='\0';
                        for(i=pos,m=0;i<l_rep+pos;i++,m++)
                                    {line[i]=rep[m];}
                        return(strlen(line));
                        }
            else if(l_rep-l_srch<0)
                        {
                        for(i=(pos+l_srch),m=0;i<=l_line-1;i++,m++)
                                                            {
                                                            line[pos+l_rep+m]=line[i];
                                                            }
                        line[l_line-l_srch+l_rep]='\0';
                        for(i=pos,m=0;i<pos+l_rep;i++,m++)
                                    {line[i]=rep[m];}
                        return(strlen(line));
                        }
   else
   return(l_line);
}

Friday, March 8, 2013

C Program #11


/* STACK*/


#include<stdio.h>
#include<conio.h>
#define STACKSIZE 100
#define TRUE 1
#define FALSE 0
FILE *fp;
struct stack
                {
                int items[STACKSIZE];
                int top;
                };
struct stack s,s1;
void push(struct stack *ps);
void push1(struct stack *ps,int x);
void pop(struct stack *ps);
int pop1(struct stack *ps);
int isempty(struct stack *ps);
void display(struct stack *ps);
void reverse(struct stack *ps);
void filestack(struct stack *ps, FILE *fp);
void duplicate(struct stack *ps);
void secondStack(struct stack *ps);
int IsStackequal(struct stack *ps,struct stack *ps1);
int main()
{
                fp=fopen("data.txt","w");
                int choice,x;
                s.top=s1.top=-1;
                do
                {
                                printf("\n Press 1 to push a element:");
                                printf("\n Press 2 to pop a element:");
                                printf("\n Press 3 to test for empty:");
                                printf("\n Press 4 to display the elements of stack in order of insertion:");
                                printf("\n Press 5 to display the elements of stack in reverse order:");
                                printf("\n Press 6 to read from a file and print in reverse order:");
                                printf("\n Press 7 to duplicate the top element:");
                                printf("\n Press 8 to enter elements in the second array:");
                                printf("\n Press 9 to display the second stack:");
                                printf("\n Press 10 to compare stack one and two:");
                                printf("\n Enter choice:");
                                printf("\n Type 7 to exit:");
                                scanf("%d",&choice);
                                switch(choice)
                                                {
                                                                case 1:
                                                                push(&s);
                                                                break;
                                                                case 2:
                                                                pop(&s);
                                                                case 3:
                                                                x=isempty(&s);
                                                                if(x==TRUE)
                                                                printf("\n Stack is empty:\n");
                                                                else
                                                                printf("\n Stack is not empty");
                                                                break;
                                                                case 4:
                                                                display(&s);
                                                                break;
                                                                case 5:
                                                                reverse(&s);
                                                                break;
                                                                case 6:
                                                                filestack(&s,fp);
                                                                break;
                                                                case 7:
                                                                duplicate(&s);
                                                                break;
                                                                case 8:
                                                                secondStack(&s1);
                                                                break;
                                                                case 9:
                                                                display(&s1);
                                                                break;
                                                                case 10:
                                                                x=IsStackequal(&s,&s1);
            if(x==TRUE)
                                                                printf("\n Both Stacks are equal:\n");
                                                                else
                                                                printf("\n Stacks are unequal:\n");
            break;
                                                                default:
                                                                break;
                                                }
                                }
                                while(1<=choice && choice<=10);
                return 0;
}
int IsStackequal(struct stack *ps,struct stack *ps1)
                {
                int flag=1,k=ps->top,i;

                if(ps->top != ps1->top)
                                return (FALSE);
                else
                                {
                                 for(i=0;i<k;i++)
                                                if(ps->items[i]==ps1->items[i])
                                                                continue;
                                                else
                                                                { flag=0; break;}
                                 }
                                if(flag=1)
                                                return (TRUE);
                                else
                return (FALSE);

                }
void secondStack(struct stack *ps)
                {
                int x=1;
                printf("\n Type -999 to exit enetering:");
                while(x!=-999 || ps->top==STACKSIZE-1)
                                {
                                printf("\n Enter the element:");
                                scanf("%d",&x);
                                if(x==-999)
                break;
                                push1(&s1,x);
                                }
                }
void duplicate(struct stack *ps)
                {
                int x;
                if(ps->top==STACKSIZE-1 || isempty(&s))
                                printf("\n Element cannot be duplicated:");
                else
                                {
                                 x=pop1(&s);
                                 push1(&s,x);
                                 push1(&s,x);
                                 }
                }
int pop1(struct stack *ps)
                {
                return(ps->items[ps->top--]);
                }

void push1(struct stack *ps,int x)
                {
    if(ps->top==STACKSIZE-1)
                                printf("\n Stack Overflow:");
                 else
                                ps->items[++(ps->top)]=x;
                 }
void filestack(struct stack *ps, FILE *fp)
                {
                int n,i,x;
                printf("\n Enter the number of elements to be entered in file:");
                scanf("%d",&n);
                for(i=1;i<=n;i++)
                                {
                                printf("\n Enter the %d# elemet:",i);
                                scanf("%d",&x);
                                fprintf(fp,"%d ",x);
      }
                fclose(fp);
   fp=fopen("data.txt","r");
                for(i=1;i<=n;i++)
                                {
                                fscanf(fp,"%d",&x);
                  //          printf("%d",x);
                                push1(&s,x);
                                }
                printf("\n Data from file in reverse order:");
                while(!isempty(&s))
                                   printf("%d ",pop1(&s));
   fclose(fp);
                }
int isempty(struct stack *ps)
                {
                if(ps->top==-1)
                 return(TRUE);
                else
                return(FALSE);
   }
void reverse(struct stack *ps)
                {
                int d;
                d=ps->top+1;
                 while(d--!=0)
                                printf("%d ",ps->items[d]);
                 }
void pop(struct stack *ps)
                {
                if(ps->top==-1)
                                {
                                printf("\n Stack is empty, element cannot be popped:\n");
                                }
                else
                                {
                                printf("\n Element popped out is %d\n",ps->items[ps->top--]);
                                }
                }

void push(struct stack *ps)
                {
                int x;
                if(ps->top==STACKSIZE-1)
                                {
                                                printf("\n Stack Overflow:");
                                }
                else
                                {
                                printf("\n Enter the element to be inserted:");
                                scanf("%d",&x);
                                ps->items[++(ps->top)]=x;
                                }
                }
void display(struct stack *ps)
                {
                printf("\n");
                int i;
                printf("\n Elements of the stack are:");
                for(i=0;i<=ps->top;i++)
                                {
                                printf("%d ",ps->items[i]);
                                }
                }