Update Q2.c
This commit is contained in:
71
Q2.c
71
Q2.c
@@ -3,54 +3,55 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct Product {
|
||||
int id;
|
||||
char name[30];
|
||||
int quantity;
|
||||
int id;
|
||||
char name[30];
|
||||
int quantity;
|
||||
|
||||
struct Product *next;
|
||||
struct Product *prev;
|
||||
struct Product *next;
|
||||
struct Product *prev;
|
||||
} Product;
|
||||
|
||||
Product *create_product(int id, char *name, int quantity) {
|
||||
Product *newProduct = (Product *)malloc(sizeof(Product));
|
||||
newProduct->id=id;
|
||||
strcpy(newProduct->name, name);
|
||||
newProduct->quantity=quantity;
|
||||
Product *newProduct = (Product *)malloc(sizeof(Product));
|
||||
newProduct->id=id;
|
||||
strcpy(newProduct->name, name);
|
||||
newProduct->quantity=quantity;
|
||||
|
||||
newProduct->next = NULL;
|
||||
newProduct->prev = NULL;
|
||||
newProduct->next = NULL;
|
||||
newProduct->prev = NULL;
|
||||
|
||||
return newProduct;
|
||||
return newProduct;
|
||||
}
|
||||
|
||||
Product* insert_product(Product *head, int id, char *name, int quantity) {
|
||||
Product *new_product = create_product(id, name, quantity);
|
||||
if(new_product == NULL) {
|
||||
printf("Failed to create product\n");
|
||||
return new_product;
|
||||
}
|
||||
Product *new_product = create_product(id, name, quantity);
|
||||
if(new_product == NULL) {
|
||||
printf("Failed to create product\n");
|
||||
return new_product;
|
||||
}
|
||||
|
||||
if(head == NULL) {
|
||||
return new_product;
|
||||
}
|
||||
if(head == NULL) {
|
||||
return new_product;
|
||||
}
|
||||
|
||||
Product *temp = head;
|
||||
while(temp->next != NULL) {
|
||||
temp = temp->next;
|
||||
}
|
||||
Product *temp = head;
|
||||
while(temp->next != NULL) {
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
temp->next = new_product;
|
||||
return head;
|
||||
temp->next = new_product;
|
||||
return head;
|
||||
}
|
||||
|
||||
void print_all_products(Product *head) {
|
||||
Product *temp = head;
|
||||
while(temp != NULL) {
|
||||
printf("Product ID: %d\n", temp->id);
|
||||
printf("Product Name: %s\n", temp->name);
|
||||
printf("Product Quantity: %d\n\n", temp->quantity);
|
||||
temp = temp->next;
|
||||
}
|
||||
void print_products(Product *head) {
|
||||
Product *temp = head;
|
||||
while(temp != NULL) {
|
||||
printf("Product id: %d\n", temp->id);
|
||||
printf("Name: %s\n", temp->name);
|
||||
printf("Quantity: %d\n\n", temp->quantity);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -58,5 +59,5 @@ int main() {
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user