Showing posts with label E and TC DS programs. Show all posts

Implementation of Sorting algorithms in C

, by Engineer's Vision


#include<stdio.h>
#include<conio.h>
void selection(int a[], int n);
void insertion(int a[], int n);
void bubble(int a[],int n);
void main()
{
    int a[50],n,i,s;
    clrscr();
        do
        {
        printf("\n 1.Insertion Sort.\t 2.Selection Sort.\t 3.Bubble Sort.\t 4.Quit.");
        printf("\n Enter Your Choice:");
        scanf("%d",&s);
        switch(s)
            {
            case 1:
            printf("\n Enter The No Of Elements:");
            scanf("%d",&n);
            printf("\n Enter The Elements Of The Array:");
            for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            insertion(a,n);
            break;
            case 2:
            printf("\n Enter The No Of Elements:");
            scanf("%d",&n);
            printf("\n Enter The Elements Of The Array:");
            for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            selection(a,n);
            break;
            case 3:
            printf("\n Enter The No Of Elements:");
            scanf("%d",&n);
            printf("\n Enter The Elements Of The Array:");
            for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            bubble(a,n);
            break;
            }
        }
        while(s!=4);
}
void insertion(int a[], int n)
{
    int i,j,temp,k;
    printf("\n Unsorted Data:");
    for(k=0;k<n;k++)
    printf("%d\t",a[k]);
    for(i=1;i<n;i++)
    {
        temp=a[i];
        for(j=i-1;j>=0 && a[j]>temp;j--)
        a[j+1]=a[j];
        a[j+1]=temp;
        printf("\n (After Pass %d)",i);
        for(k=0;k<n;k++)
        printf("%d\t",a[k]);
    }
}
void selection(int a[],int n)
{
    int i,j,k,temp;
    printf("\n Unsorted Data:");
    for(k=0;k<n;k++)
    printf("%d\t",a[k]);
    for(i=0;i<n-1;i++)
    {
    k=i;
    for(j=i+1;j<n;j++)
    if(a[j]<a[k])
    k=j;
    if(k!=i)
    {
    temp=a[i];
    a[i]=a[k];
    a[k]=temp;
    }
    printf("\n (After Pass %d)",i+1);
    for(k=0;k<n;k++)
    printf("%d\t",a[k]);
    }
}
void bubble(int a[], int n)
{
    int i,j,k,temp;
    printf("\n Unsorted Data:");
    for(k=0;k<n;k++)
    printf("%d\t",a[k]);
    for(i=1;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        if(a[j]>a[j+1])
        {
            temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
        }
    printf("\n (After Pass  %d)",i);
    for(k=0;k<n;k++)
    printf("%d\t",a[k]);
    }
}

OUTPUT
1.Insertion Sort.       2.Selection Sort.       3.Bubble Sort.  4.Quit.
 Enter Your Choice:1

 Enter The No Of Elements:5

 Enter The Elements Of The Array:65
54
43
32
21

 Unsorted Data:65       54      43      32      21
 (After Pass 1)54       65      43      32      21
 (After Pass 2)43       54      65      32      21
 (After Pass 3)32       43      54      65      21
 (After Pass 4)21       32      43      54      65
 1.Insertion Sort.       2.Selection Sort.       3.Bubble Sort.  4.Quit.
 Enter Your Choice:2

 Enter The No Of Elements:5

 Enter The Elements Of The Array:65
54
43
32
21

 Unsorted Data:65       54      43      32      21
 (After Pass 1)21       54      43      32      65
 (After Pass 2)21       32      43      54      65
 (After Pass 3)21       32      43      54      65
 (After Pass 4)21       32      43      54      65
 1.Insertion Sort.       2.Selection Sort.       3.Bubble Sort.  4.Quit.
 Enter Your Choice:3

 Enter The No Of Elements:5

 Enter The Elements Of The Array:65
54
43
32
21

 Unsorted Data:65       54      43      32      21
 (After Pass  1)54      43      32      21      65
 (After Pass  2)43      32      21      54      65
 (After Pass  3)32      21      43      54      65
 (After Pass  4)21      32      43      54      65
 1.Insertion Sort.       2.Selection Sort.       3.Bubble Sort.  4.Quit.
 Enter Your Choice:
read more

C source code for implementation of Singly linked list

