The Programming Project

Sunday, March 10, 2013

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]);
                                }
                }



Wednesday, February 13, 2013

C Program #10

/* Circular Link List */

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct circular_list
{
int d;
struct circular_list *next;
};
typedef struct circular_list node;
void create(node *);
void display(node *,node *);
node *search(node *,node *);
void insert(node *,node *);
void displaypos(node *,node *);
int main()
{
int choice;
node *root,*head,*tag;
root=(node *)malloc(sizeof(node));
head=root;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display:");
printf("\n Press 3 to search:");
printf("\n Press 4 to insert");
printf("\n Press 5 to display with respect to an element in the list:");
printf("\n Press 6 to exit \n");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
display(root,head);
break;
case 3:
tag=search(root,head);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 4:
insert(root,head);
break;
case 5:
displaypos(root,head);
break;
case 6:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int n,i;
node *p;
p=record;
printf("\n Enter the number of elements to be added:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
for(i=1;i<=n-1;i++)
{
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
}
record->next=p;
return;
}
void display(node *record,node *head)
{
printf("\n List elements are:\n");
while(1)
{
printf(" %d ",record->d);
record=record->next;
if(record==head)
break;
}
return;
}
node *search(node *record,node *head)
{
display(record,head);
node *p;
int n,flag=0;
printf("\n Enter the item to be searched:");
scanf("%d",&n);
while(record->next!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
if(record==head)
          break;
}
if(flag==1)
return (p);
else
return (NULL);
}
void insert(node *record,node *head)
{
node *pos,*p,*temp;
printf("\n Search the element after which the item has to be inserted:");
pos=search(record,head);
if(pos==NULL)
{
printf("\n Element cannot be inserted:");
}
else
{
p=(node *)malloc(sizeof(node));
printf("\n Enter the item to be inserted:");
scanf("%d",&p->d);
temp=pos->next;
pos->next=p;
p->next=temp;
}

}
void displaypos(node *record,node *head)
{
node *tag,*p;
printf("\n Search the item w.r.t which the list will be displayed:");
tag=search(record,head);
if(tag==NULL)
{
printf("\n List Cannot be displayed:\n");
}
else
{
p=tag;
printf("\n List with respect to %d",tag->d);
while(tag->next!=p)
{
printf(" %d ",tag->next->d);
tag=tag->next;
}
}
return;
}

C Program #9

/*Doubly Linked List */

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct list_element
{
int d;
struct list_element *next,*prev;
};
typedef list_element node;
void create(node *);
void displayn(node *);
void displayr(node *);
node *search(node *);
node *inserteb(node *);
void insertel(node *);
void inserte(node *);
node *deletef(node *);
void deletee(node *);
void deleteb(node *);
int main()
{
int choice;
node *root,*record,*tag ;
record=(node *)malloc(sizeof(node));
root=record;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display list in forward drection:");
printf("\n Press 3 to display list in reverse direction:");
printf("\n Press 4 to search in the list:");
printf("\n Press 5 to inserte at the begining of the list:");
printf("\n Press 6 to inserte at the end of the list:");
printf("\n Press 7 to inserte between first and last element:");
printf("\n Press 8 to delete the first element:");
printf("\n Press 9 to delete the last elemnt:");
printf("\n Press 10 to delete between the last and first elements:");
printf("\n Press 11 to exit:");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
displayn(root);
break;
case 3:
displayr(root);
break;
case 4:
tag=search(root);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 5:
root=inserteb(root);
break;
case 6:
insertel(root);
break;
case 7:
inserte(root);
break;
case 8:
root=deletef(root);
break;
case 9:
deletee(root);
break;
case 10:
deleteb(root);
   break;
case 11:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int i,n;
node *p;
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
record->prev=NULL;
for(i=1;i<=n-1;i++)
{
p=record;
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
record->prev=p;
}
record->next=NULL;
return;
}
void displayn(node *record)
{
 printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->next;
}


}
void displayr(node *record)
{
printf("\n");
while(record->next!=NULL)
{
 // printf("\t %d",record->d);
record=record->next;
}
printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->prev;
}
}
node *search(node *record)
{
int n,flag=0;
node *p;
printf("\n Enter the element to be searched:\n");
scanf("%d",&n);
while(record!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
}
if(flag==1)
return (p);
else
return (NULL);
}
node *inserteb(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=record;
record->prev=p;
p->prev=NULL;
return (p);
}
void insertel(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
while(record->next!=NULL)
{
record=record->next;
}
record->next=p;
p->next=NULL;
p->prev=record;
}
void inserte( node *record)
{
node *p,*tag,*pv;
displayn(record);
printf("\n Enter the element after which insertion has to be done:");
tag=search(record);
if(tag==NULL)
{
printf("\n Cannot be inserted");
}
else
{
pv=tag;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=tag->next;
tag->next=p;
p->prev=pv;
p->next->prev=p;

}
}
node *deletef(node *record)
{
node *p;
p=record->next;
p->prev=NULL;
free(record);
return(p);
}
void deletee(node *record)
{
 node *p;
while(record->next!=NULL)
{
record=record->next;
}
p=record->prev;
p->next=NULL;
p->prev=record->prev->prev;
   free(record);
}
void deleteb(node *record)
{
 node *p,*tag;
 displayn(record);
 printf("\n Item to be deleted, should be searched:\n");
 tag=search(record);
 if(tag==NULL)
printf("\n Element not found, try again");
 else
{
p=tag->prev;
p->next=tag->next;
p->next->prev=p;
free(tag);
}
}