0% found this document useful (0 votes)
170 views3 pages

SHOPPING C Program

This C program implements a shopping cart functionality using linked lists. It allows a user to add products to a stock list, add items from stock to a cart, display the current stock and cart, and calculate the total cost of items in the cart. Nodes in the linked lists store the product name, quantity, and cost of each item. Functions are defined to create nodes, insert into the stock and cart lists, display the lists, and calculate the total cost. The main function provides a menu for the user to interact with these shopping cart features.

Uploaded by

Dragon City
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views3 pages

SHOPPING C Program

This C program implements a shopping cart functionality using linked lists. It allows a user to add products to a stock list, add items from stock to a cart, display the current stock and cart, and calculate the total cost of items in the cart. Nodes in the linked lists store the product name, quantity, and cost of each item. Functions are defined to create nodes, insert into the stock and cart lists, display the lists, and calculate the total cost. The main function provides a menu for the user to interact with these shopping cart features.

Uploaded by

Dragon City
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Cart Shopping

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{char *pname;int quan,cost;struct node* next;}NODE;
typedef NODE* SHOP;
SHOP makeNode(char *n,int q,int c)
{
SHOP temp;
temp=(SHOP)malloc(sizeof(NODE));
temp->quan=q;
temp->cost=c;
temp->pname=(char*)malloc(20);
temp->pname=n;
temp->next=NULL;
return temp;
}

SHOP insertS(SHOP l)
{
int q,c;
char *nm;
nm=(char*)malloc(15);
printf("\nEnter Product Name: ");
scanf("%s",nm);
printf("\nEnter quantity: ");
scanf("%d",&q);
printf("\nEnter cost: ");
scanf("%d",&c);
SHOP temp=makeNode(nm,q,c),t=l;
if(!l)
return temp;
while(l->next)
l=l->next;
l->next=temp;
return t;
}
SHOP insertC(SHOP cart,SHOP s)
{
int q,c;
char *nm;
nm=(char*)malloc(15);
printf("\nEnter Product Name: ");
scanf("%s",nm);
printf("\nEnter quantity: ");
scanf("%d",&q);
while(s)
{
if(strcmp(nm,s->pname)==0)
{
if(s->quan==0)
{
printf("\n%s is not available.",nm);
return cart;
}
if(q>s->quan)
{
printf("\nOnly %d [Link] will be billed only for that.",s->quan);
q=s->quan;
s->quan=0;
}
else
{
printf("\n%d of %s has been added to the cart.",q,nm);
s->quan-=q;
}
c=q*s->cost;
break;
}
s=s->next;
}
if(!s)
{
printf("\nProduct Not Found");
return cart;
}
SHOP temp=makeNode(nm,q,c),t=cart;
if(!cart)
return temp;
while(cart->next)
cart=cart->next;
cart->next=temp;
return t;
}
int calc(SHOP l)
{
int s=0;
while(l)
{
s+=l->cost;
l=l->next;
}
return s;
}
void display(SHOP l)
{
if(!l)
{
printf("\nEmpty.");
return;
}
while(l)
{
printf("\n %-10s\t%d\tRs %d.00",l->pname,l->quan,l->cost);
l=l->next;
}
}
void main()
{
SHOP stock=NULL,basket=NULL;
int ch=0;
printf("\n\t\t\t\t\t\t\tSHOP\n");
printf("\[Link] Stock\[Link] Products\[Link] Stock\[Link] Cart\[Link]\n");
do
{

printf("\nChoose an option : ");


scanf("%d",&ch);
switch(ch)
{
case 1: stock=insertS(stock);
break;
case 2: basket=insertC(basket,stock);
break;
case 3: printf("\nStock : ");
display(stock);
break;
case 4: printf("\nCart : ");
display(basket);
printf("\n\n\nTotal cost : Rs %d.00",calc(basket));
break;
case 5: printf("\nVisit Again soon.");
exit(0);
default: printf("\nInvalid option");
}
}while(ch!=5);
}

You might also like