, by Engineer's Vision

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data ;
struct node * next;
}node;
node *create();
node *insert_b(node *head,int x);
node *insert_e(node *head,int x);
node *insert_in(node *head,int x);
node *delete_b (node *head);
node *delete_e(node *head);
node *delete_in(node *head);
void search ( node *head ) ;
void print (node *head);

void main()
{
    int op,op1,x;
    node *head=NULL ;
    clrscr();
    do
    {
        printf("\n1.Create\n2.Insert\n3.Delete\n4.Search");
        printf("\n5.Print\n6.Quit");
        printf("\n Enter your choice:");
        scanf("%d",&op);
        switch(op)
        {
        case 1 :
        head=create();
        break;
        case 2 :
        printf("\n\t1.Beginning\n\t2.End\n\t3.Intermediate");
        printf("\n Enter your choice");
        scanf("%d",&op1);
        printf("\n Enter the data to be inserted: ");
        scanf("%d",&x);
        switch(op1)
        {
            case 1 :
            head=insert_b(head,x);
            break;
            case 2 :
            head=insert_e(head,x);
            break;
            case 3 :
            head=insert_in ( head,x);
            break;
            }
            break;
        case 3 :
        printf("\n\t1.Beginning\n\t2.End\n\t3.Intermediate");
        printf("\n Enter your choice:");
        scanf("%d",&op1);
        switch(op1)
        {
             case 1 :
             head=delete_b(head);
             break;
             case 2 :
             head=delete_e(head);
             break;
             case 3 :
             head=delete_in ( head);
             break;
        }
        break ;
        case 4 :
        search (head);
        break;
        case 5 :
        print (head);
        break;
        }
    }while(op!=6);
}

node *create()
{
    node *head,*p;
    int i,n;
    head=NULL;
    printf("\nEnter No Of Data:");
    scanf ( " %d",&n);
    printf ( "\n Enter The Data:" );
    for ( i=0;i<n;i++)
    {
        if ( head==NULL)
        p=head=(node*)malloc(sizeof(node));
        else
        {
        p->next=(node*)malloc(sizeof(node));
        p=p->next;
        }
        p->next=NULL;
        scanf("%d",&(p->data));
    }
    return(head);
}
node *insert_b(node *head,int x)
{
    node *p;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=head;
    head=p;
    return(head);
}
node *insert_e(node *head, int x )
{
    node *p,*q;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=NULL;
    if ( head==NULL)
    return (p) ;
    q=head;
    while(q->next!=NULL)
    q=q->next;
    q->next=p;
    return(head);
}
node *insert_in(node *head, int x )
{
    node *p , *q ;
    int y;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=NULL;
    printf("\nInsert After Which Number?:");
    scanf("%d",&y);
    for(q=head;q!=NULL&&q->data != y ;q=q->next)
    ;
    if (q!=NULL)
    {
        p->next=q->next;
        q->next=p;
    }
    else
    printf("\nData Not Found." );
    return (head);
}
node *delete_b(node *head)
{
    node *p,*q;
    if(head==NULL)
    {
        printf("\n Underflow.....Empty Linked List");
        return (head);
    }
    p=head;
    head=head->next;
    free(p);
    return(head);
}
node *delete_e( node *head)
{
    node *p,*q;
    if(head==NULL)
    {
        printf("\n Underflow.....Empty Linked List");
        return (head);
    }
    p=head;
    if ( head->next==NULL)
    {
        head=NULL;
        free(p);
        return(head);
    }
    q=head;
    while(q->next->next!=NULL)
        q=q->next;
        p=q->next;
        q->next=NULL;
        free(p);
        return(head);
}
node *delete_in(node *head)
{
    node *p,*q;
    int x,i;
    if(head==NULL)
    {
        printf("\nUnderflow....Empty Linked List");
        return(head);
    }
    printf("\nEnter the data to be deleted : ");
    scanf("%d",&x);
    if(head->data==x)
    {
        p=head;
        head=head->next;
        free(p);
        return(head);
    }
    for(q=head;q->next->data!=x && q->next !=NULL;q=q->next)
    if(q->next==NULL)
    {
        printf("\nUnderflow.....data not found");
        return(head);
    }
    p=q->next;
    q->next=q->next->next;
    free(p);
    return(head);
}
void search(node *head)
{
    node *p;
    int data,loc=1;
    printf("\nEnter The Data To Be Searched: ");
    scanf("%d",&data);
    p=head;
    while(p!=NULL && p->data != data)
    {
        loc++;
        p=p->next;
    }
    if(p==NULL)
    printf("\nNot Found.");
    else
    printf("\nFound At Location=%d",loc);
}
void print(node *head)
{
    node *p;
    printf("\n\n");
    for(p=head;p!=NULL;p=p->next)
    printf("%d ",p->data);
}
read more

