what is the code to impliment a liked list?????
i think i got it but i dont know how to check to see if the list is actually being created.
what is the code to impliment a liked list?????
i think i got it but i dont know how to check to see if the list is actually being created.
Post your code, or make a function to print your list's contents:
Quzah.Code:void printList( Node *n ) { while( n ) { printf("Node->next = %d\n", n->next ); //print address printf("Node->data = %d\n", n->data ); //print value n = n->next; } }
Hope is the first step on the road to disappointment.
here is my code
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book
{
int code;
char title[20];
char author[20];
int pages;
struct book*next;
}*head,*work;
FILE*library;
main()
{
int code;
int sRec;
int pick;
char title;
char author;
int pages;
char ans;
library=fopen("library.dat","wb");
fread(library,sizeof(struct book),1,library);
fclose(library);
printf ("\t\t\tTusket Municipal Library\n\n\n");
printf ("\tPlease make a selection...\n\n");
printf ("\t1.) Enter a book in the inventory\n");
printf ("\t2.) Delete a book from the inventory\n");
printf ("\t3.) Search the inventory for a book\n");
printf ("\t4.) Display the book inventory\n");
printf ("\t5.) Exit the program\n\n");
printf ("\tEnter your selection: \n");
scanf ("%d", &pick);
while (pick==1)
{
system("cls");
head=(struct book*)malloc(sizeof(struct book));
work=head;
printf("Enter code:");
scanf("%d",&code);
printf("Enter Title:");
scanf("%s", &title);
printf("Enter Author:");
scanf("%s", &author);
printf("Enter Number of Pages:");
scanf("%d",&pages);
printf("Do you wnat to add another book?:");
scanf("%s",&ans);
while (ans!='N')
{
work->next=NULL;
}
work->next=(struct book*)malloc(sizeof(struct book));
work=work->next;
library=fopen("library.dat","a+b");
if((library=fopen("library.dat","a+b"))!=NULL)
{
fwrite (&sRec,sizeof(struct book),(size_t)3,library);
fclose (library);
}
library=fopen("library.dat","a+b");
/* Test the write by reading it back from the file */
library=fopen("library.dat","w+b");
fread(&sRec,sizeof(struct book),(size_t)1,library);
fclose(library);
}
while (pick==2)
{
}
while (pick==3)
{
}
while(pick==4)
{
}
while(pick==5)
{
}
return 0;
}
Why do you keep opening and closing your file? Why don't you just open it the first time as "ab+" and leave it open? I don't see the point in reopening the file every time.
You can always use 'rewind' to go to the beginning of the file again, and also can use fseek to move about the file.
Um... no. You are using 'title' and 'author' here incorrectly. You keep reading into them, but they're only a single charcter, see:main()
{
int code;
int sRec;
int pick;
char title;
char author;
int pages;
char ans;
This is a "BadThing(TM)".printf("Enter code:");
scanf("%d",&code);
printf("Enter Title:");
scanf("%s", &title);
printf("Enter Author:");
scanf("%s", &author);
Quzah.
Hope is the first step on the road to disappointment.
You could tryOriginally posted by mattyans
what is the code to impliment a liked list?????
i think i got it but i dont know how to check to see if the list is actually being created.
this link. It has help on many other algos as well.