#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int porductID;
float ProductPrice;
char productName[10];
struct node *next;
};
typedef struct node Node;
void DisplayBill(Node *ptr);
void ListOfProduct(Node *ptr);
void InsertProductToBillList(Node **ptr,float PR,int PID,char PName[]);
void DeleteElementFromeList(Node **ptr,int position);
int NumberOfNodes(Node *ptr);
int main()
{
Node *head=NULL;
int choice;
while(1)
{
printf("Enter 1 for to print total bill,2 Insert product to list\n");
printf("Enter 3 Display list of items,0 stop\n");
printf("Enter choice:");
scanf("%d",&choice);
if(choice<=0)
{
break;
}
else
{
switch(choice)
{
case 1:
{
DisplayBill(head);
break;
}
case 2:
{
float PR;
int PID;
char PName[10];
printf("Enter product id:");
scanf("%d",&PID);
printf("Enter product price:");
scanf("%f",&PR);
printf("Enter product name:");
scanf("%s",PName);
InsertProductToBillList(&head,PR,PID,PName);
break;
}
case 3:
{
printf("List of elements that you added into list:\n");
ListOfProduct(head);
break;
}
case 4:
{
int position;
printf("Enter index of element you want to delete:");
scanf("%d",&position);
DeleteElementFromeList(&head,position);
break;
}
default:
{
printf("Something is wrong\n");
}
}
}
}
return 0;
}
int NumberOfNodes(Node *ptr)
{
int count=0;
if(ptr==NULL)
{
return 0;
}
else
{
while(ptr!=NULL)
{
count++;
}
}
return count;
}
void InsertProductToBillList(Node **ptr,float PR,int PID,char PName[])
{
Node *p=*ptr;
Node *n=(Node*)malloc(sizeof(Node));
n->porductID=PID;
n->ProductPrice=PR;
strcpy(n->productName,PName);
n->next=NULL;
if(*ptr==NULL)
{
*ptr=n;
}
else
{
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;
}
}
void DisplayBill(Node *ptr)
{
int NumberOfProduct,count=0;
float TotalBill=0.0;
if(ptr==NULL)
{
printf("Product list is empty\n");
TotalBill=0;
NumberOfProduct=0;
}
else
{
while(ptr!=NULL)
{
count++;
TotalBill+=ptr->ProductPrice;
ptr=ptr->next;
}
printf("Total number of products in list is:%d\n",count);
printf("Total Bill of products %d is:%.2f\n",count,TotalBill);
}
}
void ListOfProduct(Node *ptr)
{
int count=0;
if(ptr==NULL)
{
printf("Product list is empty\n");
}
else
{
while(ptr!=NULL)
{
printf("Product number:%d\n",count+1);
printf("Product ID:%d\n",ptr->porductID);
printf("Product name is:%s\n",ptr->productName);
printf("Product price is:%f\n",ptr->ProductPrice);
ptr=ptr->next;
++count;
}
}
printf("\n");
}
void DeleteElementFromeList(Node **ptr,int position)
{
int count=0;
Node *n;
Node *p=*ptr;
if(*ptr==NULL)
{
printf("Product list is empty\n");
}
else
{
while(count<position)
{
n=p;
p=p->next;
count++;
}
n->next=p->next;
free(p);
}
}
0 Comments