Data Structure 6
Mini Market Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct linkedList{
char productName[100];
int quantity;
struct linkedList *next;
struct linkedList *prev;
};
typedef struct linkedList node;
node* head;
node* tail;
node* current;
node* temp;
void createProduct(char productName[], int quantity)
{
temp = (struct linkedList*)malloc(sizeof(struct linkedList));
strcpy(temp->productName,productName);
temp->quantity = quantity;
temp->prev = temp->next = NULL;
if(head==NULL)
{
head = tail = temp;
}
else
{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void popHead()
{
if(head != NULL)
{
if(head == tail)
{
free(head);
head = tail = NULL;
}
else
{
head = head->next;
free(head->prev);
head->prev = NULL;
}
}
}
void printGroceries()
{
struct linkedList* current = head;
int i=1;
if(head==NULL){
printf("Wish list is empty...\n");
}
else{
while(current != NULL)
{
printf("%d. %s (%d pcs)\n", i, current->productName, current->quantity);
current = current -> next;
i++;
}
}
puts("");
}
void deleteProduct(char productName[101])
{
linkedList* current = head;
int flag = 0;
if(head==NULL){
printf("Groceries is empty...\n");
return;
}
else if(head==tail && strcmp(head->productName,productName)==0){
free(head);
head=tail=NULL;
}
else if(strcmp(head->productName,productName)==0){
current=head;
head=head->next;
free(current);
}
else if(strcmp(tail->productName,productName)==0){
current=tail;
tail=tail->prev;
free(current);
tail->next=NULL;
}
else{
current = head;
while(current)
{
if(strcmp(current->productName,productName)==0)
{
struct linkedList* temp = current->next;
temp->prev = current->prev;
current->prev->next = temp;
free(current);
break;
}
current = current->next;
}
printf("Product not found...\n");
}
}
void editQuantity(char productName[], int newQuantity)
{
node* myNode = head;
while(myNode != '\0')
{
if(strcpy(myNode -> productName, productName) == myNode -> productName)
{
myNode -> quantity = newQuantity;
printf("Product quantity is updated to %d\n", newQuantity);
break;
}
else
{
while(myNode != '\0')
{
myNode = myNode -> next;
if(strcpy(myNode -> productName, productName) == myNode -> productName)
myNode -> quantity = newQuantity;
printf("Product quantity is updated to %d\n", newQuantity);
break;
}
}
printf("Product name is no where to be found!\n");
}
}
void popAll(){
while(head != NULL)
popHead();
}
int main(void)
{
int key, value, quantity, newQuantity, choice, input;
char productName[100];
do{
printf("Menu:\n");
printf("1. Add Product\n");
printf("2. View Groceries\n");
printf("3. Delete Product\n");
printf("4. Edit Quantity\n");
printf("5. Exit\n");
printf("Please choose: ");
scanf("%d",&choice);
if(choice == 1){
printf("\n");
printf("Insert product name: "); getchar();
scanf("%[^\n]", productName);
printf("Insert quantity: ");
scanf("%d", &quantity);
createProduct(productName,quantity);
printf("\n");
}
else if(choice == 2){
printf("\n");
printGroceries();
printf("\n");
}
else if(choice == 3){
printf("\n");
printf("Insert product name: "); getchar();
scanf("%[^\n]",productName);
deleteProduct(productName);
printf("\n");
}
else if(choice == 4)
{
printf("\n");
printf("Insert product name: ");
getchar();
scanf("%[^\n]", productName);
printf("Insert quantity: ");
scanf("%d", &newQuantity);
editQuantity(productName, newQuantity);
printf("\n");
}
}
while(choice != 5);
popAll();
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct linkedList{
char productName[100];
int quantity;
struct linkedList *next;
struct linkedList *prev;
};
typedef struct linkedList node;
node* head;
node* tail;
node* current;
node* temp;
void createProduct(char productName[], int quantity)
{
temp = (struct linkedList*)malloc(sizeof(struct linkedList));
strcpy(temp->productName,productName);
temp->quantity = quantity;
temp->prev = temp->next = NULL;
if(head==NULL)
{
head = tail = temp;
}
else
{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void popHead()
{
if(head != NULL)
{
if(head == tail)
{
free(head);
head = tail = NULL;
}
else
{
head = head->next;
free(head->prev);
head->prev = NULL;
}
}
}
void printGroceries()
{
struct linkedList* current = head;
int i=1;
if(head==NULL){
printf("Wish list is empty...\n");
}
else{
while(current != NULL)
{
printf("%d. %s (%d pcs)\n", i, current->productName, current->quantity);
current = current -> next;
i++;
}
}
puts("");
}
void deleteProduct(char productName[101])
{
linkedList* current = head;
int flag = 0;
if(head==NULL){
printf("Groceries is empty...\n");
return;
}
else if(head==tail && strcmp(head->productName,productName)==0){
free(head);
head=tail=NULL;
}
else if(strcmp(head->productName,productName)==0){
current=head;
head=head->next;
free(current);
}
else if(strcmp(tail->productName,productName)==0){
current=tail;
tail=tail->prev;
free(current);
tail->next=NULL;
}
else{
current = head;
while(current)
{
if(strcmp(current->productName,productName)==0)
{
struct linkedList* temp = current->next;
temp->prev = current->prev;
current->prev->next = temp;
free(current);
break;
}
current = current->next;
}
printf("Product not found...\n");
}
}
void editQuantity(char productName[], int newQuantity)
{
node* myNode = head;
while(myNode != '\0')
{
if(strcpy(myNode -> productName, productName) == myNode -> productName)
{
myNode -> quantity = newQuantity;
printf("Product quantity is updated to %d\n", newQuantity);
break;
}
else
{
while(myNode != '\0')
{
myNode = myNode -> next;
if(strcpy(myNode -> productName, productName) == myNode -> productName)
myNode -> quantity = newQuantity;
printf("Product quantity is updated to %d\n", newQuantity);
break;
}
}
printf("Product name is no where to be found!\n");
}
}
void popAll(){
while(head != NULL)
popHead();
}
int main(void)
{
int key, value, quantity, newQuantity, choice, input;
char productName[100];
do{
printf("Menu:\n");
printf("1. Add Product\n");
printf("2. View Groceries\n");
printf("3. Delete Product\n");
printf("4. Edit Quantity\n");
printf("5. Exit\n");
printf("Please choose: ");
scanf("%d",&choice);
if(choice == 1){
printf("\n");
printf("Insert product name: "); getchar();
scanf("%[^\n]", productName);
printf("Insert quantity: ");
scanf("%d", &quantity);
createProduct(productName,quantity);
printf("\n");
}
else if(choice == 2){
printf("\n");
printGroceries();
printf("\n");
}
else if(choice == 3){
printf("\n");
printf("Insert product name: "); getchar();
scanf("%[^\n]",productName);
deleteProduct(productName);
printf("\n");
}
else if(choice == 4)
{
printf("\n");
printf("Insert product name: ");
getchar();
scanf("%[^\n]", productName);
printf("Insert quantity: ");
scanf("%d", &newQuantity);
editQuantity(productName, newQuantity);
printf("\n");
}
}
while(choice != 5);
popAll();
}
Comments
Post a Comment