D.S. Programs

Write a Program to perform all operations (10 operations) of singly linked list on following structure:

    struct Product

    {

        int code;

    char name[10];

        float price, quantity, amount;

    };

    Calculate amount as price * quantity

Ans :-

# include <stdio.h>

# include <conio.h>


struct product

{

    int code;

    char name[10];

    float price,quantity,amount;

    struct product *next;

};

struct product *start;

void main()

{

   int choice;


   void insertfirst();

   void insertlast();

   void insertspecific();

   void deletefirst();

   void deletelast();

   void deletespecificbyvalue();

   void deletespecificbynodeno();

   void display();

   void search();

   void sort();


   do

   {

      clrscr();


      printf("\n\t1.insertfirst");

      printf("\n\t2.insertlast");

      printf("\n\t3.insertspecific");

      printf("\n\t4.deletefirst");

      printf("\n\t5.deletelast");

      printf("\n\t6.deletespecificbyvalue");

      printf("\n\t7.deletespecificbynodeno");

      printf("\n\t8.display");

      printf("\n\t9.search");

      printf("\n\t10.sort");

      printf("\n\t0.Exit");


      printf("\n\tEnter Your Choice:");

      scanf("%d",&choice);


      switch(choice)

      {

  case 1:insertfirst();

   break;

  case 2: insertlast();

   break;

  case 3: insertspecific();

   break;

  case 4: deletefirst();

   break;

  case 5:  deletelast();

   break;

  case 6: deletespecificbyvalue();

   break;

  case 7: deletespecificbynodeno();

   break;

  case 8:  display();

   break;

  case 9:  search();

   break;

  case 10:  sort();

   break;

  case 0: printf("\nEnd of program");

    break;

  default: printf("\nInvalid Choice");

     break;

      }

      getch();

   }

   while(choice!=0);

}

void insertfirst()

{

   struct product *node;


   if(start==NULL)

   {

      start = (struct product *) malloc(sizeof(struct product));


      printf("\n\nEnter pro. Code=");

      scanf("%d",&start->code);


      printf("\n\tEnter pro.Name=");

      fflush(stdin);

      gets(start->name);


      printf("\n\nEnter pro. price=");

      scanf("%f",&start->price);


      printf("\n\nEnter pro. quantity=");

      scanf("%f",&start->quantity);


      start->amount=start->price*start->quantity;


      start->next=NULL;

   }

   else

   {

      node = (struct product *) malloc(sizeof(struct product));


      printf("\n\nEnter pro. Code=");

      scanf("%d",&node->code);


      printf("\n\tEnter pro.Name=");

      fflush(stdin);

      gets(node->name);


      printf("\n\nEnter pro. price=");

      scanf("%f",&node->price);


      printf("\n\nEnter pro. quantity=");

      scanf("%f",&node->quantity);


      node->amount=node->price*node->quantity;



      node->next=start;

      start=node;

   }

}

void insertlast()

{

      struct product *node,*temp;

      node = (struct product *) malloc(sizeof(struct product));


      printf("\n\nEnter pro. Code=");

      scanf("%d",&node->code);


      printf("\n\tEnter pro.Name=");

      fflush(stdin);

      gets(node->name);


      printf("\n\nEnter pro. price=");

      scanf("%f",&node->price);


      printf("\n\nEnter pro. quantity=");

      scanf("%f",&node->quantity);


      node->amount=node->price*node->quantity;


      node->next=NULL;


      if(start==NULL)

      {

start=node;

      }

      else

      {

temp=start;


while(temp->next!=NULL)

{

      temp= temp->next;

}

temp->next=node;

      }


}

void insertspecific()

{

     int count=0,a,pos;

     struct product *node, *newnode;

     if(start==NULL)

     {

start = (struct product *) malloc(sizeof(struct product));


printf("\n\tEnter pro. Code=");

scanf("%d",&start->code);


printf("\n\tEnter pro. Name=");

fflush(stdin);

gets(start->name);


printf("\n\nEnter pro. price=");

scanf("%f",&start->price);


printf("\n\nEnter pro. quantity=");

scanf("%f",&start->quantity);


start->amount=start->price*start->quantity;



start->next =NULL;

     }

     else

     {

node = start;

while(node!=NULL)

{

    count++;

    node=node->next;

}

do

{

    printf("\n\tEnter Node Number Between 1 to %d=",count+1);

    scanf("%d",&pos);

}

while(pos<1 || pos>count+1);

if(pos==1)

{

      insertfirst();

}

else if(pos==count+1)

{

      insertlast();

}

else

{

       node = start;

       a=1;

       while(a<pos-1)

       {

node = node->next;

a++;

       }

   newnode=(struct product *) malloc(sizeof(struct product));


   printf("\n\tEnter pro. Code=");

   scanf("%d",&newnode->code);


   printf("\n\tEnter pro. Name=");

   fflush(stdin);

   gets(newnode->name);


   newnode->next= node->next;

   node->next = newnode;

}

     }

}

