Add Q2.c
This commit is contained in:
54
Q2.c
Normal file
54
Q2.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct Product {
|
||||||
|
int id;
|
||||||
|
char name[30];
|
||||||
|
int quantity;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
newProduct->next = NULL;
|
||||||
|
newProduct->prev = NULL;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(head == NULL) {
|
||||||
|
return new_product;
|
||||||
|
}
|
||||||
|
|
||||||
|
Product *temp = head;
|
||||||
|
while(temp->next != NULL) {
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp->next = new_product;
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
//void print_products
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user