My code will take the info from input.txt that students informations. hold them in a linked list and then sort them by birth year then print. when ı run, it print meanless numbers and every time ı run, the output changes. but when ı debug code, the output will be correct.
When ı run my code the output: Student names in ascending order by birthday: -2013265920000 M-2013265920-2013265920-2013265920-2013265920 Ali Pehlivan;2005 -2013265920-2013265920-20132659200
But I debug my code and output: Student names in ascending order by birthday: Ali Pehlivan;2005 Selami Kilic;2002 Esma Sultan;2001 Mehmet Ali Sarsil;2000
my input file 'input.txt' as: 040160811;Ali Pehlivan;2005 040180224;Mehmet Ali Sarsil;2000 820190040;Esma Sultan;2001 150190207;Selami Kilic;2002
it seems about my 'sortAndPrintByBirth' function. But ı cannot find any mistake. I think its a memory stuff.
#include <stdio.h>
#include <stdlib.h>
struct n {
char number[9];
char name;
int year;
struct n* next;
};
typedef struct n node;
struct s_node {
char* number;
struct s_node *next;
}*stack = NULL;
int isLetter(char character);
int isNumber(char character);
int charToInt(char character);
node** sortAndPrintByBirth(node* *list);
node** sortAndPrintByFaculty(node* *list);
int length(node** list);
void push(char* value);
char* pop();
void display();
int main() {
node* Student;
Student = (node *) malloc(sizeof(node));
node* head1;
node* head;
head = Student;
head1 = head;
node* Students[100];
FILE* file;
file = fopen("C:\\Users\\PC\\CLionProjects\\untitled1\\input.txt", "r");
char fchar;
fchar = fgetc(file);
int switchNY = 0; // 0 for school number, 1 for birth year
int studentCounter = 0;
while(fchar != EOF){ /// Dosyadan veri alma
if(isLetter(fchar)){
head1->name = fchar;
head1->year = (int) NULL;
head1->next = malloc(sizeof(node*));
head1 = head1->next;
fchar = fgetc(file);
}else if(isNumber(fchar)){
if(switchNY == 0){
for(int i = 0; i < 9; i++){
head1->number[i] = fchar;
head1->year =(int) NULL;
head1->number[i+1] = (char) NULL;
fchar = fgetc(file);
}
}else {
for (int i = 0; i < 4; i++) {
head1->year = charToInt(fchar);
head1->next = malloc(sizeof(node *));
head1 = head1->next;
fchar = fgetc(file);
}
if (fchar == EOF) {
Students[studentCounter] = head; ///en son yapmaya çalıştığın şey her \n karakterinde
Students[studentCounter + 1] = NULL; ///yeni student tanımlayıp onun adresini arrayde tutmak
fchar = fgetc(file);
Student = (node *) malloc(sizeof(node));
head = Student;
head1 = head;
studentCounter++;
}
switchNY = 0;
}
}else if(fchar == '\n'){ /// kod ; lerde buraya giriyo, onu düzelt
Students[studentCounter] = head; ///en son yapmaya çalıştığın şey her \n karakterinde
Students[studentCounter+1] = NULL; ///yeni student tanımlayıp onun adresini arrayde tutmak
fchar = fgetc(file);
Student = (node *) malloc(sizeof(node));
head = Student;
head1 = head;
studentCounter++;
}else if(fchar == ';'){
head1->year = (int) NULL;
if(switchNY == 1){
head1->name = fchar;
}
switchNY = 1;
fchar = fgetc(file);
head1->next = malloc(sizeof(node*));
head1 = head1->next;
}
}
fclose(file);
sortAndPrintByBirth(Students);
//sortAndPrintByFaculty(Students);
return -1;
}
int isLetter(char character) {
return ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character == ' '));
}
int isNumber(char character) {
return (character >= '0' && character <= '9');
}
int charToInt(char character) {
if (character >= '0' && character <= '9') {
return character - '0';
} else {
return -1; // Eğer karakter bir sayı değilse -1 döndür
}
}
node** sortAndPrintByBirth(node* *list) {
int i = length(list);
node* result[i];
for(int a=0; a<i; a++){
result[a] = list[a]->number;
}
for(int count = 0; count < i-1; count++){
for(int step = 0; step < i-1 ; step++){
node *iter1;
node *iter2;
iter1 = result[step];
iter2 = result[step+1];
int year1 = 0,year2 = 0;
while (iter2 != NULL){
while ((void *) iter1->next->year == NULL) { ///void* eklendi, derleyicinin isteği
iter1 = iter1->next;
}
while ((void *) iter2->next->year == NULL) { ///void* eklendi, derleyicinin isteği
iter2 = iter2->next;
}
for (int j = 0; j < 4; ++j) {
iter1 = iter1->next;
year1 = 10 * year1 + iter1->year;
}
for (int j = 0; j < 4; ++j) {
iter2 = iter2->next;
year2 = 10 * year2 + iter2->year;
}
break;
}
if(year1 < year2){
node* temp;
temp = result[step];
result[step] = result[step+1];
result[step+1] = temp;
}else if(year2 == year1){
continue;
}
}
}
node* iter3;
printf("Student names in ascending order by birthday:\n");
for (int j = 0; j < i; ++j) {
iter3 = result[j]->next;
while(iter3->year == (int )NULL){
printf("%c",iter3->name);
iter3 = iter3->next;
}
for (int k = 0; k < 4; ++k) {
printf("%d",iter3->year);
iter3 = iter3->next;
}
printf("\n");
}
return result;
}
node** sortAndPrintByFaculty(node* *list){
int i = length(list);
node* result[i];
char* temp[i];
for(int a=0; a<i; a++){
temp[a] = list[a]->number;
}
/// temp = "040160811","040180224","820190040","150190207"
for(int count = 0; count < i-1; count++){
for(int step = 0; step < i-1 ; step++){
for(int digit = 0; digit < 3 ; digit ++){
if(charToInt(temp[step][digit]) < charToInt(temp[step+1][digit])){
char* c;
c = temp[step];
temp[step] = temp[step+1];
temp[step+1] = c;
break;
}else if(charToInt(temp[step][digit]) == charToInt(temp[step+1][digit])){
continue;
}else{
break;
}
}
}
}
for (int j = 0; j < i; ++j) {
push(temp[j]); ///Değerler büyükten küçüğe stack'e push'landı.
}
//display();
printf("School numbers by the faculty codes in ascending order:\n");
for (int j = 0; j < i; ++j) {
printf("%s\n", pop());
}
return result;
}
int length(node** list){
int value = 0;
while(list[value] != NULL){
value++;
}
return value;
}
void push(char* value) {
struct s_node *m;
m=(struct s_node*)malloc(sizeof(struct s_node ));
m->number= value ;
m->next=stack;
stack=m;
}
void display() {
struct s_node *temp=stack;
while(temp!=NULL) {
printf("%s\t", temp->number);
temp=temp->next;
}
}
char* pop() {
struct s_node *temp ;
if (stack == NULL) {
printf("\nSTACK is Empty.");
} else {
char* i = stack->number;
temp = stack;
stack = stack->next;
free(temp); // sadece temp'i serbest bırak, number için ayrı bir free yapmaya gerek yok
return i;
}
return NULL;
}
At least these problems:
Insufficient allocation
Member .next
is a pointer to type struct n
AKA node
. Allocation should be on the size of the referenced data, not the size of the pointer.
// head1->next = malloc(sizeof(node*));
head1->next = malloc(sizeof head1->next[0]);
Save time, compiler with all warnings enabled