Search This Blog

Thursday 28 January 2016

Write a C++ program using class Rectangle which contains length and width as data members and two member functions – getdata and area. The getdata function takes length and width as an input and the area function ccalculates the area and display it on the screen. Use Scope Resolution Operator.

#include<iostream>
using namespace std;

class Rectangle
{
int length,width;

public:

void getdata();

void area();

};

void Rectangle::getdata()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter width:";
cin>>width;
}
void Rectangle::area()
{
cout<<"Area is:"<<length*width;
}
int main()
{
Rectangle r;
r.getdata();
r.area();
return 0;
}

Writa a C++ program using class which contains two member functions getdata and putdata to read the information(name and age) of the person and disply it on the screen respectively. Use Scope Resolution Operator

#include<iostream>
using namespace std;

class Person
{
      int age;
      char name[10];

      public:

      void getdata();

      void putdata();
};

void Person::getdata()
{
cout<<"\nEnter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void Person::putdata()
{
cout<<"\nName:"<<name<<endl;
cout<<"Age :"<<age<<endl;
}
int main()
{
Person p;
p.getdata();
p.putdata();
return 0;
}

Write a program to demonstrate the use of Scope Resolution Operator:: with variable name.

Without Class Example:

#include <iostream.h>

char n = 'k';   // A global variable

int main() {

   char n = 'g';   // A local variable
   cout  << ::n << endl;  // Print the global variable: k
   cout  << n   << endl;  // Print the local variable: g
}




With Class Example

#include <iostream.h>

class gajjar {
public:
  void output();
};

void gajjar::output() {
  cout << "Outside the class.\n";
}

int main() {
  gajjar x;
  x.output();

  return 0;

}

Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet.

#include<iostream.h>

using namespace std;
class gallon
{
float g;
public:

void data()
{
cout<<"\nEnter number of gallon:";
cin>>g;
}

void feet()
{
cout<<"\nEquivalent cubic feet is:"<<g/7.481;
}
};
int main()
{
gallon ans;
ans.data();
ans.feet();

return 0;
}

Write a C++ program, that will ask a temperature in Fahrenheit and display in Celsius using a class called temp and member functions.

#include<iostream>

using namespace std;

class temp
{
float feren,celc;
public:

void data()
{
cout<<"Enter Temp in fahrenheit:";
cin>>feren;
}

void convert()
{
celc=(0.56)*(feren-32);
cout<<"Temp in celsius is:"<<celc;
}
};

int main()
{
temp t;
t.data();
t.convert();
return 0;
}

Write a C++ program that will ask a temperature in Fahrenheit and display in Celsius without using class

#include<iostream>

using namespace std;

int main()
{
float feren,celc;

cout<<"Enter Temp in fahrenheit:";
cin>>feren;

celc=(0.56)*(feren-32);

cout<<"Temp in celsius is:"<<celc;

return 0;
}

An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a C++ program to read the ballots and count the votes cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, the ballot should be considered as a 'spolit ballot', and the program should also count the number of spoilt ballots.

#include<iostream>

using namespace std;

int main()
{
int n,x=0,i,ch,one=0,two=0,three=0,four=0,five=0;
cout<<"Enter total num of students";
cin>>n;

for(i=0;i<n;i++)
{
cout<<"\nEnter votes for \n1 \n2 \n3 \n4 \n5\nEnter vote for:";
cin>>ch;

switch(ch)
{
case 1:one++;
break;

case 2:two++;
break;

case 3:three++;
break;

case 4:four++;
break;

case 5:five++;
break;

default:x++;
}

}


cout<<"\nvotes given to person 1 is:"<<one<<endl;
cout<<"\nvotes given to person 2 is:"<<two<<endl;
cout<<"\nvotes given to person 3 is:"<<three<<endl;
cout<<"\nvotes given to person 4 is:"<<four<<endl;
cout<<"\nspoiled vote are:"<<x;

return 0;
}

Write a program to read the values of a, b and c and display the value of x, where x = a/(b-c)

#include<iostream>

using namespace std;

int main()
{
int a,b,c,x;
cout<<"Enter a b & c for x=a/(b-c)"<<endl;
cin>>a>>b>>c;
if(b-c==0)
{
cout<<"Division not possible";
}
else
{
x=a/(b-c);
cout<<"x is:"<<x;
}
return 0;
}

Writa a C++ program to input an integer value from the keyboard and display the message "Good Morning" that many times on screen.

#include<iostream>

using namespace std;

int main()
{
int n;
cout<<"Enter Num:";
cin>>n;
int i;
for(i=0;i<n;i++)
{
cout<<"Good Morning!"<<endl;
}
return 0;
}

Monday 25 January 2016

Write a program which calculates volume of cube, cylinder, and rectangular box. (Use function overloading).

Practical 8


#include<iostream.h>
using namespace std;

int volume(int s)
{
    return s*s*s;
}
double volume(double r,int h)
{
    return 3.14*r*r*h;
}
float volume(float l,int b,int h)
{
    return l*b*h;
}
int main()
{
    int x,s,h,b;
    double y,r;
    float z,l;
    cout<<"\nEnter the value of s for cube:";
    cin>>s;
    x=volume(s);
    cout<<"\nVolume of cube is:"<<x<<endl;
    cout<<"\nEnter the value of r & h for cylinder:";
    cin>>r>>h;
    y=volume(r,h);
    cout<<"Volume of cylender is"<<y<<endl;
    cout<<"\nEnter valu of l,b & h:";
    cin>>l>>b>>h;
    z=volume(l,b,h);
    cout<<"\nVolume of rectangle:"<<z;
    return 0;
}



output:

enter the value f s for cube:2
volume of cube is 8

enter the value of r & h for cylinder
10
10
volume of cylinder is 314

enter the value of l b h
2
4
2
volume of rectangle is 16

Saturday 23 January 2016

Sorting of Array in C++

#include<iostream>

using namespace std;

void sort(int a[],int n);                                    // call by reference is used
int max(int a[],int n);                                     
// call by reference is used 
 
int main()
{
    int n;
    cout << "Enter size of array" << endl;
    cin >> n;
    int a[n];                                                       // Dynamic Initialization of array
    for(int i=0;i<n;i++)
        cin >> a[i];
    sort(a,n);
    cout << "Sorted array:" << endl;
    for(int i=0;i<n;i++)
        cout << a[i] << endl;
    return 0;
}

void sort (int a[],int n)
{
    int temp,ind;
    for(int i=n-1;i>0;i--)
    {
        ind=max(a,i);
        temp=a[ind];
        a[ind]=a[i];
        a[i]=temp;
    }   
}

int max(int a[],int n)
{
    int max=0,ind;
    for(int i=0;i<=n;i++)
    {
        if(max<a[i])
        {
            max=a[i];
            ind=i;
        }
    }
    return ind;
}

Function Overloading In C++

#include<iostream>

using namespace std;

void power(double m,int n=2);                //default value of n is set to 2
void power(int m,int n=2);                      
//default value of n is set to 2
 
int main()
{
    int m,n;
    double dm;
    cout << "Enter the integer value of m"<<endl;
    cin >> m;
    cout << "Enter the double value of m"<<endl;
    cin >> dm;
    cout << "Enter the integer value of n"<<endl;
    cin >> n;
    power(dm,n);
    power(m,n);
    power(dm);                                            //default value from prototype is used
    power(m);   
                                           //default value from prototype is used
 }

void power(double m,int n)
{
    double r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}

void power(int m,int n)
{
    int r=m;
    for(int i=1;i<n;i++)
    r=r*m;
    cout << "Value of "<< m <<" raise to "<< n <<" is "<<r<<endl;
}

Write a program to swap two variables using function.(call by value & call by reference).

 Practical 7

Call By value:

#include<iostream.h>
void swap(int x, int y);

int main ()
{
   int a  100;
   int b = 200;

   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

   return 0;
}
void swap(int x, int y)
{
   int temp;

   temp = x;
   x = y;
   y = temp;

   return;
}



output:


Before swap, value of a :100
Before swap, value of b : 200


After swap, value of a :100
After swap, value of b : 200



Call by Reference:

#include<iostream.h>
void swap(int &x, int &y);
int main ()
{
   int a = 100;
   int b = 200;

   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

   return 0;
}
void swap(int &x, int &y)            //no need to use pointer ! in c++ we can pass address like this
{
   int temp;

   temp = x;
   x = y;
   y = temp;

   return;
}




output:


Before swap, value of a :100
Before swap, value of b : 200


After swap, value of a :200
After swap, value of b : 100

Write a program to demonstrate the use of Manipulators (setw () and endl).

Practical 6


Endl

#include<iostream.h>


int main()
{

 char name[10];
 cout<<"Enter your name: ";                      //same line it will get your name
 cin>>num;

 cout<<"Hello, "<<name<<endl;                //next line will be printed in new line
 cout<<"Welcome to www.coscet.blogspot.in!!";
 getch();
 return 0;

}



output:

hello
welcome to www.coscet.blogspot.in


setw

Which basically set the width of field:
It is included in iomanip file

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

int main()
{
 int num1,num2,num3;
 cout<<"Enter three numbers:\n";
 cin>>num1>>num2>>num3;

 cout<<"\nDisplaying the three numbers\n"
     <<"Num1:"<<setw(8)<<num1<<endl
     <<"Num2:"<<setw(8)<<num2<<endl      //field set is 8
     <<"Num3:"<<setw(8)<<num3<<endl;     //the output will be:    1234 !!

 getch();
 return 0;

}



output:
        1234



Another Example:

#include<iostream.h>
#include<iomanip.h>

int main()
{
     cout<<setw(10);
     cout<<"karan"<<endl;

     return 0;
}

Write a program to demonstrate the use of Scope Resolution Operator:: with variable name.

Practical 5


Without Class Example:

#include <iostream.h>

char n = 'k';   // A global variable

int main() {

   char n = 'g';   // A local variable
   cout  << ::n << endl;  // Print the global variable: k
   cout  << n   << endl;  // Print the local variable: g
}


output:

k
g



With Class Example

#include <iostream.h>

class gajjar {
public:
  void output();
};

void gajjar::output() {
  cout << "Outside the class.\n";
}

int main() {
  gajjar x;
  x.output();

  return 0;

}



Write a program to arrange an array of N elements into ascending order.

Practical 4

#include<iostream.h>
#include <conio.h>

int main()
{
    int i, j, a, n, num[30];

    cout<<"Enter the value of n:";
    cin>>n;
    cout<<"Enter the numbers:";
    for (i = 0; i < n; ++i)
        cin>>num[i];
    for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (number[i] > number[j])
            {
                a =  num[i];
                num[i] = num[j];
                num[j] = a;
            }
        }
    }
    cout<<"Ascending order:";
    for (i = 0; i < n; ++i)
       cout<<num[i];
}


