Pages

Saturday, September 24, 2016

C Program to swap 2 nos without using 3rd variable

Program to swap 2 nos without using 3rd variable-

#include<stdio.h>
void main()
{
int a,b;
printf("Enter two numbers ");
scanf("%d %d",&a,&b);
printf("Before swaping a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swaping a=%d b=%d",a,b);

Output-
Enter two numbers 10 20
Before swaping a=10 b=20


After swaping a=20 b=10

Friday, September 23, 2016

C++ Program for Matrix Addition, Multiplication, Inverse and Transpose using Operator Overloading

Program-

#include<iostream>
using namespace std;

#define MAX 10
class Matrix
{
   int matrix[MAX][MAX],x,y;
 
public:
   void get();
   void put();
   Matrix operator+(Matrix);
   Matrix operator-(Matrix &m);
   Matrix operator*(Matrix);
   Matrix operator^(Matrix &m);
};
void Matrix::get()
{
   cin>>x>>y;
   cout<<"Enter the Matrix :\n";
   for(int i=0;i<x;i++)
      for(int j=0;j<y;j++)
         cin>>matrix[i][j];

}

void Matrix::put()
{
   for(int i=0;i<x;i++)
   {
      cout<<"\n\t";
      for(int j=0;j<y;j++)
         cout<<matrix[i][j]<<" ";
   }
   cout<<endl;
}

Matrix Matrix::operator+(Matrix b)
{
   Matrix temp;
   if((x!=b.x)||(y!=b.y))    //a.x!=b.x
   {
      cout<<"\n\tMatrix Addition is not possible the result is incorrect\n\n";
      temp.x=0;
      temp.y=0;
   }
   else
   {
      temp.x=x;
      temp.y=y;
   }
   for(int i=0;i<x;i++)
      for(int j=0;j<y;j++)
         temp.matrix[i][j]=matrix[i][j]+b.matrix[i][j];
   return temp;
}

Matrix Matrix::operator -(Matrix &m)
{
 Matrix r,r2;
 int ch;
 float determinant=0;

  cout<<"\n1. 2*2 matrix ";
  cout<<"\n2. 3*3 matrix ";
  cout<<"\n enter choice ";
  cin>>ch;
 if(ch==1)  
 {
  cout<<"To Find Inverse Of Matrix \n";
  cout<<"Enter elements of 2*2 matrix:\n";
  for(int i=0;i<2;i++)
{
        for(int j=0;j<2;j++)
          {
              cin>>r2.matrix[i][j];
            }
  }

  //swap the a and d
  float temp=r2.matrix[0][0];
  r2.matrix[0][0]=r2.matrix[1][1];
  r2.matrix[1][1]=temp;

//take determinant
  float d1=r2.matrix[0][0]*r2.matrix[1][1];
  float d2=r2.matrix[0][1]*r2.matrix[1][0];

  //put negtive in front of b and c
  r2.matrix[0][1]=r2.matrix[0][1]-(r2.matrix[0][1]*2);
  r2.matrix[1][0]=r2.matrix[1][0]-(r2.matrix[1][0]*2);

  for(int i=0;i<2;i++)
  {
          for(int j=0;j<2;j++)
         {
              cout<<(r2.matrix[i][j]/(d1-d2))<<"  ";
         }
 cout<<"\n";
}

   r2.x=2;
   r2.y=2;
   return r2;
}
else          
{
 cout<<"To Find Inverse Of Matrix \n";
 cout<<"Enter elements of 3x3 matrix:\n";
 for(int i=0;i<3;i++)
 {
    for(int j=0;j<3;j++)
    {
       cin>>r.matrix[i][j];
    }
 }
 cout<<"\nThe entered matrix is:\n";
 for(int i=0;i<3;i++)
 {
   for(int j=0;j<3;j++)
   {
     cout<<r.matrix[i][j]<<"  ";
    }
  cout<<"\n";
 }
 for(int i=0;i<3;i++)
 {
      determinant = determinant + (r.matrix[0][i]*(r.matrix[1][(i+1)%3]*
      r.matrix[2][(i+2)%3] - r.matrix[1][(i+2)%3]*r.matrix[2][(i+1)%3]));
 }
 if(determinant==0)
 {
  cout<<"Inverse does not exist (Determinant=0).\n";
 }
 else
 {
  cout<<"\nInverse of matrix is: \n";
 }
 for(int i=0;i<3;i++)
 {
  for(int j=0;j<3;j++)
  {
   cout<<((r.matrix[(i+1)%3][(j+1)%3] *
    r.matrix[(i+2)%3][(j+2)%3]) - (r.matrix[(i+1)%3][(j+2)%3]*
     r.matrix[(i+2)%3][(j+1)%3]))/ determinant<<"\t";
  }
  cout<<"\n";
  }
  r.x=3;
   r.y=3;
   return r;
  }
}

Matrix Matrix::operator*(Matrix b)
{
   Matrix r;
   if((x!=b.y)||(y!=b.x))
   {
      cout<<"\n\tMatrix Multiplication is not possible the result is incorrect\n\n";
      r.x=0;
      r.y=0;
   }
   else
   {
      r.x=x;
      r.y=b.y;
   }
   for(int i=0;i<MAX;i++)
      for(int j=0;j<MAX;j++)
         r.matrix[i][j]=0;
   for(int i=0;i<x;i++)
      for(int j=0;j<b.y;j++)
         for(int k=0;(k<y)||(k<b.x);k++)
            r.matrix[i][j]+=matrix[i][k]*b.matrix[k][j];

   return r;
}

Matrix Matrix::operator ^(Matrix &m)
{
   Matrix r;
   for(int i=0;i<x;i++)
      for(int j=0;j<y;j++)
         r.matrix[i][j]=matrix[j][i];
   r.x=x;
   r.y=y;
 
   return r;
}

int main()
{
   char option;
   Matrix a,b,c;
   int temp=1;
   while(temp)
   {
      cout<<"\tSelect Option\n\n1.Matrix Addition\n2.Matrix Inverse\n3.Matrix Multiplication\n4.Matrix Transponse\n5.Exit\n";
      cin>>option;
      switch(option)
      {
        case '1':
         cout<<"\n\tMatrix Addition\n";
         cout<<"Enter the order of first Matrix :\n";
         a.get();
         cout<<"Enter the order of second Matrix :\n";
         b.get();
         c=a+b;
         cout<<"The Addition is:\n";
         c.put();
         break;
        case'2':
         c=a-a;  
         break;
        case'3':
         cout<<"\n\tMatrix Multpication\n";
         cout<<"Enter the order of first Matrix :\n";
         a.get();
         cout<<"Enter the order of second Matrix :\n";
         b.get();
         c=a*b;
         cout<<"The Multiplication is:\n";
         c.put();
         break;
        case'4':
         cout<<"\n\tMatrix Transpose\n";
         cout<<"Enter the order of Matrix :\n";
         a.get();
         c=a^a;
         
         cout<<"The Transpose is:\n";
         c.put();
         break;
         case'5':
         cout<<"\n\tPress any key to exit\n";
         temp=0;
         break;
         default:
         cout<<"\n\tEnter a valid option\n";
      }
     
   }
   return 0;
}

Output-

        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
1

        Matrix Addition
Enter the order of first Matrix :
2 2
Enter the Matrix :
1 2
3 4
Enter the order of second Matrix :
2 2
Enter the Matrix :
1 2
3 4
The Addition is:

        2 4
        6 8
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
2

1. 2*2 matrix
2. 3*3 matrix
 enter choice 1
To Find Inverse Of Matrix
Enter elements of 2*2 matrix:
4 7
2 6
0.6  -0.7
-0.2  0.4
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
2

1. 2*2 matrix
2. 3*3 matrix
 enter choice 2
To Find Inverse Of Matrix
Enter elements of 3x3 matrix:
1 2 3
0 1 4
5 6 0

The entered matrix is:
1  2  3
0  1  4
5  6  0

Inverse of matrix is:
-24     20      -5
18      -15     4
5       -4      1
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
3

        Matrix Multpication
Enter the order of first Matrix :
2
2
Enter the Matrix :
1 2
3 4
Enter the order of second Matrix :
2 2
Enter the Matrix :
1 2
3 4
The Multiplication is:

        7 10
        15 22
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
4

        Matrix Transpose
Enter the order of Matrix :
3 3
Enter the Matrix :
1 2 3
4 5 6
7 8 9
The Transpose is:

        1 4 7
        2 5 8
        3 6 9
        Select Option

1.Matrix Addition
2.Matrix Inverse
3.Matrix Multiplication
4.Matrix Transponse
5.Exit
5

        Press any key to exit

--------------------------------
Process exited after 150.3 seconds with return value 0
Press any key to continue . . .

Friday, September 9, 2016

C++ Program for Bank

Program-

#include<iostream>
using namespace std;

class Bank
{
class DOB
{
int dd,mm,yyyy;
public:
void getDate()
{
cout<<"\nEnter Birth Date\n";
cout<<"\nD ate [dd] ";
cin>>dd;
cout<<"Month [mm] ";
cin>>mm;
cout<<"Year [yyyy] ";
cin>>yyyy;
}
void showDate()
{
cout<<"\nBirth Date- "<<dd<<"/"<<mm<<"/"<<yyyy;
}
};
int acc_no;
char name[30],acc_type[20];
double balance;
DOB dt;

public:
static int no_of_acc;

void assignData(int acno)
{
acc_no=acno;
cout<<"Enter account type ";
cin>>acc_type;
cout<<"Enter name ";
cin>>name;
dt.getDate();
balance=0.0;
no_of_acc++;
}
int searchAccNumber(Bank b[],int acno)
{
for(int i=0;i<no_of_acc;i++)
{
if(b[i].acc_no==acno)
return i;
}
return -1;
}

void showData()
{
cout<<"\nAccount number "<<acc_no;
cout<<"\nAccount type "<<acc_type;
cout<<"\nNAme "<<name;
cout<<"\nCurrent Balance "<<balance;
dt.showDate();
}

void depositAmount()
{
double amt;
cout<<"Enter amount to deposit";
cin>>amt;
if(amt>0)
{
balance+=amt;
cout<<"amount deposited, current balance= Rs."<<balance;
}
else
cout<<"invalid amount ";
}

void withdrawAmount()
{
double amt;
cout<<"enter amt to withdraw";
cin>>amt;
if(amt<=balance && amt>0)
{
balance-=amt;
cout<<"amt withdrawn, current balance Rs."<<balance;
}
else
cout<<"invalid amt entered";
}

static void getTotalAccounts()
{
cout<<"Total number of a/c="<<no_of_acc;
}
};
int Bank::no_of_acc;

int main()
{
int flag=1,ch,i,acno;
Bank b[10];
cout<<"1. Open a/c \n2. Deposit money \n3. Withdraw amt \n4. A/c info and bal\n5. Total no. of A/c\n6. Exit";
while(flag==1)
{
cout<<"\nEnter choice ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter A/c no.";
cin>>acno;
i=b[0].searchAccNumber(b,acno);
if(i==-1)
b[Bank::no_of_acc].assignData(acno);
else
cout<<"\n A/c no. already exists";
break;

case 2: cout<<"enter a/c no.";
cin>>acno;
i=b[0].searchAccNumber(b,acno);
if(i!=-1)
b[i].depositAmount();
else
cout<<"\nInvalid a/c no.";
break;

case 3: cout<<"enter a/c no.";
cin>>acno;
i=b[0].searchAccNumber(b,acno);
if(i!=-1)
b[i].withdrawAmount();
else
cout<<"\n Invalid a/c no";
break;

case 4: cout<<"enter a/c no.";
cin>>acno;
i=b[0].searchAccNumber(b,acno);
if(i!=-1)
b[i].showData();
else
cout<<"\n Invalid a/c no";
break;

case 5: Bank::getTotalAccounts();
break;

case 6: flag=0;

}
}
return 0;
}


