好了,所以我得到這些錯誤在用gcc編譯:故障排除編譯時間循環鏈表錯誤
prelab6.h: In function âinsertHeadCircularâ:
prelab6.h:45: error: incompatible types in assignment
prelab6.h:46: error: incompatible types in assignment
prelab6.c: At top level:
prelab6.c:41: warning: data definition has no type or storage class
prelab6.c:41: warning: parameter names (without types) in function declaration
prelab6.c:41: error: conflicting types for âprintInOrderâ
prelab6.h:81: error: previous definition of âprintInOrderâ was here
prelab6.c:42: warning: data definition has no type or storage class
prelab6.c:42: warning: parameter names (without types) in function declaration
prelab6.c:42: error: conflicting types for âprintReverseâ
prelab6.h:112: error: previous definition of âprintReverseâ was here
我已經試了又試,但無濟於事修正這些錯誤。感謝任何和所有的幫助。
這是我的.c文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my.h"
int main (int argc, char **argv)
{
char firstname[100];
char lastname[100];
int monthsEmployed;
FILE *fptr;
fptr = fopen(argv[1], "r");
if (fptr == NULL)
printf ("Incorrect file reading!");
if (argc != 2)
printf ("Incorrect number of arguments!");
employeeInfo *insert;
insert = malloc(sizeof(employeeInfo));
employeeList *head;
head = NULL;
while(!feof(fptr))
{
fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed);
strcpy(insert->firstname, firstname);
strcpy(insert->lastname, lastname);
insert->monthsEmployed = monthsEmployed;
head = insertHeadCircular(head, insert);
}
}
printInOrder(head); // display the linked list
printReverse(head); // display the linked list in reverse
我的.h文件中(注意事物被註釋掉了,因爲我嘗試不同的事情沒有結果):
typedef struct employeeInfo{
char firstname[100];
char lastname[100];
int monthsEmployed;
}employeeInfo;
//Struct containing pointers to the next and previous used to make a circular linked list
typedef struct list{
employeeInfo emp;
struct list *next;
struct list *previous;
}employeeList;
employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp);
void printInOrder(employeeList head);
void printReverse(employeeList head);
employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp)
{
employeeList *theprevious = head;
employeeList *current;
employeeList *thenext = head;
current = malloc(sizeof(employeeList));
employeeInfo *employee;
if(thenext==NULL)
{
current->next = current;
current->previous = current;
}
else
{
current->next = thenext;
thenext->previous = current;
while(theprevious->next != thenext)
{
theprevious = theprevious->next;
}
current->previous = theprevious;
theprevious->next = current;
}
current->emp = (employeeInfo *)malloc(sizeof(employeeInfo));
employee = current->emp;
employee = malloc(sizeof(employeeInfo));
strcpy(employee->firstname, emp->firstname);
strcpy(employee->lastname, emp->lastname);
employee->monthsEmployed = emp->monthsEmployed;
/*
employeeList *newcell, *first = head;
if(head == NULL)
{
newcell = (struct list *)malloc(sizeof(struct list));
strcpy(newcell->firstname, emp->firstname);
strcpy(newcell->lastname, emp->lastname);
newcell->monthsEmployed = emp->monthsEmployed;
return newcell;
}
while(head->next != first)
{
head = head->next;
}
newcell = (struct list *)malloc(sizeof(struct list));
head->next = newcell;
strcpy(newcell->firstname, emp->firstname);
strcpy(newcell->lastname, emp->lastname);
newcell->monthsEmployed = emp->monthsEmployed;
newcell->next = first;
*/
return current;
}
void printInOrder(employeeList head)
{
/*employeeInfo *first = head;
if (head == NULL)
{
printf("The circularly linked list is empty!\n");
return;
}
do
{
printf("%s %s %d\n", emp.firstname, emp.lastname, head.monthsEmployed);
head = head->next;
} while(head != first);
*/
/*employeeInfo current = head;
employeeInfo start = head;
int loop = 0;
printf("--------------\n");
while(current != start || loop==0)
{
loop++;
printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed);
printf("--------------\n");
current=current->next;
}*/
}
void printReverse(employeeList head)
{/*
employeeList current = head
employeeInfo start = head
int theloop=0;
printf("--------------\n");
while(current! = start || loop==0)
{
loop++;
printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed);
printf("--------------\n");
current=current->previous;
}*/
}
編輯好的程序
錯誤:
file.c: In function âmainâ:
file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ
的.C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"
int main (int argc, char **argv)
{
char firstname[100];
char lastname[100];
int monthsEmployed;
FILE *fptr;
fptr = fopen(argv[1], "r");
if (fptr == NULL)
printf ("Incorrect file reading!");
if (argc != 2)
printf ("Incorrect number of arguments!");
employeeInfo *insert;
insert = malloc(sizeof(employeeInfo));
employeeList *head;
head = NULL;
while(!feof(fptr))
{
fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed);
strcpy(insert->firstname, firstname);
strcpy(insert->lastname, lastname);
insert->monthsEmployed = monthsEmployed;
head = insertHeadCircular(head, insert);
}
printInOrder(head); // display the linked list
printReverse(head); // display the linked list in reverse
}
的.H:
typedef struct employeeInfo{
char firstname[100];
char lastname[100];
int monthsEmployed;
}employeeInfo;
typedef struct list{
employeeInfo emp;
struct list *next;
struct list *previous;
}employeeList;
typedef employeeList *listnode;
employeeList *insertHeadCircular(employeeList *head, employeeInfo emp);
void printInOrder(employeeList *head);
void printReverse(employeeList *head);
employeeList *insertHeadCircular(employeeList *head, employeeInfo emp)
{
listnode newPtr;
listnode firstPtr;
listnode tempPtr;
newPtr = (employeeList *)malloc(sizeof(employeeList));
strcpy(newPtr->emp.firstname, emp.firstname);
strcpy(newPtr->emp.lastname, emp.lastname);
newPtr->emp.monthsEmployed = emp.monthsEmployed;
if(head == NULL)
{
newPtr->next = newPtr;
newPtr->previous = newPtr;
head = newPtr;
firstPtr = newPtr;
}
else
{
tempPtr = firstPtr;
newPtr->next = tempPtr;
tempPtr->previous = newPtr;
newPtr->previous = head;
head->next = newPtr;
firstPtr = newPtr;
}
return head;
}
void printInOrder(employeeList *head)
{
listnode currentPtr = head;
do
{
printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed);
currentPtr= currentPtr->previous;
}
while(currentPtr !=head);
}
void printReverse(employeeList *head)
{
listnode currentPtr = head->next;
do
{
printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed);
currentPtr = currentPtr->next;
}
while(currentPtr != head->next);
}
你的代碼在你的.h文件中? –
他可能正在解決一個實驗任務.. :)並在h文件中添加了代碼:) – duedl0r
@Flunkie:homework tag?代碼中發生錯誤的行在哪裏? – duedl0r