Sunday, September 22, 2013

Wap to perform Single Linked List Operations.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *start;

int ins_beg()
{
struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the element:");
scanf("%i",&new_node->info);
if(start==NULL)
{
    new_node->link=NULL;
    start=new_node;
}
else
      {
            new_node->link=start;
            start=new_node;
      }
printf("\n Element Inserted sucessfully.\n");
main();
}
int ins_end()
{
    struct node *new_node,*lp;
new_node=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the element:");
scanf("%i",&new_node->info);
if(start==NULL)
{
    new_node->link=NULL;
    start=new_node;
}
else
      {
            lp=start;
            while(lp->link!=NULL)
            {
                  lp=lp->link;
            }
            lp->link=new_node;
            new_node->link=NULL;
      }
printf("\n Element Inserted sucessfully.\n");
main();
}
int ins_pos()
{
struct node *new_node,*tptr;
int pos,i=0;
printf("\n Enter the position:");
scanf("%i",&pos);
if(pos==1)
    {ins_beg();
    main();}
else
{
tptr=start;
for(i=1;i<(pos-1)&&tptr!=NULL;i++)
    tptr=tptr->link;
if(tptr==NULL)
{printf("\n Invalid Position.");
    main();}
    else
    {   new_node=(struct node*)malloc(sizeof(struct node));
        printf("\n Enter the element:");
        scanf("%i",&new_node->info);
        new_node->link=tptr->link;
        tptr->link=new_node;
        printf("\n Item Inserted Sucessfully.");
        main();
    }
}
main();
}
int disp()
{
struct node *t;
if(start==NULL)
   {

    printf("\n List is empty.");
    return;
   }
t=start;
printf("\n Elements are:");
while(t!=NULL)
{
    printf("\n\n%i",t->info);
    t=t->link;
}
main();
}
int del()
{
int pos;
struct node *tptr,*ptr;
ptr=start;
printf("\n Enter the Position::");
scanf("%d",&pos);
if(pos==1)
{
 start=start->link;
}
else
{  int i=0;
    while(i<pos-1 && ptr!=NULL)
    {
        tptr=ptr;
        ptr=ptr->link;
        i++;
    }
    tptr->link=ptr->link;
}
main();
}


int main()
{
    do
    {
    int c,d;

    printf("\n\t\t Single Linked List Operations. \n 1.Insert \t 2.Delete \t 3.Display \t4.Exit.\n\n Enter Your Choice:");
    scanf("%d",&c);
    switch(c)
    {
    case 1:printf("\n\t Insert Operations\n\t 1.Begining \t 2.End \t 3.At a given position\nEnter your choice:");
           scanf("%d",&d);
           switch(d)
           {
               case 1:ins_beg();break;case 2:ins_end();break;case 3:ins_pos();break;default:printf("\n Wrong Choice.!!!");main();break;
           }

case 2:del();break;
case 3:disp();break;
case 4:exit(1);break;
default:printf("Wrong Choice!!!!\n");break;
    }
    }while(1);

    return 0;

}

Thursday, September 19, 2013

Wap to compute the area of a isosceles triangle.


#include<stdio.h>
#include<math.h>
int main() {
 int a, b, c;
 float area, s;
 printf("Enter the sides of a triangle:\n");

 scanf("%d%d%d", &a, &b, &c);
 if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0)) {
  if (a == b || b == c || a == c) {
   s = (a + b + c) / 2.0;
   area = sqrt((s * (s - a) * (s - b) * (s - c)));
   printf("\nArea of an isosceles triangle is: %2f\n", area);
  } else {
   printf("\nTriangle is not an isosceles ");
  }
 } else {
  printf("\n Triangle formation not possible\n");
 }
 return 0;
}

WAP to classify the triangle as equilateral, isosceles and scalene.


#include<stdio.h>
#include<math.h>

int main() {

 int a, b, c;

 float s, area;

 printf("Enter the values of the sides of the triangle: \n");

 scanf("%d %d %d", &a, &b, &c);

 if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0))

 {

  s = (a + b + c) / 2.0;

  area = sqrt((s * (s - a) * (s - b) * (s - c)));

  if (a == b && b == c)

  {

   printf("Equilateral Triangle. \n");

   printf("Area of Equilateral Triangle is: %f", area);

  }

  else if (a == b || b == c || a == c)

  {

   printf("Isosceles Triangle. \n");

   printf("Area of an Isosceles Triangle: %f", area);

  }

  else

  {

   printf("Scalene Triangle. \n");

   printf("Area of Scalene Triangle: %f", area);

  }

 }

 else {
  printf("Triangle formation not possible");
 }

 return 0;
}

