Hi everyone!
I'm new here, so please excuse any mis-posts or errors that I may have.
I'm trying to create a Linked List from a text input file, sort the Linked List, then print out the sorted linked list to an output file.
So far, I'm just trying to get the gist of linked list.
I haven't completed the sorting method yet.
I'm just having trouble even creating the linked list!
I get compiling errors:
Final.c:23: warning: assignment makes pointer from integer without a cast
C:\Users\User\Final.c: At top level:
Final.c:39: error: conflicting types for 'AddToList'
Final.c:23: error: previous implicit declaration of 'AddToList' was here
I have no idea what to do
insert
Code:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
/********************************************************************/
/* CreateList
/********************************************************************/
/*
* Input: A pointer to the node that we want to create, a pointer
to the input file and a value for the input file
* Output: The pointer to the node is updated.
* Task: Create a node linked list
*/
struct node* CreateList(struct node *head, int m, FILE *fp1){
do{
fscanf(fp1, "%d", &m);
head = AddToList(head, m); // Add to List
}
while(!feof(fp1));
return head;
}
/********************************************************************/
/* AddToList
/********************************************************************/
/*
* Input: A pointer to the node that we want to create,
and a value for the input file
* Output: The pointer to the node is updated.
* Task: Add a node to the linked list
*/
struct node* AddToList(struct node *head, int data){
struct node *tmp;
if(head == NULL){
head=(struct node *)malloc(sizeof(struct node));
if(head == NULL){
printf("Error! memory is not available\n");
exit(0);
}
head-> data = data;
head-> next = head;
}
else{
tmp = head;
while (tmp-> next != head)
tmp = tmp-> next;
tmp-> next = (struct node *)malloc(sizeof(struct node));
if(tmp -> next == NULL)
{
printf("Error! memory is not available\n");
exit(0);
}
tmp = tmp-> next;
tmp-> data = data;
tmp-> next = head;
}
return head;
}
/********************************************************************/
/* WriteList
/********************************************************************/
/*
* Input: A pointer to the node that we want to write and
a pointer to the output file
* Output: No output
* Task: Print the linked list
*/
void WriteList(struct node *head, FILE *fp3)
{
struct node *current;
current = head;
if(current!= NULL)
{
do
{
printf("%d\t",current->data);
fprintf(fp3,"%d\n", current->data);
current = current->next;
} while (current!= head);
printf("\n");
}
else
printf("The list is empty\n");
}
/********************************************************************/
/* DeleteList
/********************************************************************/
/*
* Input: A pointer to the node that we want to delete.
* Output: No output.
* Task: Delete the linked list
*/
void DeleteList(struct node *head)
{
struct node *current, *tmp;
current = head->next;
head->next = NULL;
while(current != NULL) {
tmp = current->next;
free(current);
current = tmp;
}
}
/*
WORK IN PROGRESS
*/
struct node* RemoveFromList(struct node *head, int data){
return head;
}
/********************************************************************/
/* checkFile
/********************************************************************/
/*
* Input: A pointer to the file that we are reading and a value
to count the error messages.
* Output: The counter
* Task: Check input and output files.
*/
int checkFile (FILE *fp, int count){
if (count == 0){
count++;
if (fp == NULL){
printf("\nError opening input file Input.txt ...Program terminating\n");
exit(1);
}
return count;
}
if (fp == NULL){
printf("\nError opening input file Sorted.txt ...Program terminating\n");
exit(1);
}
return count;
}
int main()
{
FILE *fp1, *fp3;
int m;
int count = 0;
struct node *head = NULL;
// Open file 1 and check for failure
fp1 = fopen("Input.txt", "r");
count = checkFile(fp1, count);
fp3 = fopen("Sorted.txt", "w");
count = checkFile(fp3, count);
// Creates List
CreateList(head, m, fp1);
// Writes to output
WriteList(head, fp3);
// Deletes List
DeleteList(head);
getchar(); // must press enter to continue
}