I am not expecting a fast answer, but I have to create a car database for my data structures class using a linked list in C, and I need to sort it by year as the data is being inputted then print it out on the screen. I have been working on this for a couple weeks and can't seem to make any head way. I understand the basic concepts of a linked list, how to traverse the list, and add to the end, etc, but when I attempt to sort it I completely mess it up. I know right now the program just stops after inputting data from the second entry, and before that I had it stuck in an infinite loop printing out the same two entries. I just don't know what I am doing wrong, and need some direction. I am not asking for you to finish my work for me, but perhaps point out what I am doing wrong, or what I should look into. I know that isn't specific, but I am not sure where to turn to. I am just frustrated and at the point of desperation. Once I finally get the basic code working I am going to clean it up, and hopefully separate it into separate functions, but this is the code that I have now. I am still working on it, and haven't stopped just because I am posting here. Thank you very much ahead of time for any help, I really do appreciate it quite a bit right now...
// Project 1 Phase II.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef struct node
{
int data; // will store information
char make[150];
char model[150];
char engine[150];
int year;
int cost;
node *next; // the reference to the next node
};
int _tmain(int argc, _TCHAR* argv[])
{
int count = 0;
int cars;
char make[150];
char model[150];
char engine[150];
int year;
int cost;
node *head = NULL;
node *temp;
node *test;
node *prev;
//Ask user for how many cars to be inputted into database
printf("How many Cars do you wish to enter?\n");
scanf("%d", &cars);
printf("\n");
while (count < cars) {
temp = (node*)malloc(sizeof(node)); //allocate space for node
temp->data = count;// store data(first field)
//Ask user for Data using carCount to input in the correct part of the structure
printf("Enter Car\n");
printf("Make: ");
scanf("%s", make);
printf("Model: ");
scanf("%s",model);
printf("Cost: ");
scanf("%d", &cost);
printf("Year: ");
scanf("%d", &year);
printf("Engine ( 4 cyl, 6 cyl, etc...): ");
scanf("%s", &engine);
printf("\n");
strcpy(temp->model, model);
strcpy(temp->engine, engine);
strcpy(temp->make, make);
temp->cost=cost;
temp->year=year;
temp->next=NULL; // store the address of the pointer head(second field)
if (head == NULL){
head=temp;
head->next=NULL;
test=head;
prev=head;
} else {
if (test->year > temp->year){
test->next = NULL;
temp->next = test;
prev->next = temp;
head = prev;
}
while (test->next != NULL){
test=test->next;
prev=test;
}
test->next=temp;
}
count = count + 1;
}
node *temp1;
temp1 = head;
while( temp1!=NULL )
{
cout<< temp1->data<<" ";// show the data in the linked list
cout<< temp1->make<<" ";
cout<< temp1->model<<" ";
cout<< temp1->engine<<" ";
cout<< temp1->year<<" ";
cout<< temp1->cost<<"\n ";
temp1 = temp1->next; // tranfer the address of 'temp->next' to 'temp'
}
return 0;
}