Print Your Name without using semicolon in program in C/C++

C

#include<stdio.h>
int main()
{
if(printf("Your Name"))
}


C++

#include<iostream>
using namespace std;
int main()
{
if(cout<<"Your Name")
return 0;
}

Infix to Post fix Conversion


#define SIZE 50            /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */

push(char elem) { /* Function for PUSH operation */
 s[++top] = elem;
}

char pop() { /* Function for POP operation */
 return (s[top--]);
}

int pr(char elem) { /* Function for precedence */
 switch (elem) {
 case '#':
  return 0;
 case '(':
  return 1;
 case '+':
 case '-':
  return 2;
 case '*':
 case '/':
  return 3;
 }
}

main() { /* Main Program */
 char infx[50], pofx[50], ch, elem;
 int i = 0, k = 0;
 printf("\n\nRead the Infix Expression ? ");
 scanf("%s", infx);
 push('#');
 while ((ch = infx[i++]) != '\0') {
  if (ch == '(')
   push(ch);
  else if (isalnum(ch))
   pofx[k++] = ch;
  else if (ch == ')') {
   while (s[top] != '(')
    pofx[k++] = pop();
   elem = pop(); /* Remove ( */
  } else { /* Operator */
   while (pr(s[top]) >= pr(ch))
    pofx[k++] = pop();
   push(ch);
  }
 }
 while (s[top] != '#') /* Pop from stack till empty */
  pofx[k++] = pop();
 pofx[k] = '\0'; /* Make pofx as valid string */
 printf("\n\nGiven Infix Expn: %s  Postfix Expn: %s\n", infx, pofx);
}

Stack Operations.


#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
int item,i;
int push(int stack[SIZE],int &tos,int item)
{
   if(tos==SIZE-1)
        {printf("\n Stack Overflow.");

        }
   tos=tos+1;
   stack[tos]=item;
   return 0;
}
int pop(int stack[SIZE],int &tos)
{
int no;
if(tos==-1)
    printf("\n Stack is empty Cannot delete any Element...\n");
else
{no=stack[tos];
tos=tos-1;}
return 0;
}
int display(int stack[SIZE],int tos)
{   if(tos==-1)
    printf("\n Stack Is Empty......\n");
else{
     printf("\n Stack is:\n");
    for(i=tos;i>=0;i--)
    {    printf("\n __\n|");
        printf("%d",stack[i]);
        printf("|");}}
}
int main()
{
int ch,stack[SIZE],n;
static int tos=-1;
do
{
printf(" \n STACK OPERATIONS \n\t1.FILL STACK(before pushing) 2.PUSH\t 3.POP\t4.DISPLAY\t5.EXIT\n");
scanf("%d",&ch);
switch(ch)
{   case 1:printf("\n Enter the no of elements to be inserted:");
           scanf("%d",&n);
           printf("\n Enter elements:");
           for(i=0;i<n;i++)
            {scanf("%d",&stack[i]);tos++;}
            break;
    case 2:
            printf("\n Enter the element to be inserted:");
            scanf("%d",&item);
           push(stack,tos,item);
           break;
    case 3:
            pop(stack,tos);
            break;
    case 4:display(stack,tos);
           break;
    case 5:printf("\n Terminating......");
           exit(0);
           break;
    default:printf("\n Wrong Choice!!!");
    break;
}printf("\n Press Any Key to Continue.......\n ");


}while(n!=5);
return 0;
}

Wednesday, September 18, 2013

Program to find the transitive closure of a matrix using Warshall's Algorithm.


#include<stdio.h>
int i,j;
#define V 4
void transitiveClosure(int graph[V][V])
{
    int i, j, k;
    for (k = 0; k < V; k++)
    {
        for (i = 0; i < V; i++)
        {
          for (j = 0; j < V; j++)
            {
              graph[i][j] = graph[i][j] || (graph[i][k] && graph[k][j]);
            }
        }
    }
    printf ("Following matrix is transitive closure of the given graph\n");
    for ( i = 0; i < V; i++)
    {
        for ( j = 0; j < V; j++)
            printf ("%d ", graph[i][j]);
        printf("\n");
    }
}
int main()
{
    int graph[V][V] = { {1, 1, 0, 1},
                        {0, 1, 1, 0},
                        {0, 0, 1, 1},
                        {0, 0, 0, 1}
                      };
    transitiveClosure(graph);
    return 0;
}