void deletefirst()

{

     struct product *temp;


     if(start==NULL)

     {

printf("\n\tsingly linked list is empty");

     }

     else

     {

temp=start;

printf("\n\tDelete Node Information");

printf("\n\tpro.code=%d",temp->code);

printf("\n\tpro.Name=%s",temp->name);

printf("\n\tpro.price=%f",temp->price);

printf("\n\tpro.quantity=%f",temp->quantity);

printf("\n\tpro.amount=%f",temp->amount);


start=start->next;

free(temp);

     }

}

void deletelast()

{

   struct product *node, *temp=NULL;


   if(start==NULL)

   {

      printf("\n\tSingly Linked List is Empty");

   }

   else

   {

      if(start->next==NULL)

      {

temp = start;

start = start->next;

      }

      else

      {

node = start;

while(node->next->next != NULL)

{

     node= node->next;

}

temp = node->next;

node->next = NULL;

      }

      printf("\n\tDelete Node Information:");

      printf("\n\tpro. Code = %d",temp->code);

      printf("\n\tpro. Name = %s",temp->name);

      printf("\n\tpro.price=%f",temp->price);

      printf("\n\tpro.quantity=%f",temp->quantity);

      printf("\n\tpro.amount=%f",temp->amount);


      free(temp);

   }

}

void deletespecificbyvalue()

{

     int cd,flag=0;

     struct product *node,*temp;


     if(start == NULL)

     {

printf("\n\tSingly Linked List is Empty");

     }

     else

     {

printf("\n\tEnter product Code to Delete:");

scanf("%d",&cd);


if(start->code == cd)

{

    deletefirst();

}

else

{

   node=start;

   while(node->next != NULL)

   {

      if(node->next->code == cd)

      {

temp = node->next;

node->next = node->next->next;

flag = 1;

break;

      }

      node = node->next;

   }

   if(flag == 1)

   {

       printf("\n\tDelete Node Information:");

       printf("\n\tpro. Code = %d",temp->code);

       printf("\n\tpro. name = %d",temp->name);

       printf("\n\tpro.price=%f",temp->price);

       printf("\n\tpro.quantity=%f",temp->quantity);

       printf("\n\tpro.amount=%f",temp->amount);


       free(temp);

   }

   else

   {

       printf("\n\tpro. Code does not exits");

   }

}

     }

}

void deletespecificbynodeno()

{

    int count=0, a, pos;

    struct product *node,*temp;


    if(start == NULL)

    {

printf("\n\tSingly Linked List is Empty");

    }

    else

    {

       node = start;

       while(node != NULL)

       {

  count++;

  node = node->next;

       }

       do

       {

  printf("\n\tEnter Node no to delete between 1 to %d:",count);

  scanf("%d",&pos);

       }

       while(pos<1 || pos>count);

       if(pos == 1)

       {

  deletefirst();

       }

       else if(pos == count)

       {

   deletelast();

       }

       else

       {

   a = 1;

   node = start;

   while(a < pos-1)

   {

       node = node->next;

       a++;

   }

   temp = node->next;

   node->next = node->next->next;


   printf("\n\tDelete Node Information:");

   printf("\n\tpro. Code = %d",temp->code);

   printf("\n\tpro. Name = %d",temp->name);

   printf("\n\tpro.price=%f",temp->price);

   printf("\n\tpro.quantity=%f",temp->quantity);

   printf("\n\tpro.amount=%f",temp->amount);


   free(temp);

       }

    }

}

void display()

{

   struct product *node;


   if(start==NULL)

   {

       printf("\n\tSingly Linked List is empty");

   }

   else

   {

       node=start;

       printf("\n\tCode\tName\tprice\tquantity\tamount");

       while(node!=NULL)

       {

   printf("\n\t%d\t%s\t%.2f\t%.2f\t\t%.2f",node->code,node->name,node->price,node->quantity,node->amount);

   node=node->next;

       }

   }

}

void search()

