#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语句,我知道seg错误发生在while循环的“insertensertedorder”函数中的某个地方。如果您能帮助我们实现此功能,我们将不胜感激,谢谢
好的,我已经将insert函数改为:
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错误,但现在我想我只是没有好的算法
最佳答案
在分析了您的函数insertInSortedOrder()
并阅读了@JonathanLeffler的警告之后,我已经定位了排序列表中丢失项目的问题。
“new大于current”时出错
无条件覆盖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;
关于c - 将节点插入排序的链表中的函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44960624/