Output-

1. Open a/c
2. Deposit money
3. Withdraw amt
4. A/c info and bal
5. Total no. of A/c
6. Exit
Enter choice 1
Enter A/c no.123
Enter account type saving
Enter name Rajesh

Enter Birth Date

D ate [dd] 26
Month [mm] 02
Year [yyyy] 1993

Enter choice 2
enter a/c no.123
Enter amount to deposit50000
amount deposited, current balance= Rs.50000
Enter choice 1
Enter A/c no.456
Enter account type saving
Enter name Divyam

Enter Birth Date

D ate [dd] 19
Month [mm] 08
Year [yyyy] 1994

Enter choice 4
enter a/c no.123

Account number 123
Account type saving
NAme Rajesh
Current Balance 50000
Birth Date- 26/2/1993
Enter choice 5
Total number of a/c=2
Enter choice 6

C++ Program to show various types of Constructors and Destructors

Program-

#include<string.h>
#include<iostream>
using namespace std;

class Emp
{
char name[10];
float sal;
public:
Emp()
{
strcpy(name," ");
sal=0.0;
cout<<"\nDefault Constructor is called\n";
}
Emp(char * str,float s)
{
strcpy(name,str);
sal=s;
}

Emp(Emp &e)
{
strcpy(name,e.name);
sal=e.sal;
}
void input()
{
cout<<"Enter Name and salary\n";
cin>>name>>sal;
}
void put()
{
cout<<"\nName= "<<name;
cout<<"\nSalary= "<<sal;
}

~Emp()
{
cout<<"\n\n-----------Destructor Called-------------";
}
};
int main()
{
cout<<"------Default Constructor ----------\n ";
Emp e1;
e1.input();
e1.put();
cout<<"\n\n------Parameterized  Constructor where Rajesh and 50000 are passed -------\n ";
Emp e2("Rajesh",50000);
e2.put();
cout<<"\n\n------Copy Constructor where e1 is copied-------------\n ";
Emp e3(e1); //implicit call or e3=e1; //explicit call
e3.put();


return 0;
}