{

   struct product *node;

   int cd,flag=0;


   if(start == NULL)

   {

      printf("\n\tSingly Linked List is empty");

   }

   else

   {

      printf("\n\tEnter Product code to search:");

      scanf("%d",&cd);


      node = start;


      while(node != NULL)

      {

if(node->code == cd)

{

    printf("\n\tpro. Name = %s",node->name);

    printf("\n\tpro. price = %f",node->price);

    printf("\n\tpro. quantity = %f",node->quantity);

    printf("\n\tpro. amount = %f",node->amount);

    flag = 1;

    break;

}

node = node->next;

      }

      if(flag == 0)

      {

  printf("\n\tpro.Code does not exits");

      }

   }

}

void sort()

{

    int cd;

    char nm[10];

    struct product *node1, *node2;


    for(node1=start; node1!=NULL; node1=node1->next)

    {

  for(node2=node1->next; node2!=NULL; node2=node2->next)

  {

      if(node1->code > node2->code)

      {

  cd= node1->code;

  node1->code = node2->code;

  node2->code=cd;


  strcpy(nm, node1->name);

  strcpy(node1->name, node2->name);

  strcpy(node2->name,nm);

      }

  }

    }

    display();

}

float *abc(float *p)

{

      return p;

}


---------------------------------------------------------------------------------------------------------------------------------------

Write a program to perform all operations (10 operations) of doubly linked list on following structure.

struct student

{

int rollno, mark1, mark2, mark3;

char name[20], class[20];

float percent;

};

Calculate Percent and Class.


ANS :- 

# include <stdio.h>

# include <conio.h>

struct student

{

int rollno, mark1, mark2, mark3;

char name[20], clas[20];

float percent;

struct student *prev, *next;

};


struct student  *start, *newnode, *node;


void main()

{

   int choice;


   void insertfirst();

   void insertlast();

   void insertspecific();

   void deletefirst();

   void deletelast();

   void deletespecificbyvalue();

   void deletespecificbynodeno();

   void display();



   do

   {

      clrscr();


      printf("\n\t1.insertfirst");

      printf("\n\t2.insertlast");

      printf("\n\t3.insertspecific");

      printf("\n\t4.deletefirst");

      printf("\n\t5.deletelast");

      printf("\n\t6.deletespecificbyvalue");

      printf("\n\t7.deletespecificbynodeno");

      printf("\n\t8.display");

      printf("\n\t0.Exit");


      printf("\n\tEnter Your Choice:");

      scanf("%d",&choice);


      switch(choice)

      {

  case 1:insertfirst();

   break;

  case 2: insertlast();

   break;

  case 3: insertspecific();

   break;

  case 4: deletefirst();

   break;

  case 5:  deletelast();

   break;

  case 6: deletespecificbyvalue();

   break;

  case 7: deletespecificbynodeno();

   break;

  case 8:  display();

   break;

  case 0: printf("\nEnd of program");

    break;

  default: printf("\nInvalid Choice");

     break;

      }

      getch();

   }

   while(choice!=0);

}



void insertfirst()

{

struct student *newnode;


newnode = (struct student *) malloc(sizeof(struct student));


printf("\n\tEnter Roll NO: ");

scanf("%d",&newnode->rollno);


printf("\n\tEnter Mark1: ");

scanf("%d",&newnode->mark1);


printf("\n\tEnter Mark 2: ");

scanf("%d",&newnode->mark2);


printf("\n\tEnter Mark 3: ");

scanf("%d",&newnode->mark3);


printf("\n\tEnter Name: ");

fflush(stdin);

gets(newnode->name);


printf("\n\tEnter Class: ");

fflush(stdin);

gets(newnode->clas);


newnode->percent = (newnode->mark1+newnode->mark2+newnode->mark3)/3.0;


if(start == NULL)

{

newnode->next = NULL;

start = newnode;

}

else

{

newnode->next = start;

start->prev = newnode;

start = newnode;

}

}


void insertlast()

{

newnode = (struct student*) malloc(sizeof(struct student));


printf("\n\tEnter Roll NO: ");

scanf("%d",&newnode->rollno);


printf("\n\tEnter Mark1: ");

scanf("%d",&newnode->mark1);


printf("\n\tEnter Mark 2: ");

scanf("%d",&newnode->mark2);


printf("\n\tEnter Mark 3: ");

scanf("%d",&newnode->mark3);


printf("\n\tEnter Name: ");

fflush(stdin);

gets(newnode->name);


printf("\n\tEnter Class: ");

fflush(stdin);

gets(newnode->clas);


newnode->percent = (newnode->mark1+newnode->mark2+newnode->mark3)/3.0;



newnode->next = NULL;


if(start == NULL)

{

newnode->prev = NULL;

start = newnode;

}

else

{

node = start;

while(node->next != NULL)

{

node = node->next;

}

newnode->prev=node;

node->next = newnode;

}

}


