Question

Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...

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

Homework Answers

Answer #1

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 :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
C++ PROGRAMMING Submit the code for each problem separately. Important: Please place all appropriate coding comments...
C++ PROGRAMMING Submit the code for each problem separately. Important: Please place all appropriate coding comments inside of the code. Problem 1    Create a simple linked list program to create a class list containing class node {             void *info;              node *next; public:              node (void *v) {info = v; next = 0; }              void put_next (node *n) {next = n;}              node *get_next ( ) {return next;}              void *get_info (...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
Take the orderhw.c file and modify the code to build a linked list that is sorted...
Take the orderhw.c file and modify the code to build a linked list that is sorted by the address field not the name field as it is currently written! Using the following code 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*/...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT