Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning.
/INSERT.C
/*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */
/*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */
#include
#include
#include
#include
typedef struct ENTRY
{
char name[81];
}ENTRY;
typedef struct LISTREC /* LISTREC is a user-defined type, defined */
{ /* before main so it will be global */
struct ENTRY info; /* info nests all fields contained in */
struct LISTREC *link; /* ENTRY (above). */
}LISTREC; /* link is a pointer variable to another */
/* type LISTREC. NOTE: Name of type MUST */
/* be entered at end of typedef */
FILE *stream;
ENTRY part;
//prototypes !!!!
void listdelete(ENTRY part,LISTREC *liststart);
LISTREC *buildlist();
void prntlist(LISTREC *);
void listinsert(ENTRY,LISTREC *);
LISTREC * InsertAt(LISTREC *, ENTRY ,int );
void main()
{
LISTREC *liststart; /*holds a pointer to beginning of list*/
stream=fopen("data.bin","r+b");
liststart = buildlist(); /*buildlist is a function which will*/
// clrscr(); /*return the beginning of the list*/
printf("Before insertion linked list:\n");
prntlist(liststart);
printf("Enter name to insert:");
scanf(" %[^\n]",part.name);
printf("part.name = %s\n",part.name);
listinsert(part,liststart);
prntlist(liststart);
printf("Before deletion linked list:\n");
prntlist(liststart);
printf("Enter name to delete:");
scanf(" %[^\n]",part.name);
printf("part.name = %s\n",part.name);
listdelete(part,liststart);
prntlist(liststart);
printf("Enter name to insert:");
scanf(" %[^\n]",part.name);
printf("part.name = %s\n",part.name);
liststart=InsertAt(liststart,part,3);
prntlist(liststart);
printf("Enter name to insert:");
scanf(" %[^\n]",part.name);
printf("part.name = %s\n",part.name);
liststart=InsertAt(liststart,part,6);
prntlist(liststart);
printf("Enter name to insert:");
scanf(" %[^\n]",part.name);
printf("part.name = %s\n",part.name);
liststart=InsertAt(liststart,part,7);
prntlist(liststart);
/* printf("Enter name to insert:");
scanf(" %[^\n]",part.name);
printf("part.name = %s\n",part.name);
liststart=InsertAt(liststart,part,0);
prntlist(liststart);
*/
// does doesn't work but throws no error !!!!!!
printf("Enter name to insert:");
scanf(" %[^\n]",part.name);
printf("part.name = %s\n",part.name);
liststart=InsertAt(liststart,part,22);
prntlist(liststart);
}//main
/*****************FUNCTION LISTINSERT**********************/
void listinsert(ENTRY newentry,LISTREC *liststart)
{
LISTREC *last,*next;
next = liststart;
while ((strcmp(newentry.name,next->info.name)> 0) && (next->link != NULL))
{
last = next;
next = next->link;
} /*end while*/
if (strcmp(newentry.name,next->info.name) == 0) /*if both are same*/
next->info = newentry; /*updates*/
else
if (strcmp(newentry.name,next->info.name) < 0)
{
last->link = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/
last->link->info=newentry;
last->link->link = next;
}
else
{
next->link = (LISTREC*)malloc(sizeof(LISTREC));
next->link->info = newentry;
next->link->link = NULL;
} /* end else */
printf("\n\nAfter insertion, linked list:\n");
prntlist(liststart);
} /* end function listinsert*/
/**************FUNCTION BUILDLIST************************/
LISTREC *buildlist() /* Function buildlist returns type LISTREC.*/
{ /* It takes no arguments */
LISTREC *liststart = NULL;
/*of type LISTREC */
int n; /*Needed to test for end of file*/
//CREATE A DUMMY NODE !!!!
liststart=(LISTREC *)malloc(sizeof(LISTREC));
liststart->info.name[0]='\0';
liststart->link=NULL;
for (;;)
{ /*begin for*/ /*for will keep looping until breaks at end of file*/
n= fread(&part,sizeof(part),1,stream);
if (n==0) break;
listinsert(part,liststart);
} /*end for*/
return(liststart);
} /*end Function buildlist*/
/****************FUNCTION PRNTLIST********************/
void prntlist(LISTREC *liststart)
/*argument is a pointer to type LISTREC*/
{ /*begin function*/
while (liststart != NULL)
{ /*begin while*/
printf("%s\n",liststart->info.name);
liststart = liststart->link;
} /*end while*/
} /* end function prntlist*/
/*****************FUNCTION LISTDELETE**********************/
void listdelete(ENTRY part,LISTREC *liststart)
{
LISTREC *last,*next;
next = liststart;
while ((strcmp(part.name,next->info.name)!=0) && (next->link != NULL))
{
last = next;
next = next->link;
} /*end while*/
if (strcmp(part.name,next->info.name) == 0) /*if both are same*/
{
last->link=next->link; /*updates*/
free(next);
}
printf("\n\nAfter deletion, linked list:\n");
prntlist(liststart);
} /* end function listdelete*/
LISTREC * InsertAt(LISTREC *liststart, ENTRY newentry,int n)
{
int i = 0;
//LISTREC * liststart;
LISTREC * last = NULL;
LISTREC * next = liststart;
if (liststart == NULL)
{
liststart = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/
liststart->info=newentry;
liststart->link = NULL;
printf("\n Created node at %d",i);
}//if
else while ((next->link != NULL) && (i != n))
{
last = next;
next = next->link;
++i;
}//else while
if (next == NULL)
{
next = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/
next->info=newentry;
next->link = NULL;
printf("\n Created node at %d",i);
}//if
else if (i==n)
{
last->link = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/
last->link->info=newentry;
last->link->link = next;
printf("\n Created node at %d",i);
}//else
return liststart;
}// insertAT
So, in this question as we have to implement a function InsertAt which allows user to insert into the front also if you try to add to a position greater than the number of nodes you get an error warning.
So here is the solution for your problem.
LISTREC * InsertAt(LISTREC *liststart, ENTRY newentry,int n)
{
int i = 0;
//LISTREC * liststart;
LISTREC * last = NULL;
LISTREC * next = liststart;
if (liststart == NULL)
{
liststart = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/
liststart->info=newentry;
liststart->link = NULL;
printf("\n Created node at %d",i);
}//if
// here I implement the InsertAt 0 functionality
else if(n == 0){
LISTREC *node = (LISTREC*)malloc(sizeof(LISTREC));
node->info = newentry;
node->link = liststart;
liststart = node;
return liststart;
}
else while ((next->link != NULL) && (i != n))
{
last = next;
next = next->link;
++i;
}//else while
if (next == NULL)
{
next = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/
next->info=newentry;
next->link = NULL;
printf("\n Created node at %d but unable to add in the
list as required position is greater than the size of the
list",i);
//error message
}//if
else if (i==n)
{
last->link = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/
last->link->info=newentry;
last->link->link = next;
printf("\n Created node at %d",i);
}//else
return liststart;
}// insertAT
NOTE: Code written in the bold letters is the changed code to implement the required functionality.
We take the condition after checking if start node is not null then we assign the address of start node to the link of newly created node and then made this newly created node , start node.
and for the error message part we will check that we came out of the while loop because of next is null or i == n
if because of next is null that means the position at which we have to insert node is greater than the length of the list.
So, this is the solution for your problem, I hope I am able to solve your problem if yes then do give it a thumbs up. It really helps :)
Get Answers For Free
Most questions answered within 1 hours.