In: Computer Science
Hi,
I am working on an assignment in C-Programming language dealing with LInked lists,
in the code there is instructions on where to write the code. I do not know how to write Linked Lists.
Has to be in the C-language, Any help is greatly appreciated
//agelink.c
//maintains list of agents
//uses linked list
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
void listall(void);
void newname(void);
void delink(void);
void memexit(void);
void wfile(void);
/*********************************************************************
this is the structure to hold a agent information.
prt *ptrnext is a point to link to next prt structure
*********************************************************************/
struct prs
{
char name[40];
int agnumb;
float height;
struct prs *ptrnext;
};
struct prs *ptrfirst = NULL;
int n = 0;
/***********************************************************************
main() calls other methods depending on user choice. you do not
need the change code here.
**********************************************************************/
void main(void){
while(TRUE) // cycle until user chooses 'q'
{
printf("\n'e' enter new agent\n'l'
list all agents");
printf("\n 'd' delete a agent\n'r'
read file\n'q' exit: ");
char option;
scanf("%c", &option);
switch(option) //gethche()
gets()
{
case 'e':
newname(); break;
case 'l':
listall(); break;
case 'd':
delink(); break;
case 'q':
memexit(); break;
default:
printf("\nEnter only selections listed");
//puts("this is to be print out to screen")
}//end switch
}//end while
}// end main();
/**************************************************************
Complete this methods to ask user to enter agent name,
agent number, and agent height to add a new agent to the
linked list.
use scanf() to read from the user inputs
use atoi to convert char to integers.
**************************************************************/
void newname(void){
struct prs *ptrthis; // this is the pointer to new
agent.
char numstr[81];
ptrthis = malloc(sizeof(struct prs));
if(ptrthis==NULL)
{
printf("\nCan't reallocate
memory");
return;
}
//your code start here
if(!ptrfirst)
ptrfirst = ptrthis;
else
{
ptrfirst->ptrnext = ptrthis; //
note: you need make the line right to handle additonal link.
}
}
/**************************************************************
Complete this method to print out all the agent list to
screen
**************************************************************/
void listall(void){
struct prs *ptrthis;
if(ptrfirst == NULL){
printf("\nEmpty list");
perror("error in listall
method");
return ;
}
ptrthis = ptrfirst;
// in this do while loop, to print out the list
do{
}
while (ptrthis != NULL);
}
/******************************************************************
//delink()
//delete entry in agent list matching the name from user input
******************************************************************/
void delink(void){
struct prs *ptrthis, *ptrlast; // utility
pointer
char delname[81];
if(ptrfirst == NULL){
printf("\nEmpty list in
delink.\n");
return;
}
printf("\nEnter name ot be deleted: ");
scanf("%s",&delname);
ptrthis = ptrfirst;
//implement logica here to search and delete the
record matching the name
// user input from terminal. do not forget to free the
memory resouces
//used by the deleted records using free(void *ptr)
method.
do{
}while(ptrthis !=NULL);
printf("No such name on list\n");
}
//fee all memeory and exist;
void memexit(void){
struct prs *ptrthis, *ptrfree;
if(ptrfirst == NULL)
exit(0);
ptrthis = ptrfirst;
do{
ptrfree = ptrthis;
ptrthis =
ptrthis->ptrnext;
free(ptrfree);
}while (ptrthis != NULL);
exit(0);
}
Code: agelink.c
//agelink.c
//maintains list of agents
//uses linked list
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //strcmp
#define TRUE 1
void listall(void);
void newname(void);
void delink(void);
void memexit(void);
void wfile(void);
/*********************************************************************
this is the structure to hold a agent information.
prt *ptrnext is a point to link to next prt structure
*********************************************************************/
struct prs
{
char name[40];
int agnumb;
float height;
struct prs *ptrnext;
};
struct prs *ptrfirst = NULL;
int n = 0;
/***********************************************************************
main() calls other methods depending on user choice. you do not need the change code here.
**********************************************************************/
void main(void){
while(TRUE) // cycle until user chooses 'q'
{
printf("\n'e' enter new agent\n'l' list all agents");
printf("\n 'd' delete a agent\n'r' read file\n'q' exit: ");
char option;
scanf("%c", &option);
getchar();
switch(option) //gethche() gets()
{
case 'e': newname(); break;
case 'l': listall(); break;
case 'd': delink(); break;
case 'q': memexit(); break;
default:
printf("\nEnter only selections listed"); //puts("this is to be print out to screen")
}//end switch
}//end while
}// end main();
/**************************************************************
Complete this methods to ask user to enter agent name,
agent number, and agent height to add a new agent to the
linked list.
use scanf() to read from the user inputs
use atoi to convert char to integers.
**************************************************************/
void newname(void){
struct prs *ptrthis; // this is the pointer to new agent.
char numstr[81];
ptrthis = malloc(sizeof(struct prs));
if(ptrthis==NULL)
{
printf("\nCan't reallocate memory");
return;
}
//your code start here
printf("Enter Agent's name:");
scanf("%s", &(ptrthis->name));
getchar();
printf("Enter Agent' age: ");
scanf("%d", &(ptrthis->agnumb));
getchar();
printf("Enter Agent's height: ");
scanf("%f", &(ptrthis->height));
getchar();
ptrthis->ptrnext = NULL;
if(!ptrfirst)
ptrfirst = ptrthis;
else
{
ptrthis->ptrnext = ptrfirst; // note: you need make the line right to handle additonal link.
ptrfirst = ptrthis;
}
}
/**************************************************************
Complete this method to print out all the agent list to screen
**************************************************************/
void listall(void){
struct prs *ptrthis;
if(ptrfirst == NULL){
printf("\nEmpty list");
perror("error in listall method");
return ;
}
ptrthis = ptrfirst;
// in this do while loop, to print out the list
do{
printf("\n");
printf("Name: %s\n", ptrthis->name);
printf("Age: %d\n", ptrthis->agnumb);
printf("Height: %f\n", ptrthis->height);
ptrthis = ptrthis->ptrnext;
}
while (ptrthis != NULL);
}
/******************************************************************
//delink()
//delete entry in agent list matching the name from user input
******************************************************************/
void delink(void){
struct prs *ptrthis, *ptrlast; // utility pointer
char delname[81];
int flag = TRUE; //flag to indicate successful search
if(ptrfirst == NULL){
printf("\nEmpty list in delink.\n");
return;
}
printf("\nEnter name ot be deleted: ");
scanf("%s",&delname);
getchar();
ptrthis = ptrfirst;
//implement logica here to search and delete the record matching the name
// user input from terminal. do not forget to free the memory resouces
//used by the deleted records using free(void *ptr) method.
ptrlast = ptrfirst;
do{
//compare the name on the list
if(strcmp(ptrthis->name, delname)==0){
//handle if first node is being deleted
if(ptrthis == ptrfirst){
ptrfirst = ptrfirst->ptrnext;
}
ptrlast->ptrnext = ptrthis->ptrnext;
free(ptrthis);
ptrthis = NULL;
flag = TRUE-1;
//break the while loop here
break;
}
ptrlast = ptrthis;
ptrthis = ptrthis->ptrnext;
}while(ptrthis !=NULL);
if(flag==TRUE)
printf("No such name on list\n");
else
printf("Agent with name '%s' has been deleted\n", delname);
}
//free all memeory and exist;
void memexit(void){
struct prs *ptrthis, *ptrfree;
if(ptrfirst == NULL)
exit(0);
ptrthis = ptrfirst;
do{
ptrfree = ptrthis;
ptrthis = ptrthis->ptrnext;
free(ptrfree);
}while (ptrthis != NULL);
ptrfirst = NULL; //make list empty
exit(0);
}
Output:
Note: Please try to avoid using do-while loop. Instead, use while or for loop. Using do-while loop is not the best practice as it does the task before validating the loop condition.