#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MODEL 25
#define MAX_LINE 50
typedef struct car{
char* model;
char* color;
int year;
struct car* next;
}car;
car* readFromFile(FILE*);
car* insertInSortedOrder(car*, char*, char*, int);
car* createCar(char*, char*, int);
void printCars(car*);
int main(int argc, char** argv){
//creating a file pointer, opening file
FILE* fptr;
fptr = fopen(*(argv+1), "r");
car* head = NULL;
//this first reads cars from files, puts it into linked list and prints
head = readFromFile(fptr);
puts("do we get here");
printCars(head);
return 0;
}
///this function reads information from a text file, sends it to insertInSortedOrder function
car* readFromFile(FILE* fptr){
char *M, *C, *year;
char *readLine= malloc(sizeof(car)*MAX_MODEL);
int yr;
car *head=NULL;
while(fgets(readLine,MAX_LINE,fptr)!= NULL){
M = strtok(readLine,"|");
C = strtok(NULL,"|");
year = strtok(NULL,"|");
yr = atoi(year);
head = insertInSortedOrder(head,M,C,yr);
}
return head;
}
//this function sends new info to createCar to put it into a list, gets it and puts it
//into sorted order according to alphabetical order by model
car* insertInSortedOrder(car* head, char* model, char* color, int year){
//if list is emppty
if(head == NULL){
head = createCar(model,color,year);
}
else{
car *new = createCar(model,color,year);
car *current,*previous;
current = head;
previous = NULL;
puts("here?");
//compares new car model to next model, if next is greater than new, move onto next pair
while(strcmp(new->model,current->model) < 0){
puts("here2");
previous = current;
current = current->next;
puts("here2.5");
}
puts("here3");
//if only one thing in the list
if(previous->next == NULL){
//new car is next on list
if(strcmp(previous->model,new->model) > 0){
previous->next = new;
}
//new car goes into top of list
else{
head = new;
head->next = previous;
}
}
//doing stuff at middle or end
//if new is greater than current
else{
puts("here4");
previous->next = new;
new->next = current;
}
}
return head;
}
//this function creates a new "node" for the list, returns pointer
car* createCar(char* model, char* color, int year){
car *newCar = malloc(sizeof(car));
newCar->color = malloc(sizeof(char)*MAX_MODEL);
newCar->model = malloc(sizeof(char)*MAX_MODEL);
strcpy(newCar->model,model);
strcpy(newCar->color,color);
newCar->year = year;
newCar->next = NULL;
return newCar;
}
//this function prints out all of things in linked list
void printCars(car* head){
if(head == NULL)
printf("List is empty\n");
else{
printf("The list of cars in inventory are:\n");
while(head!=NULL){
printf("%-5d %-25s %-13s",head->year,head->model,head->color);
printf("\n");
head = head->next;
}
}
}
此程序的目的是读入一个文件,其中包含汽车列表及其型号名称,颜色名称和年份。一旦读入,它将按照汽车模型的类型按升序排列到链表中。这是要读入的文件:
Tesla S|royal|2015|
GMC Yukon|white|2005|
Dodge Charger|lime|2010|
Honda Civic|cobalt|1996|
Nissan Titan|saddlewood|2008|
Chevrolet Silverado|saddlewood|1999|
Ford Focus|lime|2000|
Toyota Tacoma|gunmetal|2009|
Tesla X|black|2016|
Nissan Maxima|white|2013|
Ford Explorer|forest|1996|
Chevrolet Corvette|cherry|1982|
Tesla 3|cherry|2017|
Nissan 370Z|royal|2011|
Ford F150|cherry|2006|
Honda Accord|white|2004|
Mitsubishi Eclipse|black|2002|
Toyota Tundra|cobalt|2017|
Dodge Challenger|lemon|1970|
Chevrolet Camaro|black|1966|
在命令提示符下输入“./a.out cars.txt”后,输出为
here?
here2
here2.5
Segmentation fault
由于我的puts语句,我知道在“insertInSortedOrder”函数中我的while循环中某处发生了seg错误。任何有关使此功能工作的帮助将非常感谢,谢谢
好吧我已将插入功能更改为:
car* insertInSortedOrder(car* head, char* model, char* color, int year){
//if list is emppty
if(head == NULL){
head = createCar(model,color,year);
}
else{
car *new = createCar(model,color,year);
car *current,*previous;
current = head;
previous = NULL;
//compares new car model to next model, if next is greater than new, move onto next pair
while( strcmp(new->model,current->model) < 0){
//if current is only node in list or end of list
if(current->next == NULL){
current->next = new;
return head;
}
else{
//move forward to compare new
previous = current;
current = current->next;
}
}
//when new is greater than current, put new on top of it
previous = new;
previous->next = current;
}
return head;
}
现在打印:
The list of cars in inventory are:
2015 Tesla S royal
2005 GMC Yukon white
2010 Dodge Charger lime
1999 Chevrolet Silverado saddlewood
1982 Chevrolet Corvette cherry
1966 Chevrolet Camaro black
所以耶!没有更多的seg故障,但现在我觉得我还没有好的算法
答案 0 :(得分:0)
在分析了你的函数insertInSortedOrder()
并阅读@JonathanLeffler的警告之后,我已经对你的排序列表中缺少项目的问题进行了本地化。
“ new>大于当前”错误
previous
指针使用代码previous = new;
而不检查previous是否指向节点将直接用新项覆盖列表的一部分。
head
previous == NULL
previous == NULL
时,new
项将替换head
。
使用以下代码:
if (previous==NULL) {
// new becomes the first node
new->next = head;
head = new;
}
else {
// new is inserted between previous and current
new->next = previous->next;
previous->next = new;
}
而不是代码:
previous = newitem;
previous->next = current;