I am trying to do this data stucture in C. Connect another structure to another structure. Like this:
struct room
{
int roomnumber;
struct room * nextRoom;
struct person * personList;
}*top=NULL,*temp=NULL,top1;
struct person
{
int personnumber;
struct person *next;
}*node=NULL,temp1;
Struct room has a pointer to struct person. I am having a hard time on connecting it to another struct. Is this correct?
Here is my function
void insert()
{
int val;
printf("enter value: ");
scanf("%d",&val);
newnode=create_node(val);
if(top->personList==NULL)
{
top->personList=newnode;
}
else
{
node->next=newnode;
node=newnode;
}
}
Insert a person to the room. Room is like created already. create_node() is the one who does the malloc()
Here's a version that doesn't use globals, which makes your functions much more general and reusable:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct person
{
int personNumber;
struct person *next;
} person; /* NOTE: this is a typedef, not a global declaration like you did */
typedef struct room
{
int roomNumber;
person *personList;
person *lastInList;
struct room *nextRoom;
} room; /* NOTE: this is a typedef, not a global declaration like you did */
person *create_person(int val)
{
person *ret = calloc(1, sizeof(*ret));
if (ret)
{
ret->personNumber = val;
ret->next = NULL;
}
return ret;
}
int room_insert(room *r, int val)
{
person *p = create_person(val);
if (NULL == p)
return -1;
if (NULL != r->personList)
r->lastInList->next = p;
else
r->personList = p;
r->lastInList = p;
return 0;
}
int main(int argc, char **argv)
{
room my_room = { 0 }; /* important! initializes pointers to NULL */
room my_other_room = { 0 }; /* important! initializes pointers to NULL */
person *p;
room_insert(&my_room, 5);
room_insert(&my_other_room, -5);
room_insert(&my_room, 10);
room_insert(&my_other_room, -10);
room_insert(&my_room, 15);
room_insert(&my_other_room, -15);
for (p = my_room.personList; p; p = p->next)
printf("%d\n", p->personNumber);
for (p = my_other_room.personList; p; p = p->next)
printf("%d\n", p->personNumber);
return 0;
}
Related
Im doing this program for managing students enrolled in each subject.
The system is composed by a linked list of subjects and a linked list of students. Each subject has a linked list of pointers for its students.
Each student has a list of pointers to their respective subjects.
None of the linked lists have headers.
Im trying to create the function to build the list of pointers to the subjects of each student but i dont understand how can i create it.
The structs i have so far are:
typedef struct node_subject * ListSubjects;
typedef struct node_subject {
char *name;
struct node_ptrStudent *Students;
ListSubjects next;
}NodeSubject;
typedef struct node_student * ListStudents;
typedef struct node_student {
char *name;
int num;
struct node_ptrSubject *Subjects;
ListStudents next;
}NodeStudent;
typedef struct node_ptrSubject * ListPtrSubjects;
typedef struct node_ptrSubject {
ListSubjects subjects;
ListPtrSubjects next;
}ListPtrSubjects;
typedef node_ptrStudent * ListPtrStudents;
typedef struct node_ptrStudent {
ListStudents student;
ListPtrStudents next;
}ListPtrStudents;
void createListPtrSubjects (ListSubjects);
The Linux kernel style says "It’s a mistake to use typedef for structures and pointers." https://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs; whether or not one agrees with this, I think you have to many typedef; it's kind of confusing.
If you were programming a relational database, which you kind of are, this would be an associative entity; see https://en.wikipedia.org/wiki/Associative_entity. Also, make sure that your subjects have a primary key.
For example,
#include <stdlib.h> /* EXIT_ realloc */
#include <limits.h> /* INT_MAX */
#include <errno.h> /* errno */
#include <stdio.h> /* perror printf */
#include <assert.h> /* assert */
struct Subject {
int id; /* Primary key. */
const char *name;
struct Takes *takes_head;
};
struct Student {
int id; /* Primary key. */
const char *name;
struct Takes *takes_head;
};
/* (Weak) Associative entity. */
struct Takes {
int subject_id, student_id; /* Compound key. */
struct Takes *next_subject, *next_student;
};
static struct School {
struct Subject *subjects;
int subjects_no;
struct Student *students;
int students_no;
} school;
static struct Subject *lookup_subject(const int id) {
if(id >= school.subjects_no) return 0;
return school.subjects + id;
}
static struct Subject *add_subject(const char *name) {
struct Subject *subjects, *s;
assert(name);
if(school.subjects_no == INT_MAX) { errno = ERANGE; return 0; }
/* fixme: realloc is slow; eg, double capacity. */
if(!(subjects = realloc(school.subjects,
sizeof *school.subjects * (school.subjects_no + 1)))) return 0;
school.subjects = subjects;
s = school.subjects + school.subjects_no;
s->id = school.subjects_no;
s->name = name;
s->takes_head = 0;
school.subjects_no++;
return s;
}
/* May output in a static buffer. */
static const char *subject_to_string(const int id) {
struct Subject *const subject = lookup_subject(id);
static char str[256] = ""; /* Static buffer. */
if(!subject) { sprintf(str, "<subject id %d>", id); return str; }
return subject->name;
}
static struct Student *lookup_student(const int id) {
if(id >= school.students_no) return 0;
return school.students + id;
}
static struct Student *add_student(const char *name) {
struct Student *students, *s;
assert(name);
if(school.students_no == INT_MAX) { errno = ERANGE; return 0; }
if(!(students = realloc(school.students,
sizeof *school.students * (school.students_no + 1)))) return 0;
school.students = students;
s = school.students + school.students_no;
s->id = school.students_no;
s->name = name;
s->takes_head = 0;
school.students_no++;
printf("%s is assigned id %d.\n", s->name, s->id);
return s;
}
/* May output in a static buffer. */
static const char *student_to_string(const int id) {
struct Student *const student = lookup_student(id);
static char str[256] = ""; /* Static buffer. */
if(!student) { sprintf(str, "<student id %d>", id); return str; }
return student->name;
}
static struct Takes *lookup_takes(const int student_id, const int subject_id) {
const struct Student *const student = lookup_student(student_id);
struct Takes *t;
if(!student) return 0;
for(t = student->takes_head; t && t->subject_id != subject_id; t = t->next_student);
return t;
}
static struct Takes *add_takes(const int student_id, const int subject_id) {
struct Subject *const subject = lookup_subject(subject_id);
struct Student *const student = lookup_student(student_id);
struct Takes *t = lookup_takes(student_id, subject_id);
printf("%s enrols in %s.\n", student_to_string(student_id),
subject_to_string(subject_id));
/* Already have it. */
if(t) return t;
/* Or else make a new. */
if(!subject || !student) { errno = EDOM; return 0; }
if(!(t = malloc(sizeof *t))) return 0;
t->subject_id = subject_id;
t->student_id = student_id;
t->next_subject = subject->takes_head, subject->takes_head = t;
t->next_student = student->takes_head, student->takes_head = t;
return t;
}
static void print_subject(const int subject_id) {
struct Subject *const subject = lookup_subject(subject_id);
struct Takes *t;
printf("_Subject: %s._\n", subject_to_string(subject_id));
if(!subject) return;
for(t = subject->takes_head; t; t = t->next_subject)
printf("%s takes %s.\n", student_to_string(t->student_id),
subject_to_string(t->subject_id));
}
static void print_student(const int student_id) {
struct Student *const student = lookup_student(student_id);
struct Takes *t;
printf("_Student: %s._\n", student_to_string(student_id));
if(!student) return;
for(t = student->takes_head; t; t = t->next_student)
printf("%s takes %s.\n", student_to_string(t->student_id),
subject_to_string(t->subject_id));
}
int main(void) {
const char *why = 0;
do { /* Try. */
struct Subject *subject;
struct Student *student;
int herbology_id, defense_id, potions_id;
int hermione_id, ron_id, harry_id;
why = "Herbology";
if(!(subject = add_subject(why))) break;
herbology_id = subject->id;
why = "Defense";
if(!(subject = add_subject(why))) break;
defense_id = subject->id;
why = "Potions";
if(!(subject = add_subject(why))) break;
potions_id = subject->id;
why = "Hermione";
if(!(student = add_student(why))) break;
hermione_id = student->id;
why = "Ron";
if(!(student = add_student(why))) break;
ron_id = student->id;
why = "Harry";
if(!(student = add_student(why))) break;
harry_id = student->id;
why = "enrol";
if(!add_takes(hermione_id, defense_id)
|| !add_takes(hermione_id, potions_id)
|| !add_takes(hermione_id, herbology_id)
|| !add_takes(ron_id, herbology_id)
|| !add_takes(harry_id, potions_id)) break;
print_subject(herbology_id);
print_subject(defense_id);
print_subject(potions_id);
print_student(hermione_id);
why = 0;
} while(0); if(why) perror(why); /* Catch. */
/* fixme: free memory in 'final' block. Remember to free Takes. */
return why ? EXIT_FAILURE : EXIT_SUCCESS;
}
i don't know why i can't even add 4th element...
(im working on windows with mingw)
this is my code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
typedef struct ds_list_element {
char value[MAX];
struct ds_list_element *next;
}* ds_list;
int ds_list_empty(ds_list id) { // id listy
if (id == NULL) return 1;
else return 0;
}
ds_list ds_list_add(ds_list id, char add[MAX]) {
ds_list temp;
temp = (ds_list)(malloc(sizeof(ds_list)));
strcpy(temp->value,add);
temp->next = id;
return temp;
}
void ds_list_print(ds_list id) {
if (ds_list_empty(id) == 0) {
printf("%s\n",id->value);
ds_list_print(id->next);
}
}
int main () {
ds_list my_list = NULL;
my_list = ds_list_add(my_list,"one");
my_list = ds_list_add(my_list,"two");
my_list = ds_list_add(my_list,"three");
my_list = ds_list_add(my_list,"four");
ds_list_print(my_list);
return 0;
}
and the result is:
four
three
two
y
Press any key to continue . . .
i don't know why it is happening. everything should work fine.
my friend told me it is working on ubuntu...
temp = (ds_list)(malloc(sizeof(ds_list)));
will be
temp = malloc(sizeof(*temp)));
You want to allocate memory for struct ds_list_element not struct ds_list_element*. Don't hide pointers behind typedef name. It rarely helps.
Also you should check the return value of malloc and the casting is not needed.
Use ds_list as structure not a pointer
typedef struct ds_list_element {
char value[MAX];
struct ds_list_element *next;
}ds_list;
and allocate memory for the structure not a pointer.
Working program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
typedef struct ds_list_element {
char value[MAX];
struct ds_list_element *next;
}ds_list;
int ds_list_empty(ds_list *id) { // id listy
if (id == NULL) return 1;
else return 0;
}
ds_list * ds_list_add(ds_list *id, char add[MAX]) {
ds_list *temp;
temp = (malloc(sizeof(ds_list)));
strcpy(temp->value,add);
temp->next = id;
return temp;
}
void ds_list_print(ds_list *id) {
if (ds_list_empty(id) == 0) {
printf("%s\n",id->value);
ds_list_print(id->next);
}
}
int main () {
ds_list *my_list = NULL;
my_list = ds_list_add(my_list,"one");
my_list = ds_list_add(my_list,"two");
my_list = ds_list_add(my_list,"three");
my_list = ds_list_add(my_list,"four");
ds_list_print(my_list);
return 0;
}
Output:
four
three
two
one
I have a struct and in that a struct i have a character pointer but and i am creating different instances of this struct but when i am changing the pointer in one struct the other is also changing.
#include <stdio.h>
#include <stdlib.h>
typedef struct human{
int age;
char name[100];
} Human;
int main(){
FILE *s = fopen("h.txt","r");
if(s==NULL){
printf("file not available");
}
for(int i=0 ;i<5;i++){
Human h;
fscanf(s,"%d",&h.age);
fscanf(s,"%s",h.name);
insertintolinkedlist(h);
// this method is going to insert the human into the linked list
}
return 0;
}
what is happening that all humans in the linked list have different ages but same name!
You need to allocate memory to hold the name.
char* name is just a pointer - it has no memory for saving the name.
You change it to
char name[100];
Remember to check that the names you put into Human.name isn't longer than 100 characters.
To use a linked list you can do something like:
typedef struct human{
int age;
char name[100];
struct human* next;
} Human;
int main()
{
Human* head = NULL;
Human* tail = NULL;
for(.....)
{
Human* h = malloc(sizeof(Human));
if (head == NULL) head = h;
if (tail != NULL)
{
tail->next = h;
}
tail = h;
h->next = NULL;
h->age = ....;
strncpy(h->age, "..name..", 100);
}
// ..... other code
// Remember to free all allocated memory
}
Is there any way to take a string input(as like we take for any integer) by linked list??
For example:This code is showing run time error:
struct node
{
char c;
struct node *link;
};
while(1)
{
val=getch();
if(val!=10)
add(&a[i],val);
else
break;
}
and I want to take any input string like - "asdfghj", of which string length was not known?
Given that you have a LinkedList-class that acts as an interface to the linked list and that it has the function addNode() that adds a node in a correct manner to the list.
I also assume that what you want to know is how to make every char in the inputted string a node in the linked list and that you know how to manage a linked list.
And assuming you're using C++11
int main()
{
LinkedList list;
string input;
cin >> input;
for(auto i: input)
{
list.addNode(i);
}
}
example for C
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char c;
struct node *link;
} Node;
typedef struct s_ {
Node *top;
Node *curr;
} String;
Node *Node_new(char ch){
Node *p = calloc(1, sizeof *p);
p->c = ch;
return p;
}
String *String_new(void){
String *p = calloc(1, sizeof *p);
return p;
}
void String_drop(String *s){
Node *p = s->top;
while(p){
s->curr = p;
p = p->link;
free(s->curr);
}
//s->top = s->curr = NULL;
free(s);
}
void String_add(String *s, char c){
if(s->top == NULL){
s->curr = s->top = Node_new(c);
} else {
s->curr = s->curr->link = Node_new(c);
}
}
String *get_string(FILE *fp){
String *s = String_new();
int ch;
while(EOF!=(ch=fgetc(fp)) && ch !='\n'){
String_add(s, (char)ch);
}
return s;
}
void put_string(String *s){
Node *p;
for(p = s->top; p ; p = p->link)
putchar(p->c);
putchar('\n');
}
int main(void) {
String *s = get_string(stdin);
put_string(s);
String_drop(s);
return 0;
}
You can think easily. As You just can declare a string variable instead of char. And after that You can take input normally by creating a struct variable. For example:
#include <bits/stdc++.h>
using namespace std;
struct node
{
string s;
struct node *link;
};
int main(){
node ob;
cin>>ob.s;
cout<<ob.s;
}
Hello i am trying to return a struct from a function but i cant find a way to do so without declaring the struct as global. How can this be done? Here is the code (THIS WORKS AS IT IS)
...
void log_in();
struct node
{
char name_log[20];
int passlog;
int user_point;
}tmp;
int main()
{
...
else if(sel=='2')
{
log_in();
if (tmp.passlog==TRUE)
logged_in(tmp.name_log,tmp.user_point); //and here i want to use the retun values
}
void log_in()
{
... //make the changes in the struct
}
...
What i want to achieve is to place the struct node declaration within main but sadly it wont work. So here is what i am trying to do: (THIS DOESN'T WORK)
...
struct node log_in();
int main() {
...
else if(sel=='2') {
struct node //here is where i want to declare
{
char name_log[20];
int passlog;
int user_point;
}tmp;
log_in();
if (tmp.passlog==TRUE)
logged_in(tmp.name_log,tmp.user_point); //and here i want to use the retun values
}
struct node log_in()
{
...
return tmp;
}
...
else if(sel=='2') //or within this block but I don't know how.
{ struct node tmp;
tmp=log_in();
if (tmp.passlog==TRUE)
logged_in(tmp.name_log,tmp.user_point); //and here I want to use the return values
}
and inside the function log_in()
struct node log_in()
{
struct tmp
...
return tmp;
}
use a local variable inside the function and return this variable. Assign it to another variable inside main().
Declare the structure first, then create the variable temp. Like this:
struct node
{
char name_log[20];
int passlog;
int user_point;
};
Then you can create the local variable like
struct node tmp;
Pass a pointer to the struct node to your log_in function and have it return a boolean value so the caller can check whether logging in succeeded or didn't. (Note I'm trying to guess what you want to achieve, and I might be guessing wrong.)
#include <stdio.h>
#include <string.h>
struct node {
char name_log[20];
int passlog;
int user_point;
};
int log_in(char, struct node *);
int log_in(char sel, struct node * tmp) {
int ret = 0;
if (sel == '2') {
ret = 1;
strcpy( tmp->name_log, "Gonzo" );
tmp->passlog = 33;
tmp->user_point = 99;
}
return ret;
}
int main(int argc, char ** argv) {
struct node tmp;
char sel = argv[1][0];
if ( log_in(sel, &tmp) ) {
// tmp initialized
printf( "%s, %d, %d\n", tmp.name_log, tmp.passlog, tmp.user_point );
}
else {
// tmp not initialized
}
}
Call passing 2 on the command line. (If you don't, undefined behaviour.)
If you want to use some struct in 2 different routines - you must declare it outside of both of them since they both have to see how this struct is structured.
BTW - you invoke log_in but do not use its return value.
You can't operate on a type that is unknown. If log_in() doesn't know the definition of struct node, it can't use it directly. The only thing it can do is somehow receive a pointer to a variable of this type and then either treat it as raw data (sequence of bytes) or cast said pointer to a pointer to a known to log_in() type and work with that.
You can also redefine struct node inside of log_in(), which is a way of making log_in() operate on a known type:
void log_in(void*);
void logged_in(char*, int);
int main(void)
{
int sel = '2';
if (sel == '2')
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} tmp;
log_in(&tmp);
if (tmp.passlog)
logged_in(tmp.name_log, tmp.user_point);
}
return 0;
}
void log_in(void* n)
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} *p = n;
p->passlog = 1;
}
void logged_in(char* name, int point)
{
}
If you don't want to pass tmp by a formal reference into log_in(), you must make it available globally. For example like this:
void log_in(void);
void logged_in(char*, int);
void* pTmp;
int main(void)
{
int sel = '2';
if (sel == '2')
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} tmp;
pTmp = &tmp;
log_in();
if (tmp.passlog)
logged_in(tmp.name_log, tmp.user_point);
}
return 0;
}
void log_in(void)
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} *p = pTmp;
p->passlog = 1;
}
void logged_in(char* name, int point)
{
}