A buddy and I trying to make a program that can take in records and, by using linked list and a structure, process them in a variety of task.
I've put several checks in the code to see where it goes wrong.
Code:
#include <stdlib.h>#include <string.h>
#define size 21
int check = 0;
struct student{
struct student* back;
char first[size];
char last[size];
double score;
int zip;
struct student* next;
};
struct student *head;
struct student *tail;
struct student *current;
struct student *temp;
void insert(){
struct student *lead = (struct student*)malloc(sizeof(struct student));
lead->back = current;
lead->next = tail;
temp = tail;
printf("First name: ");
scanf("%s",&lead->first);
printf("Last name: ");
scanf("%s",&lead->last);
printf("Score: ");
scanf("%f",&lead->score);
printf("Zip: ");
scanf("%d",&lead->zip);
tail->back = temp;
printf("Check 3\n");
while(&temp->back != &head){
temp = temp->back;
}
head->next = temp;
printf("Check 4\n");
current->next = lead;
printf("Check 5\n");
current = lead;
printf("\nRecord inserted successfully!\n\n");
}
int main(){
printf("Check 1\n");
head = (struct student*)malloc(sizeof(struct student));
tail = (struct student*)malloc(sizeof(struct student));
tail->back = head;
head->next = tail;
current = head;
printf("Check 2\n");
char fTemp[size],lTemp[size];
int records,i,j,k;
printf("How many records would you like to enter? (min. 5)");
scanf("%d",&records);
for(i = 0;i<records;i++){
insert();
}
For some reason it will stick after Check 3 and go into a infinite while loop. Help please!