output:

enter the value of n
4
enter numbers
1
5
4
3

ascending order is
1
3
4
5

Write a program to find the sum of the digits of a integer constant.

 Practical 3

#include<iostream.h>
#include<conio.h>
int main()
{
int num,sum=0;
cout<<"Enter an integer:";
cin>>num;

while(num!=0)
{
sum+=num%10;
num=num/10;

}

cout<<sum;

return 0;
}


output :
enter an integer :151
7

Write a program that will allow computer to be used as an ordinary calculator. Consider only common arithmetic operations.(+, -, *, / )

Practical 2

#include<iostream.h>
#include<conio.h>

int main()
{
int a,b;
cout<<"Enter value of a:";
cin>>a;

cout<<"Enter value of b:";
cin>>b;
int c=1;

while(c==1)
{
int choice;
cout<<"1.Add\n2.subtract\n3.mul\n4.div"<<endl;
cin>>choice;
switch(choice)
{
case 1:cout<<"Addition is:"<<a+b<<endl;
break;

case 2:cout<<"Subtraction is:"<<a-b;
break;

case 3:cout<<"multiplication is:"<<a*b;
break;

case 4:cout<<"Division is:"<<a/b;
break;

default:cout<<"Enter proper choice!!!"<<endl;
}
cout<<"Do you want to continue? (0/1):";
cin>>c;
}
return 0;

}



