I am working on a linked list using separate files. I have a working linked list where all my functions are in one file but when I try to compile using separate files I have issues. I don't understand what changed with the pointers. Or am I not including things correctly, also prior to separate files I didn't use a header file or function prototypes, but I believe I have those correct.
prog4.c: In function âmainâ:
prog4.c:37: warning: passing argument 1 of âinsertâ from incompatible pointer type
prog4.c:37: warning: passing argument 2 of âinsertâ from incompatible pointer type
prog4.c:37: warning: passing argument 3 of âinsertâ makes integer from pointer without a cast
prog4.c:64: error: request for member ânextâ in something not a structure or union
prog4.c:75: warning: passing argument 1 of âremnodeâ from incompatible pointer type
Code:
header.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char string[21];
int num;
struct node *next;
};
void insert(struct node *, struct node *, int, char *);
void remnode(struct node *);
Code:
#include "header.h"
int main(int argc, char *argv[])
{
FILE *infile, *outfile; /*Creates an infile, and
outfile*/
struct node *head;
struct node *current; /*Creates a head and current
node*/
head = current = NULL; /*Set the head and current node
to NULL*/
infile = fopen("prog4.in","r"); /*opens infile for reading*/
outfile = fopen("prog4.out","w");/*opens outfile for writing*/
int num; /*variable to hold the integer
value read in by fscanf */
char buffer[21]; /*array to hold the characters
read in by fscanf */
while(fscanf(infile, "%d %s", &num, buffer) == 2){
/*while the fscanf function retur$
which occurs when it reads in and
integer and the string, do the
following */
insert(&head, ¤t, &num, buffer);
struct node *printnode = head;
/*create a temporary node for
printing to a file*/
while(printnode != NULL){
/*while printnode exists, or
while another node exists in the
list, do the following*/
fprintf(outfile, "%s %d\n",
(*printnode).string, (*printnode).num);
/*print to the outfile the
number and the string from the
current print node*/
fprintf(outfile, "\n");
printnode=printnode.next;
/*set printnode to be the next no$
}/*while(printnode != NULL){...*/
} /*while(fscanf(infile, "%d %s", &num, buffer) == 2){...*/
while(head != NULL){
remnode(&head);
}/*while(head != NULL){...*/
fclose(infile);
fclose(outfile);
return 0;
} /*end main*/
Code:
/*insert.c*/
void insert(struct node **front, struct node **current, int num,
char *buffer[]){
struct node *new = (struct node*)malloc(sizeof(struct node));
/*creates a node and allocates memory - 1030*/
if(new != NULL) /*checks to makes sure memory is allocated - 1*/
{
(*new).string = buffer;
(*new).num = num;
if(*front == NULL) /*if the front node does not
exist set it equal to new - 1*/
{
*front = new;
(*new).next = NULL;
} /*end if*/
if (strcmp(new.string, *current.string) == 0){
/*if new.string is equal to
current.string insert new after
current*/
(*new).next = NULL;
(**front).next = new;
}/*if (strcmp(new.string, *current.string) == 0){*/
if (strcmp(new.string, *front.string) < 0){
int i;
if (strcmp(new.string, *front.string) > 0){
/*if new.string comes after
front.string check the next node
by calling insert and passing
front.next into front*/
insert(&front.next, ¤t, &num, buffer);
}/*if (strcmp(new.string, *current.string) > 0){*/
*current = new; /*set the current node to new
- 2*/
} /*if(new != NULL){...*/
} /*end insert*/
Code:
/*remnode.c*/
void remnode(struct node **front)
/*function to remove a node*/
{
if(front != NULL) /*checks to make sure list is not empty*/
{
struct node *temp; /*create a temporary node*/
temp = NULL; /*set the temporary node to
null*/
temp = *front; /*set the temp to the front
node*/
*front = *front.next;
/*set the front node to the
next node*/
free((*temp).string); /*free the memory for the string
from the previous front node*/
free((*temp).num); /*free the memory for the num
from the previous front node*/
free(temp); /*free the memory for previous
front node*/
} /*end if*/
} /*end remnode*/