Code Doubly Linked List
Nama : Dikky Larson
NIM : 2301853930
Berikut ini konsep coding menggunakan linked list lengkap beserta catatan dan dokumentasinya... Silahkan dibaca dan dipahami ya 😊
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// global variable here
int input = 0;
int totalCheckout = 0;
// double linked list struct initialization
struct item{
char name[50];
int qty;
int price = (rand() % 10000);
struct item *next, *prev;
};
// declare head tail as null variable
struct item *head = NULL, *tail = NULL;
// create items method here
void insertItem(char name[], int qty){
struct item *newItem = (struct item*) malloc(sizeof(struct item));
// insert item details here
strcpy(newItem->name, name);
newItem->qty = qty;
// next and prev = NULL
newItem->next = newItem->prev = NULL;
// sorted insert insert logic based on item name
if(head == NULL){
// if new item is the first item, assign head and tail to the new item
head = tail = newItem;
}else{
if(strcmp(newItem->name, head->name) < 0){
// place new item ahead of head item
newItem->next = head;
head->prev = newItem;
//assign head to new item
head = newItem;
}else if(strcmp(newItem->name, tail->name) > 0){
// place new item after last item created
newItem->prev = tail;
tail->next = newItem;
// assign tail to new item
tail = newItem;
}else{
// place new item in the middle based on name (ascending)
struct item *curr = head->next;
while(newItem->name < curr->name){
curr = curr->next;
}
// chaining item in mid
newItem->prev = curr->prev;
newItem->next = curr;
curr->prev->next = newItem;
curr->prev = newItem;
}
}
}
// pop/delete method here
bool popSearch(char name[]){
struct item *curr = head;
bool isValid = false;
if(strcmp(head->name, name) == 0){
// delete head
struct item *temp = head;
head = head->next;
temp->next = NULL;
free(temp);
return true;
}else if(strcmp(tail->name, name) == 0){
// delete tail
struct item *temp = tail;
tail = tail->prev;
tail->next = temp->prev = NULL;
free(temp);
return true;
}
while(curr != NULL){
if(strcmp(curr->name, name) == 0){
isValid = true;
break;
}
curr = curr->next;
}
if(!isValid){
printf("Item not found!\n");
return isValid;
}
// change connection/link here
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
curr->next = curr->prev = NULL;
// delete selected data
free(curr);
return true;
}
// search method here
struct item *searchItem(char name[]){
if(strcmp(head->name, name) == 0) return head;
if(strcmp(tail->name, name) == 0) return tail;
struct item *curr = head->next;
while(curr != tail){
if(strcmp(curr->name, name) == 0) return curr;
curr = curr->next;
}
return NULL;
}
// print method here
void printItems(){
// take head node as curr node
struct item *curr = head;
// loop through available items here
int i = 1;
while(curr != NULL){
printf("Item - %d:\n", i);
printf("Item name : %s\n", curr->name);
printf("Item qty : %d\n", curr->qty);
printf("Item price : %d\n", curr->price);
curr = curr->next;
printf("\n");
i++;
}
}
int main(){
while(true){
// changing variable here
char name[50]={NULL};
// main menu here
printf("1. Create\n");
printf("2. Update\n");
printf("3. Delete\n");
printf("4. Checkout\n");
printf("5. Exit\n");
printf(" >> ");
scanf("%d", &input);
getchar();
if(input >= 5 || input <=0) break;
// menu logic here
if(input == 1){
// create double linked list data
// input item name
printf("Input item name: ");
scanf("%[^\n]", name);
//input item qty1
int qty=0;
printf("Input item qty: ");
scanf("%d", &qty);
// insert method calling
insertItem(name, qty);
fflush(stdin);
}else if(input == 2){
// update data (qty) from dll (+)
if(head != NULL){
printItems();
printf("Please input item name you want to update!\n");
printf(" >> ");
scanf("%[^\n]", name);
struct item *searchResult = searchItem(name);
system("cls");
if(searchResult == NULL){
printf("Item not found. Please try again!\n");
}else{
int currQty = searchResult->qty;
printf("Item name : %s\n", searchResult->name);
printf("Item qty : %d\n\n", currQty);
printf("Item price : %d\n", searchResult->price);
printf("Input how many items you want to add\n");
printf(" >> ");
int qty = 0;
scanf("%d", &qty);
currQty += qty;
searchResult->qty = currQty;
printf("Item successfully updated!\n");
}
fflush(stdin);
}else{
printf("Sorry, there's currently no item available\n");
}
}else if(input == 3){
// delete
if(head != NULL){
printItems();
printf("Please input item name you want to delete!\n");
printf(" >> ");
scanf("%[^\n]", name);
bool isDeleted = popSearch(name);
if(isDeleted)
printf("Item successfully deleted!\n");
else{
printf("Please try again.\n");
}
}else{
printf("Sorry, there's currently no item available\n");
}
fflush(stdin);
}else if(input == 4){
// update data (qty) from dll (-)
// view all available items
printItems();
printf("Please input item name you want to checkout!\n");
printf(" >> ");
scanf("%[^\n]", name);
struct item *searchResult = searchItem(name);
system("cls");
if(searchResult == NULL){
printf("Item not found. Please try again!\n");
}else{
int currQty = searchResult->qty;
if(currQty == 0){
printf("Sorry, you cannot checkout an empty stock.\n");
}else{
printf("Item name : %s\n", searchResult->name);
printf("Item qty : %d\n\n", currQty);
printf("Item price : %d\n", searchResult->price);
printf("Input how many items you want to checkout\n");
printf(" >> ");
int qty = 0;
scanf("%d", &qty);
getchar();
currQty -= qty;
searchResult->qty = currQty;
int totalRevenue = qty * searchResult->price;
totalCheckout += totalRevenue;
printf("Item successfully checkouted!\n");
printf("You have checkouted %d of %s with revenue %d!\n", qty, searchResult->name, totalRevenue);
}
}
fflush(stdin);
}else{
printf("Invalid input. Please try again\n");
}
// additional printline for decoration
printf("=-=-=-=-=-=-=-=-=-==-=-=-=-=\n");
printf("Press enter to continue...\n");
getchar();
system("cls");
}
system("cls");
printf("You have checkouted %d in total!\n\n", totalCheckout);
printf("THANK YOU FOR USING THIS APPLICATION!\n\n");
printf("Created by :\n");
printf("Name : Dikky Larson\n");
printf("NIM : 2301853930\n");
return 0;
}
NIM : 2301853930
Berikut ini konsep coding menggunakan linked list lengkap beserta catatan dan dokumentasinya... Silahkan dibaca dan dipahami ya 😊
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// global variable here
int input = 0;
int totalCheckout = 0;
// double linked list struct initialization
struct item{
char name[50];
int qty;
int price = (rand() % 10000);
struct item *next, *prev;
};
// declare head tail as null variable
struct item *head = NULL, *tail = NULL;
// create items method here
void insertItem(char name[], int qty){
struct item *newItem = (struct item*) malloc(sizeof(struct item));
// insert item details here
strcpy(newItem->name, name);
newItem->qty = qty;
// next and prev = NULL
newItem->next = newItem->prev = NULL;
// sorted insert insert logic based on item name
if(head == NULL){
// if new item is the first item, assign head and tail to the new item
head = tail = newItem;
}else{
if(strcmp(newItem->name, head->name) < 0){
// place new item ahead of head item
newItem->next = head;
head->prev = newItem;
//assign head to new item
head = newItem;
}else if(strcmp(newItem->name, tail->name) > 0){
// place new item after last item created
newItem->prev = tail;
tail->next = newItem;
// assign tail to new item
tail = newItem;
}else{
// place new item in the middle based on name (ascending)
struct item *curr = head->next;
while(newItem->name < curr->name){
curr = curr->next;
}
// chaining item in mid
newItem->prev = curr->prev;
newItem->next = curr;
curr->prev->next = newItem;
curr->prev = newItem;
}
}
}
// pop/delete method here
bool popSearch(char name[]){
struct item *curr = head;
bool isValid = false;
if(strcmp(head->name, name) == 0){
// delete head
struct item *temp = head;
head = head->next;
temp->next = NULL;
free(temp);
return true;
}else if(strcmp(tail->name, name) == 0){
// delete tail
struct item *temp = tail;
tail = tail->prev;
tail->next = temp->prev = NULL;
free(temp);
return true;
}
while(curr != NULL){
if(strcmp(curr->name, name) == 0){
isValid = true;
break;
}
curr = curr->next;
}
if(!isValid){
printf("Item not found!\n");
return isValid;
}
// change connection/link here
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
curr->next = curr->prev = NULL;
// delete selected data
free(curr);
return true;
}
// search method here
struct item *searchItem(char name[]){
if(strcmp(head->name, name) == 0) return head;
if(strcmp(tail->name, name) == 0) return tail;
struct item *curr = head->next;
while(curr != tail){
if(strcmp(curr->name, name) == 0) return curr;
curr = curr->next;
}
return NULL;
}
// print method here
void printItems(){
// take head node as curr node
struct item *curr = head;
// loop through available items here
int i = 1;
while(curr != NULL){
printf("Item - %d:\n", i);
printf("Item name : %s\n", curr->name);
printf("Item qty : %d\n", curr->qty);
printf("Item price : %d\n", curr->price);
curr = curr->next;
printf("\n");
i++;
}
}
int main(){
while(true){
// changing variable here
char name[50]={NULL};
// main menu here
printf("1. Create\n");
printf("2. Update\n");
printf("3. Delete\n");
printf("4. Checkout\n");
printf("5. Exit\n");
printf(" >> ");
scanf("%d", &input);
getchar();
if(input >= 5 || input <=0) break;
// menu logic here
if(input == 1){
// create double linked list data
// input item name
printf("Input item name: ");
scanf("%[^\n]", name);
//input item qty1
int qty=0;
printf("Input item qty: ");
scanf("%d", &qty);
// insert method calling
insertItem(name, qty);
fflush(stdin);
}else if(input == 2){
// update data (qty) from dll (+)
if(head != NULL){
printItems();
printf("Please input item name you want to update!\n");
printf(" >> ");
scanf("%[^\n]", name);
struct item *searchResult = searchItem(name);
system("cls");
if(searchResult == NULL){
printf("Item not found. Please try again!\n");
}else{
int currQty = searchResult->qty;
printf("Item name : %s\n", searchResult->name);
printf("Item qty : %d\n\n", currQty);
printf("Item price : %d\n", searchResult->price);
printf("Input how many items you want to add\n");
printf(" >> ");
int qty = 0;
scanf("%d", &qty);
currQty += qty;
searchResult->qty = currQty;
printf("Item successfully updated!\n");
}
fflush(stdin);
}else{
printf("Sorry, there's currently no item available\n");
}
}else if(input == 3){
// delete
if(head != NULL){
printItems();
printf("Please input item name you want to delete!\n");
printf(" >> ");
scanf("%[^\n]", name);
bool isDeleted = popSearch(name);
if(isDeleted)
printf("Item successfully deleted!\n");
else{
printf("Please try again.\n");
}
}else{
printf("Sorry, there's currently no item available\n");
}
fflush(stdin);
}else if(input == 4){
// update data (qty) from dll (-)
// view all available items
printItems();
printf("Please input item name you want to checkout!\n");
printf(" >> ");
scanf("%[^\n]", name);
struct item *searchResult = searchItem(name);
system("cls");
if(searchResult == NULL){
printf("Item not found. Please try again!\n");
}else{
int currQty = searchResult->qty;
if(currQty == 0){
printf("Sorry, you cannot checkout an empty stock.\n");
}else{
printf("Item name : %s\n", searchResult->name);
printf("Item qty : %d\n\n", currQty);
printf("Item price : %d\n", searchResult->price);
printf("Input how many items you want to checkout\n");
printf(" >> ");
int qty = 0;
scanf("%d", &qty);
getchar();
currQty -= qty;
searchResult->qty = currQty;
int totalRevenue = qty * searchResult->price;
totalCheckout += totalRevenue;
printf("Item successfully checkouted!\n");
printf("You have checkouted %d of %s with revenue %d!\n", qty, searchResult->name, totalRevenue);
}
}
fflush(stdin);
}else{
printf("Invalid input. Please try again\n");
}
// additional printline for decoration
printf("=-=-=-=-=-=-=-=-=-==-=-=-=-=\n");
printf("Press enter to continue...\n");
getchar();
system("cls");
}
system("cls");
printf("You have checkouted %d in total!\n\n", totalCheckout);
printf("THANK YOU FOR USING THIS APPLICATION!\n\n");
printf("Created by :\n");
printf("Name : Dikky Larson\n");
printf("NIM : 2301853930\n");
return 0;
}
Comments
Post a Comment