Question

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ENTRY 100

    //define struct
typedef struct StudentRecords
{
    int StudentID; //must be of size 7 between 1000000 and 9999999
    char *Firstname; //= MALLOC(256*sizeof(char)); // must be any length and allocate memory dynamically.
    char *Lastname; //= MALLOC(256*sizeof(char));
    char *Department; //= MALLOC(256*sizeof(char));
    float GPA; // must be between 0 and 4
} STUDENTRECORDS; 

//define linked list structs
struct Node 
{
struct StudentRecords data;
struct Node* next;
struct Node* prev;
};

//define global head
struct Node* head;
struct Node* GetNewNode(struct StudentRecords Record)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = Record;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

//create a function to use the struct
void Insert(struct StudentRecords Record)
{
struct Node* newNode = GetNewNode(Record);
if (head==NULL) 
{
    head = newNode;
    return;
}

head->prev = newNode;
newNode->next = head;
head= newNode;
}

void Print() {
struct Node* temp = head;
printf("StudentID: ");
while (temp != NULL) 
{
    printf("%d\n", temp->data.StudentID);
    temp = temp->next;
}

}

void ReversePrint()
{   
struct Node* temp = head;
if (temp == NULL) 
{
    return;
}

while (temp->next != NULL)
{
    temp = temp->next;
}

printf("Reverse: ");
while (temp!= NULL) 
{
    printf("%d", temp->data);
    temp = temp->prev;
}
printf("\n");
}

int main()
{
    /*
    First job is read the file
    */

    //set variables
    int i=0;
    char filecontent, file_name[100];
    FILE *fp;

    STUDENTRECORDS StudentRecords[MAX_ENTRY];
    for(i=0;i<MAX_ENTRY;i++)
    {
        StudentRecords[i].Firstname = malloc(sizeof(char)*256);
        StudentRecords[i].Lastname = malloc(sizeof(char)*256);
        StudentRecords[i].Department = malloc(sizeof(char)*256);        
    }

    printf("Enter directory of file\n"); // instructs user to enter directory of file
    gets(file_name); //prompt use

    fp = fopen(file_name,"r"); //opens the file "r" is read mode for fopen()

    // here is a check to see if fp is empty and throw an error if so
    if (fp == NULL)
    {
        perror("Could not open file\n");
        //exit(EXIT_FAILURE);
    }

//this adds the content to the struct we created.
// and prints it
    i=0;
printf("Records in struct:\n");
    while(EOF!=fscanf(fp, "%d %s %s %s %f\n", &StudentRecords[i].StudentID, StudentRecords[i].Firstname, StudentRecords[i].Lastname, StudentRecords[i].Department, &StudentRecords[i].GPA))
    {

        printf("%d %s %s %s %f\n", StudentRecords[i].StudentID, StudentRecords[i].Firstname, StudentRecords[i].Lastname, StudentRecords[i].Department, StudentRecords[i].GPA);
        i++;
    }

printf("Creating linked list of structs...\n");
//Now we have to add it to a linked list.
for (i=0; i < sizeof(StudentRecords)/sizeof(StudentRecords[0]); i++) 
    Insert(StudentRecords[i]);


//Insert(StudentRecords[0]);
//Insert(StudentRecords[1]);

//print function for linked list
printf("Printing linked list...\n");
Print();    

    // fclose() must follow an fopen()
    fclose(fp);

    return 0;
}

Here is my entire code for a program I'm writing. I am to input a text file that looks like this:

2040003 AAAA BBBBBBBBB ComputerScience 3.4

2040002 AAA CCC ElectricalEngineering 3.01

2040005 AAAAAAAAAAAAAAAAA BBB ComputerScience 3.60

The output is to be sorted the the first int, or StudentID and separated by commas

2040002,AAA,CCC,ElectricalEngineering,3.01

2040003,AAAA,BBBBBBBBB,ComputerScience,3.45

2040005,AAAAAAAAAAAAAAAAA,BBB,ComputerScience,3.60

I was thinking of sorting the structs as they enter the linked list, rather than after. I think I can alter my Insert() function to place them in the right order, but everything I've tried hasn't worked. So here's the code with it simply printing the list without any sorting.

Thanks for any suggestions or pointers.

Was it helpful?

Solution

Your insert function should look something like this

void Insert(struct StudentRecords Record)
{
  struct Node* newNode = GetNewNode(Record);
  struct Node *tmp = head;
  /* Check if we need to insert at head */
  if (head==NULL)
  {
    head = newNode;
    return;
  }

  /* Check if new node is smaller than head */
  if (head->data.StudentID > Record.StudentID) {
    head->prev = newNode;
    newNode->next = head;
    head = newNode;
    return;
  }

  /* Find the node previous to node having StudentID > "StudentID in new node" */
  while(tmp->next && tmp->next->data.StudentID < Record.StudentID)
    tmp = tmp->next;

  /* Insert the new node */
  newNode->next = tmp->next;
  if (tmp->next)
    tmp->next->prev = newNode;
  tmp->next = newNode;
  newNode->prev = tmp;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top