output:
Enter value of a
5

Enter value of b
5

1.Add
2.subtract
3.mul
4.div
1
addition is 10
do you want to continue?
0

Tuesday 19 January 2016

Simple Function overloading in C++

 In C++ we are allowed to make only one function in class and we can use it with different ways.
just look over an example below I have created class name gajjar and in main its object is k I've used function karan() to overload in different requirements!

#include<iostream.h>
#include<conio.h>
class gajjar
{
    public:
    void karan(int i,int j)
    {
        cout<<"Addition is:"<<i+j<<endl;
    }
    void karan(float k)
    {
        cout<<"Division by 2 is:"<<k/2<<endl;
    }
    void karan(char k[])
    {
        cout<<"Hi "<<k<<endl;
    }
};
void main()
{
    gajjar k;
    cout<<"\nEnter two values to add:";
    int a,b;
    cin>>a;
    cin>>b;

    k.karan(a,b);

    cout<<"\nEnter float value to divide by 2:";

    float z;
    cin>>z;
    k.karan(z);

    char str[10];
    cout<<"\nEnter your first name:\n";
    cin>>str;
    k.karan(str);
    getch();
}

Monday 18 January 2016

Operator Overloading in C++

#include<iostream.h>
class KARAN
{
private:
int k;

public:
KARAN():k(5)
{
}

void operator ++()
{
k=k+1;
}

void Display()
{
cout<<"Value is: "<<k;
}
};
int main()
{
KARAN g;
++g; /* operator function void operator ++() is called */
g.Display();
return 0;
}

Cipher text example with file handling

 Simple Cipher text with file Handling
Input : hello karan gajjar
key : 3
Output : khoor ndudq jdmmdu

As we know encryption and decryption. I have kept input in encode.txt file and aftyer entering key value the decrypted text will be stored in decode.txt file!