Output-

------Default Constructor ----------

Default Constructor is called
Enter Name and salary
Bablu
40000

Name= Bablu
Salary= 40000

------Parameterized  Constructor where Rajesh and 50000 are passed -------

Name= Rajesh
Salary= 50000

------Copy Constructor where e1 is copied-------------

Name= Bablu
Salary= 40000

-----------Destructor Called-------------

-----------Destructor Called-------------

-----------Destructor Called-------------

C++ Program for Multi Stack

Program-

#include<iostream>
using namespace std;
#define MAX 5

class Stack
{
public:
int a[MAX];
int top;

Stack()
{
top=-1;
}

int getdata();
bool IsEmpty();
bool IsFull();
void push();
void pop();
};

int Stack::getdata()
{
int data;
cout<<"\nEnter the Element ";
cin>>data;
return data;
}

bool Stack::IsEmpty()
{
if(top==-1)
return 1;
return 0;
}

bool Stack::IsFull()
{
if(top==MAX)
return 1;
return 0;
}

void display(Stack & r,Stack & g,Stack &b)
{
int i;
if(r.IsEmpty())
cout<<"\nRed stack is empty";
else
{
cout<<"\nItems in  the Red stack \n";
for(i=r.top;i>=0;i--)
cout<<r.a[i]<<endl;
}

if(g.IsEmpty())
cout<<"\nGreen stack is empty";
else
{
cout<<"\nItems in  the Green stack \n";
for(i=g.top;i>=0;i--)
cout<<g.a[i]<<endl;
}
if(b.IsEmpty())
cout<<"\nBlue stack is empty";
else
{
cout<<"\nItems in  the Blue stack \n";
for(i=b.top;i>=0;i--)
cout<<b.a[i]<<endl;
}
}

