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;
}
Related
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;
}
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;
}
I'm having trouble with my assignment and was hoping to get some help.
I'm suppose to have two structs, volunteer and employee, and a union 'person' that takes firstname, lastname, telenumber + either volunteer or employee as a struct.
I have had experience using unions + structs before, but I'm suppose to have additional information in the union. I was wondering how I can set it up properly, this is what I have so far.
typedef struct volunteer{
int hours;
int tasksCompleted;
}student;
typedef struct employee{
float salary;
int serviceYears;
int level;
}employee;
typedef union person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
//employee e;
//volunteer;
}person;
Any help would be great. This is the task instruction I'm stuck on.
Create a person record that consists of the common records: first name, family name and telephone and a union between the volunteer and employee record. Make sure that you add a field to discriminate between the two records types employee or volunteer.
I think you are overthinking this: you need a structure to represent person. The key part is
"and a union between the volunteer and employee record."
typedef enum { employee_person, volunteer_person } person_type;
typedef struct person{
char firstName[20];
char familyName[20];
char telephoneNum[10];
person_type type;
union {
struct employee employee;
struct volunteer volunteer;
};
}person;
This should do what you ask:
typedef struct volunteer {
int hours;
int tasksCompleted;
} student;
typedef struct employee {
float salary;
int serviceYears;
int level;
} employee;
struct person {
char firstName[20];
char familyName[20];
char telephoneNum[10];
bool is_employee;
union {
employee e;
student s;
} info;
} person;
I need some help with Linked Lists.
I have figured out how to do individual linked list, but I am struggling when trying to implement multiple struct's and lists.
My last program was all used with Structs but now I must implement linked list's.
It says to use "External Pointers" in the functions to use in traversing through the various lists.
This is homework for one of my classes, I am not asking for you all to do it for me, but I am asking to help point me in the right direction.
The structs are as follows:
struct stockItem
{
char stockName[60];
char stockType[60];
int itemNumber;
float actualCost;
float markUp;
int totalCurrentInventory;
int monthlyRestock;
float price; //stores actual cost + markup
};
struct roomData
{
float widthFeet, widthInch;
float lengthFeet, lengthInch;
char roomName[100];
int roomNumberOfType;
char roomType[6]; //char of room type
int roomStock[100][2]; //for storing each room stock types
int roomHasStock; //if the room has a stock avaliable
int roomStockCount; //how many stocks the room has
float area; // sq ft
float rentalRate;
float profitsPerRoom;
float netProfit;
float grossProfit;
char stockLine[200];
};
struct staffData
{
char firstName[100];
char lastName[100];
char fullName[100];
int employeeNumber;
char typeOfEmployee[10];
char payType[10];
float hourlyWage;
float salary;
int hours;
char address[150];
char city[150];
char state[10];
int zip;
char phone[30];
float yearlyTotalPay;
struct hireDate //holds staff hire date
{
int month;
int day;
int year;
}hireDate;
struct birthDate //holds staff birth date
{
int month;
int day;
int year;
}birthDate;
};
typedef struct YourStructNode_ {
struct YourStructNode_ * next;
struct YourStructNode_ * prev;
YourStruct data;
} T_YourStructList;
Replace "YourStruct" by the name of your structs to make a doubly linked list.
Even if you make more than once T_XXXX_List with this pattern you should manipulate the list with the same function since the two first fields of T_Node is always the same.
Write add, insert, remove functions to manipulate this structure.
Is your linked list supposed to utilize the structs that you have developed? This way you have a linked list where each node contains an instance of all of those structs you listed.
struct node {
struct node *left;
struct node *right;
roomData room;
stockItem stock;
staffData staff;
hireDate hire;
birthDate birth;
};