CODE:

 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,key,k;
    char str[100],str2[100];
    FILE *fp,*fq;
    fp=fopen("encode.txt","r");
    fgets(str,sizeof(str),fp);
    puts(str);
    fq=fopen("decode.txt","a");
    clrscr();
    printf("Enter Key:");
    scanf("%d",&key);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]>=97 && str[i]<120)
        {
            str[i]=str[i]+key;
            str2[i]=str[i];
        }
        else if(str[i]==32)
        {
            str2[i]=str[i];
        }
        else
        {
            str[i]=str[i]-26+key;
            str2[i]=str[i];
        }
    }
    str2[i]='\0';
    puts(str2);
    fprintf(fq,"\n");
    fprintf(fq,str2);
    fclose(fq);
    fclose(fp);
    getch();
}


NOTE: You can add more elseif condition for "," , "!" , "?" this type of ascii value i have added for "space".
              and yes in this example you can't add encoded message with every new line!

Wednesday 13 January 2016

Queue using Linked List

/*Queue using Linked List*/

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

    typedef struct queue
    {

    int data;
    struct queue *next;

    }node;

    node *rear=NULL;
    node *front=NULL;
    node *getnode()
    {

    int val;
    node *temp;
    printf("Enter val:");
    scanf("%d",&val);
    temp=(node *)malloc(sizeof(node));
    temp->data=val;
    temp->next=NULL;
    return temp;

    }
    void insert()
    {

    node *neww;
    neww=getnode();
    if(front==NULL)
    {
         front=rear=neww;
    }
    else
    {
         rear->next=neww;
         rear=neww;
    }

    }
    void del()
    {

    if(front==NULL)
    {
         printf("\nQueue is Empty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n%d is deleted\n",front->data);
         front=front->next;
         t->next=NULL;
         free(t);
    }

    }
    void display()
    {

    if(front==NULL)
    {
          printf("\nEmpty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n");
         while(t)
         {
              printf("%d ",t->data);
              t=t->next;
         }
         printf("\n");
    }

    }
    void main()
    {

    int ch;
    clrscr();
    while(1)
    {
    printf("\n1.insert\n2.delete\n3.display\n4.exit\nYour choice:");
    scanf("%d",&ch);

    switch(ch)
    {
    case 1:insert();
    break;

    case 2:del();
    break;

    case 3:display();
    break;

    case 4:exit(0);
    break;

    default:printf("\nProper choice plz!\n");
    }

    }
    }

Stack Using Liked List

/* Stack Using Linked List*/

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

typedef struct stack
{

int data;
struct stack *next;

}node;
node *top=NULL;

node *getnode()
{

int val;
node *temp;
printf("Enter val");
scanf("%d",&val);
temp=(node *)malloc(sizeof(node));
temp->data=val;
temp->next=NULL;
return temp;

}
void push()
{

node *neww;
neww=getnode();
neww->next=top;
top=neww;

}
void pop()
{

if(top==NULL)
{
      printf("\nStack is Empty!\n");
}
else
{
     node *t;
     t=top;
     printf("\n%d is deleted\n",top->data);
     top=top->next;
     t->next=NULL;
     free(t);
}

}
void peep()
{

     printf("\nTop element is %d",top->data);
}

void display()
{

if(top==NULL)
{
     printf("\nEmpty!\n");
}
else
{
     node *t;
     t=top;
     while(t)
     {
          printf("\n%d",t->data);
          t=t->next;
     }
     printf("\n");
}

}
void main()
{

int ch;
clrscr();
while(1)
{
printf("\n1.push\n2.pop\n3.peep\n4.display\n5.exit\nYour choice:");
scanf("%d",&ch);

switch(ch)
{
case 1:push();
break;

case 2:pop();
break;

case 3:peep();
break;

case 4:display();
break;

case 5:exit(0);
break;

default:printf("\nProper choice plz!\n");
}
}

}

Tree (Recursive – inorder , preorder , postorder)