void Stack::push()
{
if(IsFull())
cout<<"\n stack is full";
else
{
top++;
a[top]=getdata();
}
}

void Stack::pop()
{
if(IsEmpty())
cout<<"\nstack is empty";
else
top--;
}

int main()
{
Stack red,green,blue;
  int ch,temp;

do
  {
  cout<<"\n\t Main menu";
  cout<<"\n 1:Push red";
  cout<<"\n 2:Push green";
  cout<<"\n 3:Push blue";
  cout<<"\n 4:Pop red";
  cout<<"\n 5:Pop green";
  cout<<"\n 6:Pop blue";
  cout<<"\n 7:Show";
  cout<<"\n 8:Exit";
  cout<<"\n Enter Your Choice ";
  cin>>ch;
  switch(ch)
  {
    case 1: red.push(); break;
    case 2: green.push(); break;
    case 3: blue.push(); break;
    case 4: red.pop(); break;
    case 5: green.pop(); break;
    case 6: blue.pop(); break;
    case 7: display(red,green,blue); break;
    case 8: temp=1; break;
    default: cout<<"\n wrong choice";
  }
  }while(ch!=8 && temp!=1);
}

Output-


         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

Enter the Element 10

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

Enter the Element 20

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

Enter the Element 30

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 2

Enter the Element 100

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 2

