I want to find the 5 cheapest products from all shops with O(n+m) (n=node length of each shop, m=products that are available). I have figured out the algorithm in my head but I can't translate into C. I will post the header file with the structs. Here's what I want to do:
For each shop, sort its products by their price and then create another product list with the 5 cheapest products of each shop and then sort() all of them and print the first 5.
struct Shop {
int sid;
int type;
struct Product *products;
struct Shop *next;
struct Shop *prev;
};
struct Product {
int pid;
int quantity;
int price;
struct Product *next;
};
/* Global, pointer to the header node of the circular, double-linked list of shops */
struct Shop *H;
/* Global, array of pointers for the use in event 'Find Cheapest Products' */
struct Product *CheapestProducts[5];
EDIT: it just adds the products to newProd (like TMP list)
and prints the products, quantity and price for each shop . its sorted by price.
struct Shop *shop=H;
struct Product *prod;
struct Product *newProd;
int count=0;;
if(shop==NULL) return 0;
while(shop->next!=NULL)
{
prod=shop->products;
//newProd=prod;
sortPrice(prod);
while(prod!=NULL)
{
newProd=prod;
printf("<%d:%d:%d>\t" ,newProd->pid,newProd->quantity,newProd->price);
//newProd=prod;
newProd=newProd->next;
prod=prod->next;
//sortPrice(newProd);
//sortPrice(newProd);
// printf("<%d:%d:%d>\t" ,newProd->pid,newProd->quantity,newProd->price);
}
printf("\n");
//printf("<%d:%d:%d>\t" ,newProd->pid,newProd->quantity,newProd->price);
shop=shop->next;
}
Related
A lecturer teaches 4 different classes.
The number of students in each class may differ from each other.
There are two different linked list structures for holding courses and students.
In the structure of the class (nodeClass), information about which class it is (classID) and the average of the midterm exams of the students in the class (classMidtermAverage) are kept. Besides these, there is a pointer pointing to the next class and a pointer pointing to a node belonging to the structure (nodeStudent) used to define students' information.
In the structure called nodeStudent, there is a student's id, midterm grade and a pointer showing the next student in the same class.
The program takes the student number and midterm grade from the user as input. Students whose student number starts with 66 are in the 1st class, with 77 in the 2nd class, students starting with 88 are in the 3rd class, and students starting with 99 are in the 4th class. Students must appear in the linked list sequentially. Sorting will be done in descending order according to the midterm grade. If the notes are the same, the lower number should be first in the list. The sorted list should be preserved by adding it to the correct location while adding. After all students are added to the list, the midterm average of each class will be calculated and kept in the classMidtermAverage variable of the node generated from the nodeClass structure of the relevant class.
I know we have to create a nested linked list but I am having a hard time inserting the data to the main linked list (being the classes linked list) and the child linked list (students list)
struct nodeClass
{
int classID;
double classMidtermAverage;
struct nodeClass *next;
struct nodeStudent *studentPtr;
};
struct nodeStudent
{
int studentID;
int midterm;
struct nodeStudent *next;
};
struct nodeClass* insert(struct nodeClass **head, int id, int midterm) {
struct nodeStudent* newPtr;
struct nodeClass* temp= (struct nodeClass*) malloc(sizeof(struct nodeClass*));
temp->studentPtr = (struct nodeStudent*) malloc(sizeof(struct nodeStudent));
temp->studentPtr->studentID=id;
temp->studentPtr->midterm=midterm;
long classcode=id;
while (classcode>=100){
classcode=classcode/10;
}
if (classcode=66) {
temp->classID=i;
head=temp;
}
else if( classcode=77) {
temp->classID=2;
head->next=temp;
struct nodeClass* class2= head->next;
}
else if( classcode=88) {
temp->classID=3;
head->next->next=temp;
struct nodeClass* class3= head->next->next;
}
else if( classcode=99) {
temp->classID=4;
head->next->next->next=temp;
struct nodeClass* class3= head->next->next->next;
}
return head;
}
struct nodeClass* computeClassAverage(*head) {
nodeClass* tmp;
for( tmp=head; tmp)
}
void printAll(*head);
and the main function is
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "function.h"
int main()
{
struct nodeClass *head = NULL;
int id, midterm;
scanf("%d", &id);
while(id!=-1)
{
scanf("%d", &midterm);
insert(&head, id, midterm);
scanf("%d", &id);
}
computeClassAverage(head);
printAll(head);
return 0;
}
You seem to have some misunderstanding of this subject matter which I can't quite see, so I doubt that this answer will satisfy you, but I'll go out on a limb.
Here is a way to insert new elements into the linked list:
nodeStudent* insertStudent(nodeStudent *head, nodeStudent *newp)
{
newp->next = head;
return newp;
}
nodeClass* insertClass(nodeClass *head, nodeClass *newp)
{
newp->next = head;
return newp;
}
Notice that these functions add the new elements at the head of the list. It is not difficult to write functions to add elements after a given element, or find the end of the list and insert the new element there.
typedef struct Course {
char *CourseID;
char *CourseName;
struct Course* next;
} COURSE;
typedef struct Student{
int ID;
struct Student* next
}STUDENT;
I want to build a list of courses and then a list of students with the following
each student has an ID and some courses from the courses list and a grade of each course.
but how can I make that declaration inside the STUDENT struct? I cant understand that
For example
Student: 3049583222
Course: Biology (from course list)
grade: 30
and so on, every student could have all courses from the list.
My idea:
Linked List of Students.
Every student has a linked list of its courses with the grade.
The linked list elements have a pointer to the detailed course description.
// Description of a course
typedef struct Course {
char *CourseID;
char *CourseName;
} COURSE;
// Struct to hold the students grade, as a linked list to generate a list of all courses the student has.
typedef struct coursesTaken {
int grade;
COURSE* courseDescription;
struct coursesTaken* nextCourse;
} COURSES_TAKEN;
// The student with ID and pointer to his first course.
typedef struct Student{
int ID;
COURSES_TAKEN* firstCourse;
struct Student* next;
}STUDENT;
There can be multiple ways to do that, below shown are two approaches.
You can pick one and build on it.
typedef struct Course {
char *CourseID;
char *CourseName;
struct Course* next;
} COURSE;
typedef struct Student{
int ID;
}STUDENT;
//approach 1
#define NIDS 10
#define NCOURSES 20
typedef struct Student_Course_1{
STUDENT STU_ID[NIDS]; /* to store list of student ids */
COURSE STU_Courses[NCOURSES]; /* to store list of courses */
}COURSES_OF_STUDENT_1;
//approach 2
typedef struct Student_Course_2{
STUDENT STU_ID;
COURSE STU_Courses;
}COURSES_OF_STUDENT_2;
int main()
{
COURSES_OF_STUDENT_1 cs1; /* this has list of students and courses inside it */
COURSES_OF_STUDENT_2 cs2[NIDS]; /* you can use NIDS list of students and courses info with this */
return 0;
}
There are two structures for BST
typedef struct _price {
double price;
char* shopname;
} pricing;
typedef struct _bstnode {
int id;
pricing** pl;
int nr_of_prices;
struct _bstnode *left;
struct _bstnode *right;
struct _bstnode *parent;
} bstnode;
I need to make a function that can add and update price and shop name in BST Node. One Node may contain many shops and prices
void updatePrice(bstnode* root, int id, char* shop, double price){
//to do
}
I can add a single shop name and price but how to add several objects?
(*(root->pl))=malloc(sizeof (pricing)); // ---??
(*(root->pl))->price=price;
(*(root->pl))->shopname=shop;
If nr_of_prices belongs to pl, this will add dynamically one object:
int oldidx = root->nr_of_prices++;
root->pl = realloc( root->pl, root->nr_of_prices * sizeof(*root->pl));
root->pl[oldidx] = malloc(sizeof(pricing));
root->pl[oldidx]->price = price;
root->pl[oldidx]->shopname = shop;
I have 3 structures:
struct Product{
int IdOfProduct;
char *NameOfProduct;
char *CategoryOfProduct;
int PriceOfProduct;
Struct Product *NextPtr;
};
struct Customer{
int IdOfCustomer;
char *NameOfCustomer;
char *SurnameOfCustomer;
struct Baskets *ListOfBasket;
struct Customer *NextPtr;
}
*Header=NULL;
struct Basket{
int IdOfBasket;
struct Product *ProductList;
int AmountOfTotal;
struct Basket *NextPtr;
};
I took informations about customer from the user. I have customer's id name and surname. Now, the user can select one of the customers: The user enter the id of a customer that I listed by display function.So that, new basket will be added to that specific customer. How can i write this function ?
If you want to display the list of customers, I believe this is it:
void displayFunction(struct Customer *P)
{
while(P->next != NULL)
{
printf("%d %s %s",P->IdOfCustomer,P->NameOfCustomer,P->SurnameOfCustomer);
P = P->next;
}
}
I have a School struct that holds a linked list of Student structs and each of those Student structs hold a linked list of Courses structs. I am a bit puzzled about how I would be able to free both linked lists. I am particularly unsure about how I would free the Courses linked list since it is within another linked list.
struct Courses {
char *courseName;
int creditValue;
Courses *next;
} Courses;
struct Student {
char *studentName;
int studentAge;
Courses *coursesList; //First course (node)
Student *next;
} Student;
struct School {
char *schoolName;
int schoolAge;
Student *studentList; //First student (node)
} School;
If someone could show me an example on how I would be able to free both the linked lists that would be great!
You should think in terms of ownership: when you free an item, you must free everything it owns, that is everything his has a pointer to that is not owned by something else.
Along these lines, each list item owns the next item, every School owns its list of Students, every Student owns its list of Courses.
Your type definitions seem incorrect as you use the same identifier for types and variables. You should rewrite them this way:
typedef struct Course Course;
typedef struct Student Student;
typedef struct School School;
struct Course {
char *courseName;
int creditValue;
Course *next;
};
struct Student {
char *studentName;
int studentAge;
Course *courseList; //First course (node)
Student *next;
};
struct School {
char *schoolName;
int schoolAge;
Student *studentList; //First course (node)
};
The function to free everything:
void freeSchool(School *school) {
Student *student = school->studentList;
while (student) {
Course *course = student->courseList;
while (course) {
free(course->courseName); // if it was allocated
Course *nextCourse = course->next;
free(course);
course = nextCourse;
}
free(student->studentName); // if it was allocated
Student *nextStudent = student->next;
free(student);
student = nextStudent;
}
free(school->schoolName); // if it was allocated
free(school);
}
Note the similarity between the different levels. You can also split this function into separate freeSchool, freeStudent and freeCourse functions that you can use to handle individual object deletion in the program.
An elegant way to handle element deletion is for the freeXXX function to return the next element and free the node:
Courses *freeCourse(Course *course) {
Course *next = course->next;
free(course->courseName); // if it was allocated
free(course);
return next;
}
Student *freeStudent(Student* student) {
Student *next = student->next;
while (student->courseList) {
student->courseList = freeCourse(student->courseList);
}
free(student->studentName); // if it was allocated
free(student);
return next;
}
School *freeSchool(School *school) {
while (school->studentList) {
school->studentList = freeStudent(school->studentList);
}
free(school->schoolName); // if it was allocated
free(school);
return NULL;
}