/*Tree (Recursive – inorder , preorder , postorder)*/

    #include<stdio.h>
    #include<conio.h>
    typedef struct tree{

    struct tree *right;
    struct tree *left;
    int data;

    }node;

    void inorder(node *root)
    {

    if(root!=NULL)
    {
          inorder(root->left);
          printf("%d ",root->data);
          inorder(root->right);
    }

    }
    void preorder(node *root)
    {

    if(root!=NULL)
    {
         printf("%d ",root->data);
         preorder(root->left);
         preorder(root->right);
    }

    }
    void postorder(node *root)
    {
    if(root!=NULL)
    {

    postorder(root->left);
    postorder(root->right);
    printf("%d ",root->data);
    }

    }
    node *getnode()
    {

    int val;
    node *temp;
    printf("\nEnter data:");
    scanf("%d",&val);
    temp=(node *)malloc(sizeof(node));
    temp->data=val;
    temp->right=NULL;
    temp->left=NULL;
    return temp;

    }
    node *insert(node *root)
    {

    char ch;
    node *neww;
    if(root==NULL)
    {
         neww=getnode();
         root=neww;
    }
    printf("\n\Ok now next node where to add?(L/R):");
    scanf("\n%c",&ch);
    if(ch=='l' || ch=='L')
    {
         neww=getnode();
         if(root->left==NULL)
         {
               root->left=neww;
         }
         else
         {
               insert(root->left);
         }
    }
    else if(ch=='r' || ch=='R')
    {
          neww=getnode();
          if(root->right==NULL)
          {
                root->right=neww;
          }
          else
          {
               insert(root->right);
          }
    }
    else
    {
          printf("\nEnter proper choice");
    }
    return root;

    }
    void main()
    {

    int ch;
    node *s=NULL;
    clrscr();
    while(1)
    {
    printf("\n1.insert\n2.inorder\n3.preorder\n4.postorder\n5.exit\n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:s=insert(s);
    break;

    case 2:inorder(s);
    break;

    case 3:preorder(s);
    break;

    case 4:postorder(s);
    break;

    case 5:exit(0);
    break;

    default:printf("\nEnter proper choice\n");
    }
    }

    }

Priority Queue

/* Priority Queue */

    #include<stdio.h>
    #include<conio.h>
    #define size 10
    struct queue
    {

    int Q[size];
    int front,rear;

    }q;
    int full()
    {

    if(q.rear==size-1)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }
    int empty()
    {

    if(q.front>q.rear)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }
    void insert(int x)
    {

    int j;
    j=q.rear;
    ++q.rear;
    while(j>=0 && q.Q[j]>=x)
    {
         q.Q[j+1]=q.Q[j];
         j--;
    }
    q.Q[j+1]=x;

    }
    void del()
    {

         int x;
         x=q.Q[q.front];
         q.front++;
    }

    void display()
    {

    int i;
    for(i=q.front;i<=q.rear;i++)
    {
         printf("%d ",q.Q[i]);
    }

    }
    void main()
    {

    int ch,val;
    q.rear=-1;
    q.front=0;
    clrscr();
    while(1)
    {
         printf("\n1.insert\n2.delete\n3.display\n4.exit:");
         scanf("%d",&ch);

         switch(ch)
         {
              case 1:if(full())
              {
                    printf("\nFULL!");
              }
              else
              {
                    printf("\nEnter data");
                    scanf("%d",&val);
                    insert(val);
              }
              break;

              case 2:if(empty())
              {
                     printf("\Aalready empty\n");
              }
              else
              del();
              break;

              case 3:
              if(empty())
              {
                    printf("\nalready empty\n");
              }
              else
              display();
              break;

              case 4:exit(0);
              break;

             default:printf("\nproper choice plz!\n");
        }
    }

    }

Doubly Linked List

/*Doubly Linked List*/

    #include<stdio.h>
    #include<conio.h>
    typedef struct dll{

    int data;
    struct dll *next;
    struct dll *prev;

    }node;

    node *getnode()
    {

    int item;
    node *temp;
    temp=(node *)malloc(sizeof(node));
    printf("\nenter data:");
    scanf("%d",&item);
    temp->data=item;
    temp->next=NULL;
    temp->prev=NULL;
    return temp;

    }

    node *create()
    {

    int ch;
    node *trv,*start,*neww;
    start=trv=NULL;

    do
    {
         neww=getnode();
         if(start==NULL)
         {
              start=neww;
              trv=start;
         }
         else
         {
              trv->next=neww;
              neww->prev=trv;
              trv=trv->next;
         }

         printf("\nEnter any key to continue & 0 for stop:");
         scanf("%d",&ch);
    }while(ch!=0);
    return start;

    }
    node *insert_first(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();
    if(start==NULL)
    {
         start=neww;
    }
    else
    {
         neww->next=start;
         start->prev=neww;
         start=neww;
    }
    return start;

    }

    node *insert_last(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();
    if(start==NULL)
    {
         start=neww;
    }
    else
    {
         while(trv->next!=NULL)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->prev=trv;
    }
    return start;

    }
    node *del_first(node *start)
    {

    node *trv;
    trv=start;
    start=start->next;
    start->prev=NULL;
    free(trv);
    return start;

    }
    node *del_last(node *start)
    {

    node *trv,*p;
    trv=start;
    if(start==NULL)
    {
         printf("\nEmpty\n");
    }
    else if(start->next==NULL)
    {
         free(start);
         start=NULL;
    }
    else
    {
         while(trv->next!=NULL)
         {
              p=trv;
              trv=trv->next;
         }
    }
    p=trv->prev;
    p->next=NULL;
    free(trv);
    return start;

    }
    void display(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
         printf("\nNo node to display\n");
    }
    printf("\n");
    while(trv!=NULL)
    {
         printf("%d ",trv->data);
         trv=trv->next;
    }

    }

    void main()
    {

    node *f=NULL,*l=NULL;
    node *s=NULL;
    int ch;
    clrscr();
    while(1)
    {
    printf("\n1.Create\n2.Insert at front\n3.Insert at last\n4.Delete at front\n5.Delete at last\n6.Display\n10.exit\nYour choice:");
    scanf("%d",&ch);

         switch(ch)
         {
         case 1:s=create();
         break;

         case 2:s=insert_first(s);
         break;

         case 3:s=insert_last(s);
         break;

         case 4:s=del_first(s);
         break;

         case 5:s=del_last(s);
         break;

         case 6:display(s);
         break;

         case 10:exit(0);
         break;

         default:printf("\nEnter proper choice\n");
         }
    }

    }