Enter the Element 200

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 7

Items in  the Red stack
30
20
10

Items in  the Green stack
200
100

Blue stack is empty
         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 4

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 7

Items in  the Red stack
20
10

Items in  the Green stack
200
100

Blue stack is empty
         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 8


C Programming Important Points 1


  • Developed at AT & T’s Bell Laboratories of USA in 1972. Designed and written by Dennis Ritche. 
  • Output Statement Syntax= printf("control string",variable);  //included in stdio.h Header file
          where, control string= format specifier, Message, Escape Seuence character.
          ex- printf("Hello");
                      Escape Sequence Character= \n(new line), \t(tab), \a(alert), \b(backspace)
                      Format Specifier= %d(integer), %f(float), %c(char)
          ex- printf("%d",c); // will print value of c

  • Program to print Variable's Value
#include<stdio.h>
void main()
{
                int a=50;
float b=7.5;
char c='w';
printf("Value of a is %d",a);
printf("\nValue of a is %f",b);           // %.2f = 7.50  two digits after decimal
printf("\nValue of a is %c",c);
}
o/p=
         Value of a is 50
         Value of a is 7.500000
         Value of a is w

[Note- While using Ubantu, don't include conio.h Header File(U Can't use getch(),clrscr(),exit(),etc.)]
To Start Terminal press- Ctrl+Alt+t
To Write/Read Code type- $gedit FileName.c
To Complie and Run type- $gcc FileName.c -o AlternateFileName
                                              $./AlternateFileName

  • Input statement syntax= scanf("Format specifier",&variable);
  • Program for Addition
#include<stdio.h>
void main()
{
int a,b,c; 
printf("Enter 2 integer values  ");
scanf("%d %d",&a,&b);
                 //dont give space between last format specifer and double qoutes (%d")
// otherwise it will accept one more character from user
c=a+b;                                 
printf("Addition is %d",c);
}
 o/p=
             Enter 2 integer values 10 20
             Addition is 30






Monday, August 29, 2016

C Program for Multi Queue

Program-

// c program for multi Queue
#include<stdio.h>
#define MAX 10

struct s
{
int queue[10];
int rear,front;

}red,green,blue;

void insert(int);
void del(int);
void show();


void main()
{

int ch;
int temp;
red.rear=green.rear=blue.rear=0;
red.front=green.front=blue.front=0;

do
{
printf("\n\t Main menu");
printf("\n 1:Insert in red");
printf("\n 2:Insert in green");
printf("\n 3:Insert in blue");
printf("\n 4:Delete From red");
printf("\n 5:Delete From green");
printf("\n 6:Delete From blue");
printf("\n 7:Show");
printf("\n 8:Exit");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(1); break;
case 2: insert(2); break;
case 3: insert(3); break;
case 4: del(4); break;
case 5: del(5); break;
case 6: del(6); break;
case 7: show(); break;
case 8: temp=1; break;
default: printf("\n wrong choice");
}
}while(ch!=8 && temp!=1);
}

void insert(int no)
{
int item;
if(no==1)
{
if(red.rear==MAX)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
red.queue[red.rear]=item;
red.rear++;
}
}
if(no==2)
{
if(green.rear==MAX)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
green.queue[green.rear]=item;
green.rear++;
}
}
if(no==3)
{
if(blue.rear==MAX)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
blue.queue[blue.rear]=item;
blue.rear++;
}
}
}

void del(int no)
{
int item,i;
if(no==4)
{
if(red.front==red.rear)
printf("\n Underflow");
else
{
item=red.queue[red.front];
printf("\n Deleted element is %d",item);
red.front++;
}
}
if(no==5)
{
if(green.front==green.rear)
printf("\n Underflow");
else
{
item=green.queue[green.front];
printf("\n Deleted element is %d",item);
green.front++;
}
}
if(no==6)
{
if(blue.front==blue.rear)
printf("\n Underflow");
else
{
item=blue.queue[blue.front];
printf("\n Deleted element is %d",item);
blue.front++;
}
}
}

