Linked List Help, Singly Linked (Multiple Structures) (C Programming) - c

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;
};

Related

making an array with a specific size that includes elements in data structure datatype

I want to make an array with 100 elements that include structure data type in C
#include <stdio.h>
struct student{
char name[20];
float grade[15];
struct date{
int day;
int month;
int year;
};
};
int main(){
struct std[100];
}
With a structure type like this:
struct student{
char name[20];
float grade[15];
struct date_tag{
int day;
int month;
int year;
} date;
};
You can do things like this
int main(){
struct student std[100];
std[99].grade[14]=1.5;
std[99].grade[14].date.day=28;
}
The identifier date_tag in that code is called a struct tag.
The corresponding type is struct date_tag and it is used to define a member of of that type within the the type struct student. The member is called date.
Within main() an array of struct students is defined and then accessed for demonstration purposes.

Build a struct with a struct inside

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;
}

Storing date and time in a linked list in C

struct Node
{
int id;
char name[25];
int age;
int No;
int RoomNo;
struct Node* next;
}*Test;
14;Jack;25;2445201;402;20/08/2020 11:00
That is the struct and content of the txt file. I want to store each data into linked list until a semicolon. But I couldn't store date should I divide the date or can I store it as one. Because I tried to store it as string but didn't work, I cant store it as integer aswell.
fscanf(Print, "%d;%[^;];%d;%d;%d", &Test->id, Test->name, &Test->age, &Test->No, &Test->RoomNo);
suggest:
struct Node
{
int id;
char name[25];
int age;
int No;
int RoomNo;
char date[11];
char time[6];
struct Node* next;
};
struct Node myStruct;
struct Node *ptr2myStruct = &myStruct;
What are you using to extract the line of data?
Suggest: 1) use fgets() to input the whole line. 2) use sscan() or strtok() to extract the fields from the whole line.

Pointer to pointer to nested structure

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;

C-Void pointer inside a generic list (Contains stucture)

I have 2 different stuctures but i need to contain them in the same generic list.
The structures are :
typedef struct Val1{
int num1;
int num2;
int num3;
}Val1;
typedef struct Val2{
char name[50];
char surname[50];
int ID;
}Val2;
And the list is:
typedef stuct list {
void *value;
struct node *next;
}list;
typedef struct L_head{
node *head;
int num_members;
}L_head;
I need to use the same list implementation but the list needs to handle both of the
strucrure types.I cant figure out how to initialize the list and put some elements in the list .Any advice will be helpfull.
The canonical solution for C would add a common initial field having distinct values to both structures.
typedef struct Val1 {
int discriminator;
int num1;
int num2;
int num3;
} Val1;
typedef struct Val2 {
int discriminator;
char name[50];
char surname[50];
int ID;
} Val2;
If neccessary, you can define a new struct instead.
That has the advantage of preserving the previous layout and alignment guarantees:
struct packed {
int discriminator;
union {struct Val1;struct Val2};
};
Anyway, you could integrate it directly into the node:
typedef stuct node {
struct node *next;
int discriminator; /* You might want to reserve 0 for no content */
union {struct Val1;struct Val2};
} node;
The technical term of the solution is "discriminated union".
You can have a two element array of void pointers - One pointing to struct of type 1 and another to type 2. This should help to get the right structures with the right casting.
If you are able to distinguish between the type of each of the nodes, you could resort to C unions, like in:
typedef struct Val1{
int num1;
int num2;
int num3;
} Val1;
typedef struct Val2{
char name[50];
char surname[50];
int ID;
} Val2;
union {
Val1 val1;
Val2 val2;
} Val;
For adding the list items, you need to cast them to (void*).
In order to determine the type again upon access, you could add another field(enum) to the list, which stores it's type.
I bet this is some training or homework. If C++ is possible:
start using std::list or boost::list implementation.

Resources