| Function | What it does | Key pointer operation |
| createNode() | malloc a new node, fill fields, return pointer | malloc(sizeof(Node)) |
| addItem() | Walk to tail, set tail->next = newNode | Insert at tail |
| displayCart() | Traverse headβNULL, print each node | current = current->next |
| getTotal() | Traverse, accumulate priceΓqty | Same traversal |
| countItems() | Traverse, count++ each node | Same traversal |
| removeItem() | Find node, bypass with prev->next, free node | prev->next = current->next; free(current) |
| clearCart() | Free every node one by one | tmp=current->next; free(current); current=tmp |
shopping_cart_complete.c
C β Complete Project
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ββββββββββββββββββββββββββββββββββββββ
NODE DEFINITION
βββββββββββββββββββββββββββββββββββββββ */
typedef struct Node {
char name[50];
float price;
int qty;
struct Node *next;
} Node;
Node *head = NULL; /* global head β empty cart */
/* ββββββββββββββββββββββββββββββββββββββ
CREATE NODE
βββββββββββββββββββββββββββββββββββββββ */
Node* createNode(const char *name, float price, int qty) {
Node *n = (Node*)malloc(sizeof(Node));
strcpy(n->name, name);
n->price = price;
n->qty = qty;
n->next = NULL;
return n;
}
/* ββββββββββββββββββββββββββββββββββββββ
ADD ITEM β insert at tail
βββββββββββββββββββββββββββββββββββββββ */
void addItem(const char *name, float price, int qty) {
Node *newNode = createNode(name, price, qty);
if (head == NULL) { head = newNode; }
else {
Node *curr = head;
while (curr->next != NULL) curr = curr->next;
curr->next = newNode;
}
printf(" [+] '%s' added (qty:%d @ Rs%.2f)\n", name, qty, price);
}
/* ββββββββββββββββββββββββββββββββββββββ
DISPLAY CART β traverse & print
βββββββββββββββββββββββββββββββββββββββ */
void displayCart() {
if (head == NULL) { printf(" Cart is empty.\n"); return; }
printf("\n %-20s %8s %5s %10s\n","Item","Price","Qty","Subtotal");
printf(" %-50s\n", "--------------------------------------------------");
Node *curr = head;
while (curr != NULL) {
printf(" %-20s %8.2f %5d %10.2f\n",
curr->name, curr->price, curr->qty,
curr->price * curr->qty);
curr = curr->next;
}
printf(" %-50s\n", "--------------------------------------------------");
}
/* ββββββββββββββββββββββββββββββββββββββ
GET TOTAL
βββββββββββββββββββββββββββββββββββββββ */
float getTotal() {
float total = 0.0;
Node *curr = head;
while (curr != NULL) { total += curr->price * curr->qty; curr = curr->next; }
return total;
}
/* ββββββββββββββββββββββββββββββββββββββ
REMOVE ITEM β find, bypass, free
βββββββββββββββββββββββββββββββββββββββ */
void removeItem(const char *name) {
Node *curr = head, *prev = NULL;
while (curr != NULL && strcmp(curr->name, name) != 0) {
prev = curr; curr = curr->next;
}
if (curr == NULL) { printf(" '%s' not in cart.\n", name); return; }
if (prev == NULL) head = curr->next; /* was head node */
else prev->next = curr->next; /* middle/tail */
printf(" [-] '%s' removed. (was Rs%.2f Γ %d)\n",
curr->name, curr->price, curr->qty);
free(curr);
}
/* ββββββββββββββββββββββββββββββββββββββ
CLEAR CART β free all nodes
βββββββββββββββββββββββββββββββββββββββ */
void clearCart() {
Node *curr = head;
while (curr != NULL) {
Node *tmp = curr->next; /* save next BEFORE freeing */
free(curr); /* free current node */
curr = tmp; /* move to saved next */
}
head = NULL;
printf(" Cart cleared.\n");
}
/* ββββββββββββββββββββββββββββββββββββββ
MENU
βββββββββββββββββββββββββββββββββββββββ */
void printMenu() {
printf("\n ββββββββββββββββββββββββββββ\n");
printf(" β π SHOPPING CART β\n");
printf(" β βββββββββββββββββββββββββββ£\n");
printf(" β 1. Add Item β\n");
printf(" β 2. Remove Item β\n");
printf(" β 3. View Cart β\n");
printf(" β 4. Get Total β\n");
printf(" β 5. Clear Cart β\n");
printf(" β 6. Exit β\n");
printf(" ββββββββββββββββββββββββββββ\n");
printf(" Choice: ");
}
/* ββββββββββββββββββββββββββββββββββββββ
MAIN
βββββββββββββββββββββββββββββββββββββββ */
int main() {
int choice;
char name[50];
float price;
int qty;
/* Pre-load some items */
addItem("Apple", 40.0, 2);
addItem("Milk", 55.0, 1);
addItem("Bread", 30.0, 3);
do {
printMenu();
scanf("%d", &choice);
getchar(); /* flush newline */
switch (choice) {
case 1:
printf(" Name: "); fgets(name, 50, stdin);
name[strcspn(name, "\n")] = 0; /* remove newline */
printf(" Price: "); scanf("%f", &price);
printf(" Qty : "); scanf("%d", &qty);
getchar();
addItem(name, price, qty);
break;
case 2:
printf(" Remove item name: ");
fgets(name, 50, stdin);
name[strcspn(name, "\n")] = 0;
removeItem(name);
break;
case 3:
displayCart();
break;
case 4:
displayCart();
printf(" TOTAL: Rs %.2f\n", getTotal());
break;
case 5:
clearCart();
break;
case 6:
clearCart(); /* always free before exit */
printf(" Goodbye! π\n");
break;
default:
printf(" Invalid choice.\n");
}
} while (choice != 6);
return 0;
}