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