void display()

{

struct student *node;

if(start == NULL)

{

printf("\n\tDoubly Linked LIst is Empty...");

}

else

{

node = start;

printf("\n\tRollno\tMark1\tMark2\tMark3\tName\t\tClass\tPercentage");
while(node->next != NULL)
{
printf("\n\t%d\t%d\t%d\t%d\t%s\t\t%s\%.2f",node->rollno,node->mark1,node->mark2,node->mark3,node->name,node->clas,node->percent);
node = node->next;
}
       printf("\n\t%d\t%d\t%d\t%d\t%s\t\t%s\t%.2f",node->rollno,node->mark1,node->mark2,node->mark3,node->name,node->clas,node->percent);
}
}

void insertspecific()
{
int a, count = 0, pos;

struct student *node, *newnode;

if(start == NULL)
{
start = (struct student *)malloc(sizeof(struct student));

printf("\n\tEnter Roll NO: ");
scanf("%d",&newnode->rollno);

printf("\n\tEnter Mark1: ");
scanf("%d",&newnode->mark1);

printf("\n\tEnter Mark 2: ");
scanf("%d",&newnode->mark2);

printf("\n\tEnter Mark 3: ");
scanf("%d",&newnode->mark3);

printf("\n\tEnter Name: ");
fflush(stdin);
gets(newnode->name);

printf("\n\tEnter Class: ");
fflush(stdin);
gets(newnode->clas);

start->prev = NULL;
start->next = NULL;
}
else
{
node = start;
while(node != NULL)
{
count++;
node = node->next;
}

       do
       {
printf("\n\tEnter Where to insert between 1 to %d: ");
scanf("%d",&pos);
       }
       while(pos<1||pos>count+1);

       if(pos == 1)
       {
insertfirst();
       }
       else if(pos == count+1)
       {
insertlast();
       }
       else
       {
newnode = (struct student *) malloc(sizeof(struct student)); printf("\n\tEnter Roll NO: ");
printf("\n\tEnter Roll NO: ");
scanf("%d",&newnode->rollno);

printf("\n\tEnter Mark1: ");
scanf("%d",&newnode->mark1);

printf("\n\tEnter Mark 2: ");
scanf("%d",&newnode->mark2);

printf("\n\tEnter Mark 3: ");
scanf("%d",&newnode->mark3);

printf("\n\tEnter Name: ");
fflush(stdin);
gets(newnode->name);

printf("\n\tEnter Class: ");
fflush(stdin);
gets(newnode->clas);

a=1;
node = start;

while(a<pos-1)
{
node = node->next;
a++;
}

newnode->next = node->next;
newnode->prev = node;

node->next->prev = newnode;
node->next = newnode;
       }
}
}


void deletefirst()
{
struct student *temp;

if(start == NULL)
{
printf("\n\tDoubly Linked List is empty...");
}
else
{
temp = start;
if(start->next == NULL)
{
start = NULL;
}
else
{
start->next->prev=NULL;
start = start->next;
}

printf("\n\tDelete NOde Information...");
printf("\n\tEnter Roll NO: %d",temp->rollno);

printf("\n\tEnter Mark1: %d",temp->mark1);

printf("\n\tEnter Mark 2: %d",temp->mark2);

printf("\n\tEnter Mark 3: %d",temp->mark3);

printf("\n\tEnter Name: %s",temp->name);

printf("\n\tEnter Class: %s",temp->clas);

free(temp);
}

}

void deletelast()
{
struct student *node, *temp;

if(start == NULL)
{
printf("\n\tDoubly Linked List is Empty...");
}
else
{
if(start->next ==  NULL)
{
temp = start;
start = NULL;
}
else
{
node = start;
while(node->next->next != NULL)
{
node = node->next;
}
temp = node->next;
node->next = NULL;
}

printf("\n\tDelete Node Information..");

printf("\n\tEnter Roll NO: %d",temp->rollno);

printf("\n\tEnter Mark1: %d",temp->mark1);

printf("\n\tEnter Mark 2: %d",temp->mark2);

printf("\n\tEnter Mark 3: %d",temp->mark3);

printf("\n\tEnter Name: %s",temp->name);

printf("\n\tEnter Class: %s",temp->clas);

free(temp);
}
}