C source code for implementation of stacks using Linked List

, by Engineer's Vision

/* stacks using Linked List */

#include<stdio.h>
#include<conio.h>
typedef struct stack
{
int data;
struct stack *next;
}stack;
void initialize(stack **);
int empty(stack *);
int pop(stack **);
void push(stack **, int);
void print(stack *p);
void main()
{
stack *top;
int x,op;
initialize(&top);
clrscr();
do
{
printf("\n1.push \n2.pop \n3.print \n4.quit");
printf("\n enter your choice");
scanf("%d",&op);
switch(op)
{
case 1:
printf("enter a number");
scanf("%d",&x);
push(&top,x);
break;
case 2:
if(!empty(top))
{
x=pop(&top);
printf("\n popped value is=%d",x);
}
else printf("\n stack is empty");
break;
case 3:
print(top);
break;
}
}
while(op!=4);
}
void initialize(stack **t)
{
*t=NULL;
}
int empty(stack *top)
{
if(top==NULL)
return (1);
return (0);
}
void push(stack **t, int x)
{
stack *p;
p=(stack *)malloc(sizeof(stack));
p->data=x;
p->next=*t;
*t=p;
}
int pop(stack **t)
{
int x;
stack *p;
p=*t;
*t=p->next;
x=p->data;
free(p);
return(x);
}
void print(stack *p)
{
printf("\n");
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}
read more

C source code for implementation of Queue using Linked List

, by Engineer's Vision


/* Queue using Linked List */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct Q
{
node *R,*F;
}Q;
void initialize(Q*);
int full(Q*);
int empty(Q*);
void addqueue(Q*,int);
int delqueue(Q*);
void print(Q*);
void main()
{
Q q;
int x,i,op;
initialize(&q);
clrscr();
do
 {
  printf("\n1.insert\n2.delete\n3.print\n4.quit");
  printf("\nenter your choice");
  scanf("%d",&op);
  switch(op)
   {
   case 1:
   printf("\nenter a value");
   scanf("%d",&x);
   addqueue(&q,x);
   break;
   case 2:
   if(!empty(&q))
    {
    x=delqueue(&q);
    printf("\ndeleted data=%d",x);
    }
   else printf("\nqueue is empty");
   break;
   case 3:
   print(&q);
   break;
  }
 }while(op!=4);
}
void initialize(Q *qP)
{
qP->R=NULL;
qP->F=NULL;
}
void addqueue(Q *qP, int x)
{
node *P;
P=(node*)malloc(sizeof(node));
P->data=x;
P->next=NULL;
if(empty(qP))
 {
 qP->R=P;
 qP->F=P;
 }
else
 {
 (qP->R)->next=P;
 qP->R=P;
 }
}
int delqueue(Q *qP)
{
 int x;
 node *P;
 P=qP->F;
 x=P->data;
 if(qP->F==qP->R) //deleting the last element
  initialize(qP);
 else
  qP->F=P->next;
 free(P);
 return(x);
}
void print(Q *qP)
{
 int i;
 node *P;
 P=qP->F;
 while(P!=NULL)
  {
  printf("\n%d",P->data);
  P=P->next;
  }
}
int empty(Q *qP)
{
 if(qP->R==NULL)
 return 1;
 return 0;
}
read more

C source code for implementation of Queue using array

, by Engineer's Vision