void show()
{
int i;
if(red.front==red.rear)
printf("\n red queue is empty");
else
{
printf("\n Elements of Red queue are \n");
for(i=red.front;i<red.rear;i++)
{
printf("%d\t",red.queue[i]);
}
}

if(green.front==green.rear)
printf("\n green queue is empty");
else
{
printf("\n Elements of green queue are \n");
for(i=green.front;i<green.rear;i++)
{
printf("%d\t",green.queue[i]);
}
}


if(blue.front==blue.rear)
printf("\n blue queue is empty");
else
{
printf("\n Elements of blue queue are \n");
for(i=blue.front;i<blue.rear;i++)
{
printf("%d\t",blue.queue[i]);
}
}
}


Output-


         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 1

 Enter the value 10

         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 1

 Enter the value 20

         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 1

 Enter the value 30

         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 2

 Enter the value 100

         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 2

 Enter the value 200

         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 7

 Elements of Red queue are
10      20      30
 Elements of green queue are
100     200
 blue queue is empty
         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 4

 Deleted element is 10
         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
 Enter Your Choice 7

 Elements of Red queue are
20      30
 Elements of green queue are
100     200
 blue queue is empty
         Main menu
 1:Insert in red
 2:Insert in green
 3:Insert in blue
 4:Delete From red
 5:Delete From green
 6:Delete From blue
 7:Show
 8:Exit
Enter Your Choice 8

C Program for Multi Stack

Program-

//Implementation of Multi Stack
#include<stdio.h>
#define MAX 10

struct s
{
 int stack[10];
 int top;
}red,green,blue;

void push(int);
void pop(int);
void show();


void main()
{
int ch;
red.top=green.top=blue.top=-1;
int temp;
do
{
printf("\n\t Main menu");
printf("\n 1:Push red");
printf("\n 2:Push green");
printf("\n 3:Push blue");
printf("\n 4:Pop red");
printf("\n 5:Pop green");
printf("\n 6:Pop blue");
printf("\n 7:Show");
printf("\n 8:Exit");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(1); break;
case 2: push(2); break;
case 3: push(3); break;
case 4: pop(4); break;
case 5: pop(5); break;
case 6: pop(6); break;
case 7: show(); break;
case 8: temp=1; break;
default: printf("\n wrong choice");
}
}while(ch!=8 && temp!=1);
}

void push(int no)
{
int item;
if(no==1)
{

if(red.top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
red.top++;
red.stack[red.top]=item;
}
}

if(no==2)
{

if(green.top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
green.top++;
green.stack[green.top]=item;
}
}

if(no==3)
{

if(blue.top==MAX-1)
printf("\n overflow");
else
{
printf("\n Enter the value ");
scanf("%d",&item);
blue.top++;
blue.stack[blue.top]=item;
}
}
}

void pop(int no)
{
int item;

if(no==4)
{

if(red.top==-1)
printf("\n Underflow");
else
{
item=red.stack[red.top];
printf("\n Deleted element is %d",item);
red.top--;
}
}
if(no==5)
{

if(green.top==-1)
printf("\n Underflow");
else
{
item=green.stack[green.top];
printf("\n Deleted element is %d",item);
green.top--;
}
}
if(no==6)
{

if(blue.top==-1)
printf("\n Underflow");
else
{
item=blue.stack[blue.top];
printf("\n Deleted element is %d",item);
blue.top--;
}
}
}

void show()
{
int i;

if(red.top==-1)
printf("\n red stack is empty");
else
{
printf("\n Elements of Red stack are \n");
for(i=0;i<=red.top;i++)
{
printf("%d\t",red.stack[i]);
}
}

if(green.top==-1)
printf("\n green stack is empty");
else
{
printf("\n Elements of green stack are \n");
for(i=0;i<=green.top;i++)
{
printf("%d\t",green.stack[i]);
}
}

if(blue.top==-1)
printf("\n blue stack is empty");
else
{
printf("\n Elements of blue stack are \n");
for(i=0;i<=blue.top;i++)
{
printf("%d\t",blue.stack[i]);
}
}
}

Output-


         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

 Enter the value 10

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

 Enter the value 20

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 1

 Enter the value 30

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 2

 Enter the value 100

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 2

 Enter the value 200

         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 7

 Elements of Red stack are
10      20      30
 Elements of green stack are
100     200
 blue stack is empty
         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 4

 Deleted element is 30
         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
 Enter Your Choice 7

 Elements of Red stack are
10      20
 Elements of green stack are