void deletespecificbyvalue()
{
int brollno, flag = 0;
struct student *node, *temp;

if(start == NULL)
{
printf("\n\tDoubly Linked List is Empty...");
}
else
{
printf("\n\tEnter Roll no Delete: ");
scanf("%d",&brollno);

if(start->rollno == brollno)
{
deletefirst();
}
else
{
node = start;
while(node->next->next != NULL)
{
if(node->next->rollno == brollno)
{
temp = node->next;
temp->next->prev = node;
node->next = temp->next;

printf("\n\tDelete Node Information....");
printf("\n\tStudent Rollno : %d",temp->rollno);
printf("\n\tStudent Mark1 : %d",temp->mark1);
printf("\n\tStudent Mark2 : %d",temp->mark2);
printf("\n\tStudent Mark3 : %d",temp->mark3);
printf("\n\tStudent Name : %s",temp->name);
printf("\n\tStudent Class : %s",temp->clas);
printf("\n\tStudent Percent : %.2f",temp->percent);

free(temp);
flag = 1;
break;
}     // If over
node = node->next;
} // while over
if(flag == 0)
{
printf("\n\tStudent Roll %d is not found",brollno);
}
} // else over
} // else over
}  // function over

void deletespecificbynodeno()
{
int a, count = 0, no;
struct student *node, *temp;

if(start == NULL)
{
printf("\n\tDoubly Linked list is Empty...");
}
else
{
node = start;
while(node != NULL)
{
count++;
node = node->next;
}
do
{
printf("\n\tEnter Roll no to delete between 1 to %d: ",count);
scanf("%d",&no);
}
while(no < 1 || no > count);

if(no == 1)
{
deletefirst();
}
else if(no == count)
{
deletelast();
}
else
{
a = 1;
node = start;
while(a < no-1)
{
node = node->next;
a++;
}
temp = node->next;

temp->next->prev = node;
node->next = temp->next;

printf("\n\tDelete Node Information....");
printf("\n\tStudent Rollno : %d",temp->rollno);
printf("\n\tStudent Mark1 : %d",temp->mark1);
printf("\n\tStudent Mark2 : %d",temp->mark2);
printf("\n\tStudent Mark3 : %d",temp->mark3);
printf("\n\tStudent Name : %s",temp->name);
printf("\n\tStudent Class : %s",temp->clas);
printf("\n\tStudent Percent : %.2f",temp->percent);

free(temp);
} // else over
}// else over
} // function over

---------------------------------------------------------------------------------------------------------------------------------------
Write a program for linked list, implemention of stack of following
structure:
struct bill
{
int itemcode, quantity;
char itemname[20];
float price, amount, discount;
};

Calculate amount as quantity * price
Calculate dicount on amount as below:
-If amount >= 10000 then discount 5% of amount
-If amount > 5000 but < 10000 then discount 3% of amount
-If amount < 5000 then discount 2% of amount
Ans :-
#include <stdio.h>
#include <conio.h>

#define size 5

void insert();
void del();
void display();

struct bill
{
int itemcode, que;
char itemname[20];
float price, amount, dis;
struct bill *next;
}b[size];

struct bill *start;
void main()
{
int choice;
clrscr();

do
{
printf("\n\t1. Insert");
printf("\n\t2. Delere");
printf("\n\t3. Display");
printf("\n\t0. Exit");

printf("\n\tEnter Your Choice : ");
scanf("%d",&choice);

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

case 2:
del();
break;

case 3:
display();
break;

case 0:
printf("\n\tEnd Of Program...");
break;

default:
printf("\n\tPlease enter valid number...");
break;
}
getch();
clrscr();
}
while(choice != 0);

getch();
}

void insert()
{
struct bill *node;

if(start == NULL)
{
start = (struct bill *) malloc(sizeof(struct bill));

printf("\n\tEnter Item Code: ");
scanf("%d",&start->itemcode);

printf("\n\tEnter Quantity: ");
scanf("%d",&start->que);

printf("\n\tEnter Item Name: ");
fflush(stdin);
gets(start->itemname);

printf("\n\tEnter Price: ");
scanf("%f",&start->price);

start->amount = start->que * start->price;

start->next = NULL;
}

else
{
node = (struct bill *)malloc(sizeof(struct bill *));

printf("\n\tEnter Item Code: ");
scanf("%d",&node->itemcode);

printf("\n\tEnter Quantity: ");
scanf("%d",&node->que);

printf("\n\tEnter Item Name: ");
fflush(stdin);
gets(node->itemname);

printf("\n\tEnter Price: ");
scanf("%f",&node->price);

node->amount = node->que * node->price;

node->next = start;

start = node;
}
}