/* Queue using array */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
typedef struct Q
{
 int R,F;
 int data[MAX];
}Q;
void initialize(Q *P);
int empty(Q *P);
int full(Q *P);
void addqueue(Q *P,int x);
int delqueue(Q *P);
void print(Q *P);
void main()
{
 Q q;
 int op,x;
 initialize(&q);
 clrscr();
 do
  {
  printf("\n1.insert\n2.delete\n3.print\n4.quit");
  printf("\nenter your choice");
  scanf("%d",&op);
  switch(op)
   {
   case 1:
   printf("\nenter a value");
   scanf("%d",&x);
   if(!full(&q))
    {
    addqueue(&q,x);
   else
    printf("\nqueue is full");
   break;
   case 2:
   if(!empty(&q))
    {
    x=delqueue(&q);
    printf("\ndeleted data=%d",x);
    }
   else
   printf("\nqueue is empty");
   break;
   case 3:
   print(&q);
   break;
  }
 }while(op!=4);
}
void initialize(Q *P)
{
 P->R=-1;
 P->F=-1;
}
int empty(Q *P)
{
 if(P->R==-1)
 return 1;
 return 0;
}
int full(Q *P)
{
 if((P->R+1)>=MAX-1)
 return 1;
 return 0;
}
void addqueue(Q *P,int x)
{
 if(P->R==-1)
  {
  P->R=P->F=0;
  P->data[P->R]=x;
  }
 else
  {
  P->R=(P->R+1);
  P->data[P->R]=x;
  }
}
int delqueue(Q *P)
{
 int x;
 x=P->data[P->F];
 if(P->R==P->F)
 {
 P->R=-1;
 P->F=-1;
 }
 else
 P->F=(P->F+1);
 return(x);
}
void print(Q *P)
{
 int i;
 if(!empty(P))
 {
 printf("\n");
 for(i=P->F;i!=P->R;i++)
 printf("%d\t",P->data[i]);
 }
 printf("%d\t",P->data[i]);
}
read more

C source code for postfix evaluation

, by Engineer's Vision


/* postfix evaluation */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 50
typedef struct stack
{
int data[MAX];
int top;
}stack;
void init(stack*);
void push(stack*,int);
void evaluate(char postfix[]);
int pop(stack*);
void main()
{
char postfix[30];
clrscr();
printf("enter the postfix expression");
gets(postfix);
evaluate(postfix);
getch();
}

void init(stack *s)
{
s->top= -1;
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
void evaluate(char postfix[])
{
stack s;
int op1,op2,val=0,i;
init(&s);
for(i=0;postfix[i]!='\0';i++)
 {
 if(isalpha(postfix[i]))
  {
  printf("\n enter the value of %c",postfix[i]);
  scanf("%d",&val);
  push(&s,val);
  }
 else
  {
  op2=pop(&s);
  op1=pop(&s);
  switch(postfix[i])
   {
   case '+': push(&s,op1+op2); break;
   case '-': push(&s,op1-op2); break;
   case '*': push(&s,op1*op2); break;
   case '/': push(&s,op1/op2); break;
   case '%': push(&s,op1%op2); break;
   }
  }
 }
 val=pop(&s);
 printf("\n value of the expression is %d",val);
 getch();
}
read more

C source code for implementation of Binary Search Tree

, by Engineer's Vision

/* binary search tree */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct bstnode
{
 int data;
 struct bstnode *left,*right;
}bstnode;

bstnode *init();
bstnode *search(bstnode*,int);
bstnode *insert(bstnode*,int);
bstnode *create();

void inorder(bstnode *t);
void preorder(bstnode *t);
void postorder(bstnode *t);
void main()
{
 bstnode *root=NULL,*p;
 int x,op;
 clrscr();
 do
 {
 printf("\n1.create\n2.search\n3.insert\n4.preorder\n5.inorder\n6.postorder\n7.exit");
 printf("\nenter your choice");
 scanf("%d",&op);
 switch(op)
  {
   case 1:
   root=create();
   break;

   case 2:
   printf("\nenter the key to be searched");
   scanf("%d",&x);
   p=search(root,x);
   if(p==NULL)
   printf("\nkey not found");
   else
   printf("\nfound");
   break;

   case 3:
   printf("\nenter the data to be inserted");
   scanf("%d",&x);
   root=insert(root,x);
   break;

   case 4:
   preorder(root);
   break;

   case 5:
   inorder(root);
   break;

   case 6:
   postorder(root);
   break;
  }
 }while(op!=7);
}

void inorder(bstnode *t)
{
if(t!=NULL)
 {
 inorder(t->left);
 printf("%d\t",t->data);
 inorder(t->right);
 }
}

void preorder(bstnode *t)
{
 if(t!=NULL)
 {
 printf("%d\t",t->data);
 preorder(t->left);
 preorder(t->right);
 }
}

void postorder(bstnode *t)
{
 if(t!=NULL)
 {
  postorder(t->left);
  postorder(t->right);
  printf("%d\t",t->data);
 }
}

bstnode *search(bstnode *root,int x)
{
 while(root!=NULL)
 {
  if(x==root->data)
  return(root);
  if(x>root->data)
  root=root->right;
  else
  root=root->left;
 }
 return(NULL);
}

bstnode *insert(bstnode *t,int x)
{
 bstnode *p,*q,*r;
 r=(bstnode*)malloc(sizeof(bstnode));
 r->data=x;
 r->left=NULL;
 r->right=NULL;
 if(t==NULL)
 return(r);
 p=t;
 while(p!=NULL)
 {
  q=p;
  if(x>p->data)
  p=p->right;
  else
  if(x<p->data)
  p=p->left;
  else
  {
  printf("\nduplicate entry");
  return(t);
  }
 }
if(x>q->data)
q->right=r;
else
q->left=r;
return(t);
}

bstnode *create()
{
int n,x,i;
bstnode *root;
root=NULL;
printf("\nno of nodes");
scanf("%d",&n);
printf("\nenter the values");
for(i=0;i<n;i++)
 {
 scanf("%d",&x);
 root=insert(root,x);
 }
return(root);
}
read more

C source code for implementation of BFS and DFS

, by Engineer's Vision


/* adjacency matrix */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
typedef struct queue
{
int r,f;
int data[MAX];
}q;

void addqueue(q *p,int x);
int delqueue(q *p);
int empty(q *p);
void bfs(int);
void dfs(int);
int G[MAX][MAX];
int n=0;
int visited[MAX];
void main()
{
 int i,j,v,op,m;
 clrscr();
 printf("\nenter the number of vertices");
 scanf("%d",&n);
 printf("\nenter the number of edges");
 scanf("%d",&m);
 for(i=0;i<n;i++)
  for(j=0;j<m;j++)
  G[i][j]=0;
  printf("\nenter graph as a list of edges(starting vtex is 0)");
  for(v=0;v<m;v++)
  {
  printf("\nenter the next edge(starting vtex,end vtex)");
  scanf("%d%d",&i,&j);
  G[i][j]=G[j][i]=1;
  }
  do
  {
   printf("\n1.dfs\n2.bfs\n3.display\n4.quit");
   printf("\nenter your choice");
   scanf("%d",&op);
   switch(op)
   {
    case 1:
    printf("\nenter the starting vertex for dfs");
    scanf("%d",&v);
    for(i=0;i<n;i++)
    visited[i]=0;
    dfs(v);
    break;

    case 2:
    printf("\nenter the starting vertex for bfs");
    scanf("%d",&v);
    bfs(v);
    break;

    case 3:
    printf("\nadjacency matrix is:");
    for(i=0;i<n;i++)
    {
     printf("\n");
     for(j=0;j<n;j++)
     printf("%d",G[i][j]);
     }
    break;
   }
  }while(op!=4);
 }

void bfs(int v)
{
 int i;
 int visited[MAX];
 q Q;
 Q.r=Q.f=-1;
 for(i=0;i<n;i++)
 visited[i]=0;
 addqueue(&Q,v);
 printf("\nvisit %d",&v);
 visited[v]=1;
 while(!empty(&Q))
 {
 v=delqueue(&Q);
 for(i=0;i<n;i++)
 if(visited[i]==0&&G[v][i]!=0)
 {
 addqueue(&Q,i);
 visited[i]=1;
 printf("\n%d",i);
 }
 }
 }

 void dfs(int i)
 {
 int j;
 printf("\n%d",i);
 visited[i]=1;
 for(j=0;j<n;j++)
 if(!visited[j]&&G[i][j]==1)
 dfs(j);
 }

 void addqueue(q *p,int x)
 {
 if(p->r==-1)
 {
 p->r=p->f=0;
 p->data[p->r]=x;
 }
 else
 {
 p->r=p->r+1;
 p->data[p->r]=x;
 }
 }

 int delqueue(q *p)
 {
 int x;
 x=p->data[p->f];
 if(p->r==p->f)
 {
 p->r=-1;
 p->f=-1;
 }
 else
 p->f=p->f+1;
 return(x);
 }

 int empty(q *p)
 {
 if(p->r==-1)
 return(1);
 return(0);
 }
read more

Students database program in C

, by Engineer's Vision

/* database of a student */

#include<stdio.h>
#include<conio.h>
#include<string.h>

typedef struct student
{
 int rollno;
 char name[20];
 int marks;
 }student;

void insert(student st[],int position, int n);
void delet(student st[], int position, int n);
int search(student st[], int rollno, int n);
void print(student st[], int n);
void read(student st[], int n );
void sort(student st[], int n);
void modify(student st[], int n);
void main()
{
 student st[30];
 int n,i,op,position,rollno;
 clrscr();
 do
 {
  printf("\n 1)create \n 2)insert \n 3) delete \n 4) search \n 5)print \n 6) sort \n 7) modify \n 8) quit");
  printf("\n enter your choice");
  scanf("%d",&op);
  switch (op)
  {
   case 1:
   printf("\n enter no of students");
   scanf("%d",&n);
   read(st,n);
   break;
  
   case 2:
   printf("\n enter the position (no of records=%d)",n);
   scanf("%d",&position);
   if(position<=n+1)
   {
   insert(st,position,n);
   n++;
   print(st,n);
   }
   else printf("\m cannot insert");
   break;
  
   case 3:
   printf("\n enter rollno");
   scanf("%d",&rollno);
   position=search(st, rollno, n);
   if(position!=-1)
   {
   delet(st,position,n);
   n--;
   print(st,n);
   }
   else printf("\n cannot delete");
   break;
 
   case 4:
   printf("\n enter rollno");
   scanf("%d",&rollno);
   position=search(st,rollno,n);
   if(position==-1)
   printf("\n not found");
   else {
   printf("\n found at the location=%d",position+1);
   printf("\n %s\t%d\t%d",st[position].name,st[position].rollno,st[position].marks);
   }
   break;

   case 5:
   print(st,n);
   break;

   case 6:
   sort (st,n);
   print(st,n);
   break;

   case 7:
   modify(st,n);
   break;
  }
 } while(op!=8);
}

void insert(student st[], int position, int n)
{
int i;
printf("\n enter data(name rollno marks):");
for(i=n-1;i>=position-1;i--)
st[i+1]=st[i];
scanf("%s%d%d", st[position-1].name,&st[position].rollno,&st[position].marks);
}

void delet(student st[], int position, int n)
{
int i;
for (i=position+1;i<n;i++)
st[i-1]=st[i];
}

int search(student st[], int rollno, int n)
{
int i;
for (i=0;i<n;i++)
if(rollno==st[i].rollno)
return(i);
return(-1);
}

void print(student st[], int n)
{
int i;
for (i=0;i<n;i++)
printf("\n %s\n%d\n%d",st[i].name,st[i].rollno,st[i].marks);
}

void read(student st[], int n)
{
int i;
printf("\n enter data(name rollno marks):");
for(i=0;i<n;i++)
scanf("%s%d%d",st[i].name,&st[i].rollno,&st[i].marks);
}

void sort(student st[], int n)
{
int i,j;
student temp;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(st[j].rollno>st[j+1].rollno)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}

void modify(student st[],int n)
{
int rollno,i;
printf("\n enter roll no");
scanf("%d",&rollno);
i=search(st,rollno,n);
if(i==-1)
printf("\n not found");
else {
printf("\n enter data(name rollno marks):");
scanf("%s%d%d",st[i].name,&st[i].rollno,&st[i].marks);
}
}
read more

C source code for Searching algorithms

, by Engineer's Vision


/* searching */

#include<stdio.h>
#include<conio.h>

int binsearch(int a[], int i, int j, int key);
int linsearch(int a[], int n, int key);
void main()
{
 clrscr();
 int a[30],n,i,key,result,op;
 do
 {
  printf("\n 1-linear search \n 2-binary search \n 3-exit");
  printf("\n enter your choice");
  scanf("%d",&op);
  switch(op)
  {
   case 1:
   printf("\n enter no of elements");
   scanf("%d",&n);
   printf("\n enter the array elements");
   for(i=0;i<n;i++)
   scanf("%d",&a[i]);
   printf("\n enter the no to be searched");
   scanf("%d",&key);
   result= linsearch(a,n,key);
   if (result==-1)
   printf("\n element not found");
   else printf("\n element found at the location %d",result+1);
   break;

   case 2:
   printf("\n enter no of elements");
   scanf("%d",&n);
   printf("\n enter the sorted array elements");
   for(i=0;i<n;i++)
   scanf("%d",&a[i]);
   printf("\n enter the no to be searched");
   scanf("%d",&key);
   result=binsearch(a,0,n-1,key);
   if (result==-1)
   printf("\n element not found");
   else printf("\n element found at the location %d",result+1);
   break;
  }
 }
while(op!=3);
getch();
}

int binsearch(int a[], int i, int j, int key)
{
 int c;
 if (i>j)
 return (-1);
 c= (i+j)/2;
 if (key==a[c])
 return(c);
 if (key>a[c])
 return(binsearch(a,c+1,j,key));
 return(binsearch(a,i,c-1,key));
}

int linsearch(int a[], int n, int key)
{
 int i;
 for(i=0;i<n;i++)
 {
  if (a[i]==key)
  return (i);
 }
 return(-1);
}
read more

Stack using Array in C

, by Engineer's Vision

/* stacks using arrays */

#include<stdio.h>
#include<conio.h>
#define MAX 6
typedef struct stack
{
int data[MAX];
int top;
} stack;
void initialize(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *, int);
void print(stack *);
void main()
{
stack s;
int x,op;
initialize(&s);
clrscr();
do
{
printf("\n 1.push \n2.pop \n3.print \n4.quit");
printf("\n enter your choice");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n enter a number");
scanf("%d",&x);
if(!full(&s))
push(&s,x);
else
printf("\n stack is full");
break;
case 2:
if(!empty(&s))
{
x=pop(&s);
printf("\n popped value is=%d",x);
}
else
printf("stack is empty");
break;
case 3:
print(&s);
break;
}
}
while(op!=4);
}
void initialize(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
return 0;
}
int full(stack *s)
{
if(s->top==MAX-1)
return (1);
return (0);
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return (x);
}
void print(stack *s)
{
int i;
printf("\n");
for(i=s->top;i>=0;i--)
printf("%d",s->data[i]);
}
read more

Polynomial Addition in C

, by Engineer's Vision

/* polynomial */

#include<stdio.h>
#include<conio.h>
#include<math.h>
typedef struct term
    {
     int power;
     float coeff;
    }term;
typedef struct polynomial
    {
     term a[20];
     int n;
    }polynomial;
void read(polynomial *ptr);
void print(polynomial *ptr);
polynomial add(polynomial *p1,polynomial *p2);
void main()
    {
    polynomial P1,P2,P3;
    int option;
    P1.n=P2.n=P3.n=0;
    clrscr();
    do
    {
     printf("\n1: Create 1'st Polynomial.");
     printf("\n2: Create 2'nd Polynomial.");
     printf("\n3: Add Polynomials.");
     printf("\n4: Quit");
     scanf("%d",&option);
     switch(option)
         {
         case 1:read(&P1); break;
         case 2:read(&P2); break;
         case 3:P3=add(&P1,&P2);
         printf("\n1'st polynomial ->");
         print(&P1);
         printf("\n2'nd polynomial ->");
         print(&P2);
         printf("\nSum=");
         print(&P3);
         break;
         }
    }while(option!=4);
    }
void read(polynomial *ptr)
    {
    int n,i,power;
    float coeff;
    term t;
    printf("\n Enter No. Of Terms:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
        printf("\n Enter A Term(Power Coeff.)");
        scanf("%d%f",&power,&coeff);
        t.coeff=coeff;
        t.power=power;
        ptr -> a[i]=t;
        }
    ptr -> n=n;
    }
void print(polynomial *ptr)
    {
    int i;
    printf("%5.2fX^%d",(ptr->a[0]).coeff,(ptr->a[0]).power);
    for(i=1;i<ptr->n;i++)
    printf("+%5.2fX^%d",(ptr->a[i]).coeff,(ptr->a[i]).power);
    }
polynomial add(polynomial *P1,polynomial *P2)
    {
    polynomial P3;
    term t;
    int i,j,k;
    i=j=k=0;
    P3.n=0;
    while(i<P1->n && j<P2->n)
        {
        if(P1->a[i].power==P2->a[j].power)
        {
        t.power=P1->a[i].power;
        t.coeff=P1->a[i].coeff+P2->a[j].coeff;
        P3.a[k]=t;
        i++;j++;k++;
        }
        else
        if(P1->a[i].power<P2->a[j].power)
        P3.a[k++]=P1->a[i++];
        else
        P3.a[k++]=P2->a[j++];
        }
        while(i<P1->n)
        P3.a[k++]=P1->a[i++];
        while(j<P2->n)
        P3.a[k++]=P2->a[j++];
        P3.n=k;
        return(P3);
    }
read more