Circular Linked List

/* Circular Linked List*/

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

    typedef struct cll{

    int data;
    struct cll *next;

    }node;

    node *getnode()
    {

    node *temp;
    int item;
    printf("\nEnter data:");
    scanf("%d",&item);
    temp=(node *)malloc(sizeof(node));
    temp->data=item;
    temp->next=NULL;
    return temp;

    }

    node *create()
    {

    int ch;
    node *start,*neww,*trv;
    start=NULL;
    trv=NULL;
    do
    {
         neww=getnode();
         if(start==NULL)
         {
              neww->next=neww;
              start=neww;
              trv=start;
          }
          else
          {
               trv->next=neww;
               neww->next=start;
               trv=trv->next;
          }

    printf("\nEnter any key to add & 0 for stop:");
    scanf("%d",&ch);

    }
    while(ch!=0);
    return start;

    }

    node *insert_first(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();

    if(start==NULL)
    {
         start=neww;
         neww->next=neww;
    }
    else
    {
         while(trv->next!=start)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->next=start;
         start=neww;
         }
     return start;

    }
    void insert_last(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();
    if(start==NULL)
    {
          neww=start;
          neww->next=neww;
    }
    else
    {
         while(trv->next!=start)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->next=start;
    }

    }
    node *del_first(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
         printf("\n\Empty\n");
    }
    else if(start->next==start)
    {
         start->next=NULL;
         free(start);
         start=NULL;
    }
    else
    {
          while(trv->next!=start)
          {
              trv=trv->next;
          }
          trv->next=start->next;
          trv=start;
          start=start->next;
          free(trv);
    }
    return start;

    }
    node *del_last(node *start)
    {

    node *trv,*temp;
    trv=start;
    if(start==NULL)
    {
         printf("\n\Empty\n");
    }
    else if(start->next==start)
    {
          start->next=NULL;
          free(start);
          start=NULL;
    }
    else
    {
          while((trv->next)->next!=start)
          {
               trv=trv->next;
          }
          temp=trv->next;
          trv->next=start;
          free(temp);
    }
    return start;

    }
    void display(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
        printf("\nEmpty !\n");
    }
    printf("\n%d ",trv->data);
    trv=trv->next;

    while(trv!=start)
    {
          printf("%d ",trv->data);
          trv=trv->next;
    }

    }

    void main()
    {

    int ch;
    node *s=NULL;
    clrscr();
    while(1)
    {
         printf("\n1.Create\n2.insert_first\n3.insert_last\n4.delete           first\n5.delete_last\n6.Display\n10.exit\nYour choice:");
         scanf("%d",&ch);

         switch(ch)
         {
              case 1:s=create();
              break;

              case 2:s=insert_first(s);
              break;

              case 3:insert_last(s);
              break;

              case 4:s=del_first(s);
              break;

              case 5:s=del_last(s);
              break;

              case 6:display(s);
              break;

              case 10:exit(0);
              break;
    
              default:printf("\nEnter proper choice\n");
         }
    }

    }

Linked List