void del()
{
struct bill *temp;

if(start == NULL)
{
printf("\n\tStack is Empty...");
}
else
{
temp = start;
printf("\n\tDelete Node Information...");
printf("\n\tEnter Item Code: %d",temp->itemcode);

printf("\n\tEnter Quantity: %d",temp->que);

printf("\n\tEnter Item Name: %s",temp->itemname);

printf("\n\tEnter Price: %.2f",temp->price);

start = start->next;

free(temp);
}
}

void display()
{
struct bill *node;
if(start == NULL)
{
printf("\n\tStack Is Empty...");
}

else
{
node = start;
printf("\nItemcode\tQuantity\tItemname\tPrice\tAmount\tDiscount");

while(node != NULL)
{
printf("\n%d\t\t",node->itemcode);
printf("%d\t\t",node->que);
printf("%s\t\t",node->itemname);
printf("%.2f\t",node->price);
printf("%.2f\t",node->amount);

if(node->amount >= 10000)
{
node->dis = node->amount * 5 / 100;
}
else if(node->amount > 5000 && node->amount < 10000)
{
node->dis = node->amount * 3 / 100;
}
else if(node->amount < 5000)
{
node->dis = node->amount * 2 / 100;
}
printf("%.2f",node->dis);
node = node->next;
}
}
}

float *abc(float *p)
{
return p;
}

---------------------------------------------------------------------------------------------------------------------------------------
Write a program to perform insert, delete, display, search and sort
operations on following array of structure:

struct person
{
long int no;
char name[15], address[30], sity[10];
float salary;
};
Ans :-
#include <stdio.h>
#include <conio.h>

#define size 5

void insert();
void del();
void display();
void search();
void sort();

struct person
{
long int no;
char name[15], address[30], city[10];
float salary;
}p[size];

int count=-1, arr[size];

void main()
{
int choice;

clrscr();

do
{
printf("\n\t1. Insert");
printf("\n\t2. Delete");
printf("\n\t3. Display");
printf("\n\t4. Search");
printf("\n\t5. Sort");
printf("\n\t0. Exit");

printf("\n\n\tEnter Your Choice: ");
scanf("%d",&choice);

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

case 2:
del();
break;

case 3:
display();
break;

case 4:
search();
break;

case 5:
sort();
break;

case 0:
printf("\n\tEnd Of Program...");
break;

default:
printf("\n\tPlease Enter Valid Choice...");
break;
}
getch();
clrscr();
}
while(choice != 0);

getch();
}

void insert()
{
if(count == size-1)
{
printf("\n\tArray is full...");
}
else
{
count++;
printf("\n\tEnter No: ");
scanf("\n\t%ld",&p[count].no);

printf("\n\tEnter Name: ");
fflush(stdin);
gets(p[count].name);

printf("\n\tEnter Adderess: ");
fflush(stdin);
gets(p[count].address);

printf("\n\tEnter City: ");
fflush(stdin);
gets(p[count].city);

printf("\n\tEnter Salary: ");
scanf("%f",&p[count].salary);

}
}

void del()
{
if(count == -1)
{
printf("\n\tArray is empty...");
}
else
{
printf("\n\t%d is deleted successfully...",p[count].no);
count--;
}
}

void display()
{
int a;
if(count == -1)
{
printf("\n\tArray is Empty...");
}
else
{

printf("\nNo.\tName\t\tAddress\t\tCity\t\tSalary");
for(a=0;a<=count;a++)
{
printf("\n%ld\t",p[a].no);
printf("%s\t\t",p[a].name);
printf("%s\t\t",p[a].address);
printf("%s\t\t",p[a].city);
printf("%.2f",p[a].salary);
}
}
}

void search()
{
int sv, a;


if(count == -1)
{
printf("\n\tArray is Empty...");
}
else
{
printf("\n\tEnter Search Value: ");
scanf("%d",&sv);

for(a=0;a<=count;a++)
{
if(p[a].no == sv)
{
printf("\n\tSearch Value %d is found on position %d", sv, a+1);
break;
}
}
}
}

void sort()
{
int a, b, c;

if(count == -1)
{
printf("\n\tArray is Empry...");
}
else
{
for(a=0;a<count;a++)
{
for(b=a+1;b<=count;b++)
{
if(p[a].no > p[b].no)
{
c = p[a].no;
p[a].no = p[b].no;
p[b].no = c;
}
}
}
display();
}
}

float *abc(float *p)
{
return p;
}
---------------------------------------------------------------------------------------------------------------------------------------
Write a program to perform all stack operation on following array of
structure:

struct Employee
{
int Empcode;
char EmpName;
float basic, hra, da, pf, net;
};

Calculate hra, da, pf, and net as following:
- hra is 10% of basic.
- da is 45% of basic.
- pf is 5% of basic.
- net is basic + hra + da - pf.

Ans :-
#include <stdio.h>
#include <conio.h>

#define size 5

int top = -1;

void push();
void pop();
void display();
void calculate();

struct employee
{
int empcode;
char empname[20];
long int basic, hra, da, pf, net;
}stk[size];

void main()
{
int choice;
clrscr();

do
{
printf("\n\t1. push.");
printf("\n\t2. pop.");
printf("\n\t3. display");
printf("\n\t0. exit");


printf("\n\tEnter Your Choice: ");
scanf("%d",&choice);

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

case 2:
pop();
break;

case 3:
display();
break;

case 0:
printf("\n\tEnd of Program...");
break;

default:
printf("\n\tPlease Enter Valid Choice...");
break;
}
getch();
clrscr();
}
while(choice != 0);

}

void push()
{
int no;

if(top == size-1)
{
printf("\n\tStack is Full...");
}
else
{
top++;

printf("\n\tEnter empcode: ");
scanf("%d",&stk[top].empcode);

printf("\n\tEnter Employee Name: ");
fflush(stdin);
gets(stk[top].empname);

printf("\n\tEnter any value for basic: ");
scanf("%ld",&stk[top].basic);


calculate();

}
}

void pop()
{
if(top == -1)
{
printf("\n\tStack is Empty...");
}
else
{
printf("\n\t%d is delete successfully...",stk[top]);

top--;
}
}

void display()
{
int a;
if(top == -1)
{
printf("\n\tStack is Empty...");
}
else
{
printf("\nEmp.code\tEmp.Name\tBasic\thra\tda\tpf\tnet");
for(a=top;a>=0;a--)
{
printf("\n%d\t\t",stk[a].empcode);
printf("%s\t\t",stk[a].empname);
printf("%ld\t",stk[a].basic);
printf("%ld\t",stk[a].hra);
printf("%ld\t",stk[a].da);
printf("%ld\t",stk[a].pf);
printf("%ld",stk[a].net);
}
}
}

void calculate()
{

       // hra is 10% of basic
       stk[top].hra = stk[top].basic * 10 / 100;

       //da is 45 % of basic
       stk[top].da = stk[top].basic * 45 / 100;

       // pf is 5% of basic
       stk[top].pf = stk[top].basic * 5 / 100;

       // net is basic + hra + da - pf
       stk[top].net = stk[top].basic + stk[top].hra + stk[top].da - stk[top].pf;
}

float * abc(float *p)
{
return p;
}
---------------------------------------------------------------------------------------------------------------------------------------
Write a program to perform all queue operations on following array
of structure.:
struct student
{
int rollno, mark1, mark2, mark3;
char name[30], class[30];
float percent;
};
Ans :-
#include <stdio.h>
#include <conio.h>

#define size 5

int queue[size], rear = -1, front = -1;

void insert();
void del();
void display();
float per(int, int, int);

struct student
{
int rollno, mark1, mark2, mark3;
char name[30], clas[30];
float percent;
}st[5];

void main()
{
int choice;
clrscr();

do
{
printf("\n\t1. Insert");
printf("\n\t2. Delete");
printf("\n\t3. Display");
printf("\n\t0. Exit");

printf("\n\n\tEnter Your Choice: ");
scanf("%d",&choice);

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

case 2:
del();
break;

case 3:
display();
break;

case 0:
exit(0);

default:
printf("\n\tPease Enter Valid Number...");
break;
}
getch();
clrscr();
}
while(choice != 0);

getch();
}

float * abc(float *p)
{
return p;
}


void insert()
{
int no, a;

if(rear == size-1)
{
printf("\n\n\tQueue is full...");
}
else
{
rear++;

printf("\n\n\tEnter Roll No: ");
scanf("%d",&st[rear].rollno);

printf("\n\tEnter Mark1: ");
scanf("%d",&st[rear].mark1);

printf("\n\tEnter Mark2: ");
scanf("%d",&st[rear].mark2);

printf("\n\tEnter Mark3: ");
scanf("%d",&st[rear].mark3);

printf("\n\tEnter Name: ");
fflush(stdin);
gets(st[rear].name);

printf("\n\tEnter Class: ");
fflush(stdin);
gets(st[rear].clas);

//queue[rear] = rear;

if(front == -1)
{
front = 0;
}
}
}

