Hi
need help as below is my code for linking lists. It keeps coming up with errors. I dont understand them how to get rid of them. Been trying for hours. Can someone please help me see where I am going wrong cos really having problems.
x Jit x
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char name[20];
struct node*link;
};
void add();
void delete();
void display();
struct node*load();
void save();
void main(void)
{
struct node*start;
char inbuf[2];
char choice;
int check;
/*set up dummy first node*/
start=(struct node*)malloc(sizeof(struct node));
strcpy(start->name,"");
start->link=NULL;
do
{
printf("Linked List with Pointers\n");
printf("Add new item...........1\n");
printf("Delete item....2\n");
printf("Display list........3\n");
printf("Load from disk..........4\n");
printf("Save to disk...........5\n");
printf("Exit from program..........6\n");
gets(inbuf);
choice=inbuf[0];
switch(choice)
{
case ('1'):add(start); /*pass ptr to start of list*/
break;
case ('2'):delete(start);break;
case ('3'):display(start);break;
case ('4'):start=load();break;
case ('5'):save(start);break;
case ('6'):break;
default :printf("Unknown Option %c\n",choice);
}
}
while(choice!='6');
}
void add(struct node*start)
{
char newname[20];
struct node*next;
struct node*temp;
struct node*prev;
temp=(struct node*)malloc(sizeof(struct node));
/*allocate storage*/
if(temp==NULL)
{
printf("Out of memory space\n");
return;
}
printf("Item to add:");
gets(newname);
strcpy(temp->name,newname);
next=start;
while(next->link!=NULL&&strcmp(next->name,newname)<0)
{
prev=next; /*store current position*/
next=next->link; /*and find next*/
}
if(strcmp(next->name,newname)<0)
prev=next; /*if before end*/
temp->link=prev->link; /*adjust pointers*/
prev->link=temp;
}
void delete(struct node*next)
{
char newname[20];
struct node*prev=next;
printf("Item to delete:");
gets(newname);
while(strcmp(next->name,newname)<0&&next->link!=NULL)
{
prev=next;
next=next->link;
}
if(strcmp(next->name,newname)1=0)
printf("%s not found./n",newname);
else
{
printf("Deleting %s\n",next->name);
prev->link=next->link;
free(next); /*return storage to free memory*/
}
}
void display(struct node*next)
{
do
{
printf("%s\n",next->name);
next=next->link;
}
while(next!=NULL);
}
struct node*load() /*this returns ptr to first item*/
{
FILE*fin;
char name[20];
struct node*start;
struct node*next;
struct node*prev;
fin=fopen("listdata","r");
start=(struct node*)malloc(sizeof(struct node));
strcpy(start->name,""); /*dummy to start list*/
prev=start;
fscanf(fin,"%s\n",name);
do
{
next=(struct node*)malloc(sizeof(struct node));
strcpy(next->name,name);
prev->link-next;
prev=next;
}
while(fscan(fin,"%s\n",name)!=EOF);
fclose(fin);
next->link=NULL; /*mark end of list*/
return(start);
}
void save(struct node*next)
{
FILE*fout;
char*temp;
next=next->link; /*ignore first non-item*/
fout=fopen("listdata","w");
while(next!=NULL)
{
fprintf(fout,"%s\n",next->name);
next=next->link;
}
fclose(fout);
}