/* Linked List all operations */

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

    typedef struct ll
    {

         struct ll *next;
         int data;

    }node;

    node *getnode()
    {

         int item;
         node *temp;
         temp=(node *)malloc(sizeof(node));
         printf("\nenter data:");
         scanf("%d",&item);
         temp->data=item;
         temp->next=NULL;
         return temp;

    }

    node *create()
    {

         int ch;
         node *trv,*start,*neww;
         start=trv=NULL;
         do
         {
              neww=getnode();
              if(start==NULL)
              {
                   start=neww;
                   trv=start;
              }
              else
              {
                   trv->next=neww;
                   trv=trv->next;
              }
              printf("\nEnter any key to continue & 0 for stop:");
              scanf("%d",&ch);
         }
         while(ch!=0);
         return start;

    }

    node *insert_before_val(node *start)
    {

    node *neww,*trv,*prv;
    int val;
    if(start==NULL)
    {
         printf("you should atleast create link list");
    }
    else
    {
         trv=start;
         printf("\nEnter value before you want to add new value:");
         scanf("%d",&val);
         while(trv->data!=val && trv!=NULL)
         {
              prv=trv;
              trv=trv->next;
         }
         if(trv==NULL)
         {
              printf("make sure that %d is there",val);
         }
         else
         {
              neww=getnode();
              neww->next=prv->next;
              prv->next=neww;
         }
    }

    }

    int search(node *start,int ele)
    {

    int pos;
    node *trv;
    pos=0;
    trv=start;
    while(trv!=NULL)
    {
         pos++;
         if(trv->data==ele)
         {
              break;
         }
         trv=trv->next;
    }
    return pos;

    }

    int count(node *start)
    {

    int c=0;
    node *trv;
    trv=start;
    while(trv!=NULL)
    {
          c++;
          trv=trv->next;
    }
    return c;

    }

    void insert_after_val(node *start)
    {
    node *trv,*neww;
    int val;
    trv=start;
    if(start==NULL)
    {
    printf(“atleast create a node”);
    }
    else
    {
    printf(“\nEnter value after you want to add element\n”);
    scanf(“%d”,&val);
    while(trv->data!=val && trv!=NULL)
    {
    trv=trv->next;
    }
    if(trv==NULL)
    {
    printf(“\nmake sure %d is there\n”,val);
    }
    else
    {
    neww=getnode();
    neww->next=trv->next;
    trv->next=neww;
    }
    }

    }
    node *insert_first(node *start)
    {

    node *neww;
    neww=getnode();
    if(start==NULL)
    {
         neww=start;
    }
    else
    {
         neww->next=start;
         start=neww;
    }
    return start;

    }
    node *insert_last(node *start)
    {
    node *neww,*trv;
    neww=getnode();
    if(start==NULL)
    {
    start=neww;
    }
    else
    {
    trv=start;
    while(trv->next!=NULL)
    {
    trv=trv->next;
    }
    trv->next=neww;
    }
    return start;
    }
    node *del_first(node *start)
    {
    node *trv;
    trv=start;
    if(start==NULL)
    {
    printf(“\nAlready empty\n”);
    }
    printf(“\n%d is deleted\n”,start->data);
    start=start->next;
    free(trv);
    return start;
    }
    void *del_last(node *start)
    {
    node *trv,*prv;
    trv=start;
    if(start==NULL)
    {
    printf(“\nEmpty!\n”);
    }
    else
    {

    while(trv->next!=NULL)
    {
    prv=trv;
    trv=trv->next;
    }
    printf(“%d is deleted”,trv->data);
    prv->next=NULL;
    free(trv);
    }
    }
    void display(node *start)
    {
    node *trv;
    trv=start;
    if(start==NULL)
    {
    printf(“\nNo node to display\n”);
    }
    printf(“\n”);
    while(trv!=NULL)
    {
    printf(“%d “,trv->data);
    trv=trv->next;
    }

    }
    void main()
    {
    int ch,c,p,ele;
    node *s=NULL;
    clrscr();
    while(1)
    {
    printf(“\n\n1.Create\n2.Insert front\n3.insert last\n4.Delete first\n5.Delete last\n6.display\n7.insert before value\n8.insert after val\n9.count\n10.search\n15.exit:”);
    scanf(“%d”,&ch);

    switch(ch)
    {
    case 1:s=create();
    break;

    case 2:s=insert_first(s);
    break;

    case 3:s=insert_last(s);
    break;

    case 4:s=del_first(s);
    break;

    case 5:del_last(s);
    break;

    case 6:display(s);
    break;

    case 7:insert_before_val(s);
    break;

    case 8:insert_after_val(s);
    break;

    case 9:c=count(s);
    printf(“\ntotal node are %d\n”,c);
    break;

    case 10:
    printf(“\nEnter element to search:”);
    scanf(“%d”,&ele);
    p=search(s,ele);
    printf(“%d found at %d position”,ele,p);
    break;

    case 15:exit(0);
    break;

    default:printf(“\nEnter proper choice\n”);
    }
    }
    }

Circular Queue

