Here is a linked list. For some reason I had to make it a stack implimentation. I was having trouble creating a queue using the bianry file and random access. Notice that I changed the name of the datafile. Change it if your file has a different extension. I wonder about that binary file. Where did you get if from? I think that if you need a queue than Salem will have to help or else someone else. Anway this works as a Linked List Stack.
Code:
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
# include <conio.h>
typedef struct node_s
{
int Value;
struct node_s *link;
}List;
List * AddNode(FILE *,List *,int count);
void OpenFile(FILE **fptr, char path[], char mode[]);
void DisplayList(List*,int);
int main()
{
List *first = NULL;
FILE *fin = NULL;
//start at the first record
long rec_num = 0;
OpenFile(&fin,"c:\\data.txt","rb");
//seek to the end of the file
fseek(fin,sizeof(List),SEEK_END);
//size represents number of records in file
long size = ( ftell(fin) / sizeof(List) ) - 1;
for(rec_num = 0; rec_num < size; rec_num++)
{
first = AddNode(fin,first,rec_num);
}
DisplayList(first,rec_num);
return 0;
}
void OpenFile(FILE **fptr, char path[], char mode[])
{
*fptr = fopen(path,mode);
if (*fptr == NULL)
{
fprintf(stderr,"Error: File a:\\name?? is missing!\n");
exit(1);
}
}
List* AddNode (FILE *fin,List *first, int count)
{
List *next = NULL;
next = (List *) malloc (sizeof(List));
next->link = NULL;
fseek(fin,sizeof(List) * count,SEEK_SET);
fread(next, sizeof(List), 1, fin);
next->link = first;
first = next;
return first;
}
void DisplayList(List *start,int rec_num)
{
int i;
for(i=0; i < rec_num; i++)
{
printf("%d\n",start->Value);
start = start->link;
}
}