100     200
 blue stack is empty
         Main menu
 1:Push red
 2:Push green
 3:Push blue
 4:Pop red
 5:Pop green
 6:Pop blue
 7:Show
 8:Exit
Enter Your Choice 8

Thursday, August 11, 2016

C Program for Linked List With all Operations

Program:-

// C Program for Linked List With all Operations
  #include<stdio.h>
#include<stdlib.h>
struct link
{
int info;
struct link * next;
};
struct link * start=NULL;

void insertFirst();
void insertLast();
void insertPos();
void show();
void delFirst();
void delLast();
void delPos();

void main()
{
int ch,temp;
do
{
printf("\n\t Main menu");
printf("\n 1:insert at first");
printf("\n 2:insert at last");
printf("\n 3:insert at Specific Position");
printf("\n 4:Delete from First");
printf("\n 5:Delete from Last");
printf("\n 6:Delete from Specific Position");
printf("\n 7:Show");
printf("\n 8:Exit");
printf("\n Enter Your Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: insertFirst(); break;
case 2: insertLast(); break;
case 3: insertPos(); break;
case 4: delFirst(); break;
case 5: delLast(); break;
case 6: delPos(); break;
case 7: show(); break;
case 8: temp=1; break;
default: printf("\n wrong choice");
}
}while(ch!=8 && temp!=1);
}

void insertFirst()
{
struct link * temp;
int num;
temp=(struct link *)malloc(sizeof(struct link));
printf("Enter the number\n");
scanf("%d",&num);
temp->info=num;
temp->next=start;
start=temp;
}


void insertLast()
{
struct link * temp,* move;
int num;
temp=(struct link *)malloc(sizeof(struct link));
printf("Enter the number\n");
scanf("%d",&num);
temp->info=num;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
move=start;
while(move->next!=NULL)
{
move=move->next;
}
move->next=temp;
}
}

void insertPos()
{
struct link * temp,* move,* back;
int num,pos,i;
temp=(struct link *)malloc(sizeof(struct link));
printf("Enter the Position to Insert The no.\n");
scanf("%d",&pos);
printf("Enter the number\n");
scanf("%d",&num);
temp->info=num;
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
move=start;
for(i=0;i<pos-1;i++)
{
back=move;
move=move->next;
}
temp->next=back->next;
back->next=temp;

}
}


void delFirst()
{
struct link * move;
int item;
if(start==NULL)
printf("link list is empty");
else
{
item=start->info;
start=start->next;
printf("item deleted=%d",item);
}
}


void delLast()
{
struct link * back,* move;
int item;
if(start==NULL)
printf("link list is empty");
else
{
move=start;
if(move->next==NULL)
{
item=move->info;
start=NULL;
}
else
{

while(move->next!=NULL)
{
back=move;
move=move->next;
}
item=move->info;
back->next=NULL;
}
printf("item deleted=%d",item);
}
}

void delPos()
{
struct link * move,* back;
int num,pos,i;

printf("Enter the Position From which to Delete The no.\n");
scanf("%d",&pos);
if(start==NULL)
printf("list is empty");
else
{
move=start;
for(i=0;i<pos-1;i++)
{
back=move;
move=move->next;
}

num=move->info;
back->next=move->next;
printf("item deleted=%d",num);
}
}


void show()
{
struct link * temp,* move;

if(start==NULL)
printf("list is empty");
else
{
move=start;
printf("Elements are ");
while(move->next!=NULL)
{
printf("%d ",move->info);
move=move->next;
}
printf("%d\n",move->info);
}

}



Output:-

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 2
Enter the number
10

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 2
Enter the number
20

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 2
Enter the number
30

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 7
Elements are 10 20 30

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 1
Enter the number
40

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 7
Elements are 40 10 20 30

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 3
Enter the Position to Insert The no.
3
Enter the number
50

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 7
Elements are 40 10 50 20 30

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 4
item deleted=40
Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 7
Elements are 10 50 20 30

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 5
item deleted=30
Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 7
Elements are 10 50 20

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 6
Enter the Position From which to Delete The no.
2
item deleted=50
Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 7
Elements are 10 20

Main menu
 1:insert at first
 2:insert at last
 3:insert at Specific Position
 4:Delete from First
 5:Delete from Last
 6:Delete from Specific Position
 7:Show
 8:Exit
 Enter Your Choice 8