/*Circular Queue -*/

    #include<stdio.h>
    #include<conio.h>
    # define size 5

    struct queue
    {

         int rear,front,Q[size];

    }q;

    void insert(int item)
    {

        
    if ((q.front == 0 && q.rear == size-1) || (q.front == q.rear+1))
    {
         printf("\nQueue Overflow\n");
         return;
    }

    

        
    if (q.front == -1)   
    {
         q.front = 0;
         q.rear = 0;
    }
    else
    {
         if (q.rear == size - 1)
              q.rear = 0;
         else
              q.rear = q.rear + 1;
    } 
    q.Q[q.rear] = item ;

    }

    void del()
    {

    if (q.front == -1)
    {
         printf("Queue Underflow\n");
         return ;
    }

    printf("\nDeleted element is %d\n",q.Q[q.front]);

    if (q.front == q.rear)
    {
         q.front = -1;
         q.rear = -1;
    }

    else
    {

         if (q.front == size - 1)
              q.front = 0;
         else
              q.front = q.front + 1;
    }

    }

    void display()
    {

        
    int i = q.front, j=q.rear;

    if (q.front == -1)    
    {
         printf("Queue is empty");
         return;
    }

    printf("\nQueue is :\n");

    if (i <= j)
    {
         while (i <= j)
         {
              printf("%d ",q.Q[i]);
              i++;
         }
    }
    else
    {
         while (i <= size - 1)
         {
              printf("%d ",q.Q[i]);
              i++;
         }

         i = 0;
         while (i <= j)
         {
              printf("%d ",q.Q[i]);
              i++;
         }
    }

    }
    void main()
    {

    
    int choice,data;
    q.front=-1;
    q.rear=-1;

    while(1)
    {
         printf("\n1.Insert\n2.Delete\n3.display\n4.exit\n");
         scanf("%d",&choice);

         switch(choice)
         {
              case 1:printf("\nEnter data\n");
              scanf("%d",&data);
              insert(data);
              break;

              case 2:del();
              break;

              case 3:display();
              break;

              case 4:exit(0);
              break;

              default:printf("\nEnter proper choice\n");
    }

    }
    getch();
    }

Simple Queue using array

/* Simple Queue using array */

    #include<stdio.h>
    #include<conio.h>
    #define size 5

    struct Queue{

    int rear,front,Q[size];

    }q;

    int qfull()
    {

    if(q.rear>=size-1)
    {
    return 1;
    }
    else
    {
    return 0;
    }

    }

    int qempty()
    {

    if(q.front==-1 || q.front>q.rear)
    {
    return 1;
    }
    else
    {
    return 0;
    }

    }

    void insert(int x)
    {

    if(q.front==-1)
    {
    q.front++;
    }
    q.rear++;
    q.Q[q.rear]=x;

    }

    int del()
    {

    int i;
    i=q.Q[q.front];
    q.front++;
    return i;

    }

    void display()
    {

    int i;
    printf("\n");
    for(i=q.front;i<=q.rear;i++)
    {
    printf("%d ",q.Q[i]);
    }
    printf("\n");

    }

    void main()
    {

    int choice,value,d;
    q.front=-1;
    q.rear=-1;

    while(1)
    {
    printf("\n1.Insert\n2.delete\n3.display\n4.exit\nEnter your choice:\n");
    scanf("%d",&choice);

    switch(choice)
    {

    case 1:if(qfull())
    {
    printf("\nQueue is full\n");
    }
    else
    {
    printf("\nEnter data\n");
    scanf("%d",&value);
    insert(value);
    }
    break;

    case 2:if(qempty())
    {
    printf("\n\Queue is empty can't delete\n");
    }
    else
    {
    d=del();
    printf("\n\deleted element is %d\n",d);
    }
    break;

    case 3:if(qempty())
    {
    printf("\nAdd at least one item\n");
    }
    else
    {
    display();
    }
    break;

    case 4:exit(0);
    break;

    default:printf("\nEnter proper choice\n");
    }

    }

    }

Stack Using Array

/* Stack using array*/

    #include<stdio.h>
    #define size 5

    struct stack
    {

    int top,S[size];

    }st;

    int stfull()
    {

    if(st.top==size-1)
    {
         return 1;
    }
    else
    {
         return  0;
    }

    }

    int stempty()
    {

    if(st.top==-1)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }

    void push(int value)
    {

    st.top++;
    st.S[st.top]=value;

    }

    void pop()
    {

    int r;
    r=st.S[st.top];
    st.top--;

    }

    void peep()
    {

    printf("\nTop element is %d\n",st.S[st.top]);

    }

    void display()
    {

    int i;
    printf("\n");
    for(i=st.top;i>=0;i--)
    {
         printf("%d\n",st.S[i]);
    }

    }

    void main()
    {

    int choice,data;
    st.top=-1;

    while(1)
    {
    printf("\n1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit\nYour choice:");
    scanf("\n%d",&choice);

    switch(choice)
    {
    case 1:
    if(stfull())
    {
         printf("\nStack is Full\n");
    }
    else
    {
         printf("\nEnter value:");
         scanf("%d",&data);
         push(data);
    }
    break;

    case 2:
    if(stempty())
    {
         printf("\nStack is Empty\n");
    }
    else
    {
         pop();
    }
    break;

    case 3:peep();
           break;

    case 4:if(stempty())
    {
         printf("\nAdd at least one element\n");
    }
    else
    {
         display();
    }
    break;

    case 5:exit(0);
           break;

    default:printf("\nEnter proper choice\n");
    }
    }

    }