void del()
{
int a;

if(rear == -1)
{
printf("\n\tQueue is Empty...");
}
else
{
if(front == rear)
{
front = -1;
rear = -1;
}

front++;
printf("\n\t%d is deleted...",front);

}
}

void display()
{
int a;
float no;

if(rear == -1)
{
printf("\n\n\tQueue is Empty...");
}
else
{
printf("\n\tRoll no\tMark1\tMark2\tMark3\tName\t\tClass\tPercentage");

for(a=front;a<=rear;a++)
{
printf("\n\t%d",st[a].rollno);
printf("\t%d",st[a].mark1);
printf("\t%d",st[a].mark2);
printf("\t%d",st[a].mark3);
printf("\t%s",st[a].name);
printf("\t\t%s",st[a].clas);
printf("\t");

per(st[a].mark1, st[a].mark2, st[a].mark3);
}
}
}

float per(int mark1, int mark2, int mark3)
{
float pers, sum;

sum = mark1 + mark2 + mark3;
pers = sum / 3;

printf("%.2f",pers);

return pers;
}
---------------------------------------------------------------------------------------------------------------------------------------
Write a program to perform all Circular Queue operations on following
array of structure:
struct book
{
int bookcode;
char booktitle[20], author[20], publisher[20];
float price;
};
Ans :-
#include <stdio.h>
#include <conio.h>


#define size 5

int queue[size], rear = -1,front = -1;

struct book
{
int bookcode;
char booktitle[20], author[20], publisher[20];
float price;
}bk[size];

void insert();
void del();
void display();


void main()
{
int choice;

clrscr();

do
{
printf("\n\t1. Insert");
printf("\n\t2. Delete");
printf("\n\t3. Display");
printf("\n\t0. Exit");

printf("\n\tEnter YOur Choice : ");
scanf("%d",&choice);

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

case 2:
del();
break;

case 3:
display();
break;

case 0:
printf("\n\tEnd of program...");
break;

default:
printf("\n\tPlease Enter Valid Number...");
break;
}

getch();
clrscr();
}
while(choice != 0);

getch();
}

void insert()
{
int no;

if(front == 0 && rear == size-1 || rear == front-1)
{
printf("\n\tQueue is full...");
}
else
{
if(rear == -1)
{
front = 0;
rear = 0;
}
else if(rear == size-1 && front > 0)
{
rear = 0;
}
else
{
rear++;
}
printf("\n\tEnter Book Code : ");
scanf("%d",&bk[rear].bookcode);

printf("\n\tEnter Book Title: ");
fflush(stdin);
gets(bk[rear].booktitle);

printf("\n\tEnter Author: ");
fflush(stdin);
gets(bk[rear].author);

printf("\n\tEnter Publisher: ");
fflush(stdin);
gets(bk[rear].publisher);

printf("\n\tEnter Price: ");
scanf("%f",&bk[rear].price);

}
}

void del()
{
if(front == -1)
{
printf("\n\tQueue is Empty...");
}
else
{
printf("\n\t%d is deleted from pos %d",queue[front], front);
if(front == rear)
{
front = -1;
rear = -1;
}
else
{
if(front == size-1 && front > rear)
{
front = 0;
}
else
{
front++;
}
}
}
}

void display()
{
int i;
if(rear==-1)
{
printf("\n\tQueueu is empty...");
}
else
{
printf("\nBook Code\tBook Name\tAuthor\t\tPublisher\tPrice");
if(front <= rear)
{
for(i = front;i<=rear;i++)
{
printf("\t%d",bk[i].bookcode);
printf("\t\t%s",bk[i].booktitle);
printf("\t\t%s",bk[i].author);
printf("\t\t%s",bk[i].publisher);
printf("\t\t%.2f",bk[i].price);
}
}
else
{
for(i = front;i<=size-1;i++)
{
printf("\t%d",bk[i].bookcode);
printf("\t\t%s",bk[i].booktitle);
printf("\t\t%s",bk[i].author);
printf("\t\t%s",bk[i].publisher);
printf("\t\t%.2f",bk[i].price);
}

for(i = 0;i<=rear;i++)
{
printf("\t%d",bk[i].bookcode);
printf("\t\t%s",bk[i].booktitle);
printf("\t\t%s",bk[i].author);
printf("\t\t%s",bk[i].publisher);
printf("\t\t%.2f",bk[i].price);
}
}
}
}

float *abc(float *p)
{
return p;
}

No comments:

Post a Comment