Question

In: Computer Science

Allow the InsertAt method to add to the front position 0 (zero) also if you try...

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.

Please do through this order. Thanks

--> I need you to add more than 2 front position.

// 1 LOAD THE LIST FROM DATA.BIN //buildlist

// 2 DISPLAY THE LIST //prntlist

// 3 ADD TO THE LIST // listinsert

// 4 DELETE FROM THE LIST // listdelete

// 5 EXIT

//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

#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

Solutions

Expert Solution

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

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

int 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);

//inserted the nodes three times at the beginning of list

printf("Enter name to insert:");

scanf(" %[^\n]",part.name);

printf("part.name = %s\n",part.name);

liststart=InsertAt(liststart,part,0);

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

printf("Enter name to insert:");

scanf(" %[^\n]",part.name);

printf("part.name = %s\n",part.name);

liststart=InsertAt(liststart,part,0);

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(n==0)

{

next = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/

next->info=newentry;

next->link =liststart;

liststart=next;

printf("\n Created node at %d",i);

}

else 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

I solved the problem adding node at 0 more than 2 times

Output


Related Solutions

I try to add the table content work, they will not allow me.. see below in...
I try to add the table content work, they will not allow me.. see below in another post pls One question pls i need help with this, kindly show your work as well so that i can learn. I WILL RATE IT AND LEAVE A COMMENT. THANKS Tidal Wave is considering purchasing a water park in San Diego comma California​, for $ 1 950 000. The new facility will generate annual net cash inflows of $ 500 000 for eight...
Imagine that you are on the front line (also known as triage) in a hospital and...
Imagine that you are on the front line (also known as triage) in a hospital and are receiving patients presenting with possible Coronavirus symptoms. What would you be looking for in terms of symptoms for these patients? How would you differentiate them from people who may be coming in for different reasons? Your role is to investigate what symptoms these patients arrive with that makes you suspicious for this disease. Also provide a reason why this virus has a special...
If you were offered a position with a company which would allow you to take the...
If you were offered a position with a company which would allow you to take the position as either a W2 employee or an independent contractor as a 'consultant' explain which one you would choose under these circumstances and why: - The job pays $100,000 per year paid monthly. One month vacation for both 1099 & W2. Job stability is equal regardless of which option you choose. No other benefits beyond what is required by Federal Law. You are in...
As a professional financial consultant, you try to advise on the position of a diversified portfolio....
As a professional financial consultant, you try to advise on the position of a diversified portfolio. Your task is to explain to the management team on the bond sensitivity, which requires you to: (a) Calculate the duration of a five-year, $3,000 Treasury Bond with a 10 per cent semi-annual coupon selling: (i) at par? (ii) selling with a yield to maturity of 20 per cent? (b) Conclude on the relationship between the duration and yield to maturity based on part...
a) In front of spherical concave mirror of radius 26cm, you position an object of height...
a) In front of spherical concave mirror of radius 26cm, you position an object of height 2.6cm somewhere along the principal axis. The result image has a height of 0.8cm. How far from the mirror is the object located? b) In front of a spherical convex mirror of radius 26cm, you position an object of height 2.6cm somewhere already no the principal axis. The resultant image has a height of 0.8cm. How far from the mirror is the object located?...
Complete the following Customer class below. Also, add the equals method that would compare each field....
Complete the following Customer class below. Also, add the equals method that would compare each field. Please pay attention to the format of the equals method as it needs to have the same format as shown below. public class Customer {       private int id; //customer id       private String name; //customer's name       private int discount; //discount rate in percent             //construct a new customer       public Customer(int id, String name, int discount) {                    }      ...
Generally Accepted Accounting Principles (GAAP) allow companies to choose any inventory costing method. GAAP also requires...
Generally Accepted Accounting Principles (GAAP) allow companies to choose any inventory costing method. GAAP also requires that companies consistently stick to one method. However, companies do switch back and forth from FIFO to LIFO to Weighted Average and vice versa. International Financial Reporting Standards (IFRS) value inventory in a similar method as GAAP. Companies can use FIFO or average costs, but LIFO is not an option. Access and review the following article from the CSU-Global Library: Krishnan, S. (2012). Inventory Valuation...
Elective abortion. Identify pros and cons that, when analyzed, will allow you form a defensible position...
Elective abortion. Identify pros and cons that, when analyzed, will allow you form a defensible position related to the issue. Define the scope of the ethical issue. Examine the scope of the issue as it relates to nursing and principles identified in codes of ethics. Identify at least two positions taken on this issue by scholarly experts in the ethics discipline. Explore the future of the issue as it relates to nursing practice.
Give me a problem you are interested in. Outline using Scientific method how you will try...
Give me a problem you are interested in. Outline using Scientific method how you will try to answer the problem step by step.
This format should allow you more time to think about your answer. You can also use...
This format should allow you more time to think about your answer. You can also use the readings to support your answer. Be sure to cite the author and page number for all quotes or ideas taken from one of the authors (e.g. Chang, p. 81). The length of your answers should range from 1-3 paragraphs, depending on the complexity of the question. Interest in Heterodox economics is increasing. What events have taken place to bring more attention to heterodox...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT