Code example for Double Linked List
Dreammer's Market
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct List{
char name[50];
int qty;
int price;
List *next,*prev;
}*head,*tail,*curr,*temp;
List *createNode(const char name[], int qty){
List *temp = (List*)malloc(sizeof(List));
strcpy(temp->name,name);
temp->qty = qty;
temp->next = temp->prev = NULL;
return temp;
}
void enter(){
puts("\nPress any key to continue...");
getchar();
}
void cls(){
for(int i=0;i<30;i++)
puts("");
}
void deleteList(char name[]){
if(!head){
puts("Your cart is empty");
return;
}
curr = head;
while(curr){
if(strcmp(curr->name,name)==0)
break;
if(strcmp(curr->name,name)>0){
puts("Not Found");
return;
}
curr = curr->next;
}
if(!curr)
printf("Not Found");
else if(curr == head && !curr->next)
head = NULL;
else if(curr == head){
head->next->prev = NULL;
head = curr->next;
}
else if(curr == tail){
curr->prev->next = NULL;
tail = curr->prev;
}
else{
curr->next->prev = curr->prev;
curr->prev->next = curr->next;
}
free(curr);
}
void edit(char name[], int change){
if(!head){
puts("Your cart is empty");
return;
}
curr = head;
while(curr){
if(strcmp(curr->name,name)==0)
break;
if(strcmp(curr->name,name)>0){
puts("Not Found");
return;
}
curr = curr->next;
}
if(!curr)
printf("Not found");
else
curr->qty = change;
}
bool search(char name[]){
if(!head)
return false;
curr = head;
while(curr){
if(strcmp(curr->name,name)==0)
return true;
if(strcmp(curr->name,name)>0)
return false;
curr = curr->next;
}
return false;
}
void pushPriority(char name[], int qty){
curr = createNode(name,qty);
curr->price = rand() % 89999 + 10000;
if(!head)
head = tail = curr;
else{
temp = head;
while(temp->next){
if(strcmp(temp->next->name,curr->name)>0)
break;
temp = temp->next;
}
if(temp==tail){
temp->next = curr;
curr->prev = temp;
tail = curr;
}
else{
curr->prev = temp;
curr->next = temp->next;
temp->next->prev = curr;
temp->next = curr;
}
}
head->prev = NULL;
tail->next = NULL;
}
void checkOut(){
if(!head){
puts("Your cart is empty");
enter();
return;
}
curr = head;
int count = 0;
int total = 0;
puts("=====================================================================================");
puts("= Dreammers Market Bill's =");
puts("=====================================================================================");
puts("= No = Item Name = Qty = Price = Total =");
puts("=====================================================================================");
while(curr){
printf("= %3d = %-35s = %6d = Rp.%7d = Rp. %11d =\n",++count,curr->name,curr->qty,curr->price, curr->qty*curr->price);
total += curr->qty*curr->price;
curr = curr->next;
}
puts("=====================================================================================");
printf("=-----------------------------Discount----------------------------= Rp. %11d =\n", -total);
puts("=====================================================================================");
printf("=------------------------------Total------------------------------= Free =\n");
puts("= ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~Kindness is Free ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ = ^^ =");
puts("=====================================================================================");
enter();
}
void deleteCart(){
char name[50];
int quantity;
char strquantity[50];
do{
printf("Input item name you want to delete>> ");
scanf("%[^\n]", name);getchar();
}while(strlen(name)<1);
curr = head;
while(curr){
if(strcmp(curr->name,name)==0)
break;
curr = curr->next;
}
if(!curr){
puts("Not Found");
enter();
return;
}
char confirm[10];
printf("%-15s: %s\n", "Item Name",curr->name);
printf("%-15s: %d\n", "Quantity",curr->qty);
printf("%-15s: %d\n\n", "Price", curr->price);
puts("Do you still want to delete this item from cart? [Yes|No] case sensitive");
do{
printf(">> ");
scanf("%[^\n]", confirm);getchar();
}while(!(strcmp(confirm,"Yes")==0 || strcmp(confirm,"No")==0));
if(strcmp(confirm,"No")==0)
return;
deleteList(name);
puts("Succesfully deleted");
enter();
}
void editCart(){
char name[50];
int quantity;
char strquantity[50];
do{
printf("Input item name you want to edit>> ");
scanf("%[^\n]", name);getchar();
}while(strlen(name)<1);
curr = head;
while(curr){
if(strcmp(curr->name,name)==0)
break;
curr = curr->next;
}
if(!curr){
puts("Not Found");
enter();
return;
}
bool error;
printf("%-15s: %s\n", "Item Name",curr->name);
printf("%-15s: %d\n", "Quantity",curr->qty);
printf("%-15s: %d\n\n", "Price", curr->price);
do{
error = false;
printf("Edit Quantity [0 to cancel] >> ");
scanf("%[^\n]", strquantity);getchar();
for(int i=0;strquantity[i]!='\0';i++){
if(strquantity[i]>=48 && strquantity[i]<=57)
continue;
else{
error = true;
break;
}
}
}while(error);
if(quantity)
return;
quantity = atoi(strquantity);
edit(curr->name, quantity);
puts("Succesfully Update Your Cart");
enter();
}
void addCart(){
char name[50];
int quantity;
char strquantity[50];
do{
printf("Input item name >> ");
scanf("%[^\n]", name);getchar();
}while(strlen(name)<1);
if(search(name)){
puts("\n\nYour item already exist!");
enter();
return;
}
bool error;
do{
error = false;
printf("Input %s quantity >> ", name);
scanf("%[^\n]", strquantity);getchar();
for(int i=0;strquantity[i]!='\0';i++){
if(strquantity[i]>=48 && strquantity[i]<=57)
continue;
else{
error = true;
break;
}
}
}while(error);
quantity = atoi(strquantity);
// printf("%d", quantity);
pushPriority(name,quantity);
}
void viewCart(){
if(!head){
puts("Your cart is empty");
enter();
return;
}
curr = head;
int count = 0;
puts("===================================================================");
puts("= Cart =");
puts("===================================================================");
puts("= No = Item Name = Qty = Price =");
puts("===================================================================");
while(curr){
printf("= %3d = %-35s = %6d = Rp.%7d =\n",++count,curr->name,curr->qty,curr->price);
curr = curr->next;
}
puts("===================================================================");
enter();
}
void mainMenu(){
int input=0;
do{
cls();
puts("Welcome to Dreammers Market!!");
puts("");
puts("Choose what to do:");
puts("1. View Cart");
puts("2. Add New Item to Cart");
puts("3. Edit Quantity Item on Cart");
puts("4. Delete Item on Cart");
puts("5. CheckOut");
puts("6. Exit");
scanf("%d", &input);getchar();
switch(input){
case 1:
viewCart();
break;
case 2:
addCart();
break;
case 3:
editCart();
break;
case 4:
deleteCart();
break;
case 5:
checkOut();
break;
}
}while(input!=6);
puts("\n\nThank you for coming!!");
}
int main(){
mainMenu();
return 0;
}
Tidak ada komentar:
Posting Komentar