Question

In: Computer Science

Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the...

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

}

Solutions

Expert Solution

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.


Related Solutions

Hi, I am working on this assignment where we have to read and write to a...
Hi, I am working on this assignment where we have to read and write to a file. Change any code that needs to be changed, I need the wfile and rfile functions to be edited to work and the rest of the program. please change the gets() to something that works. if I need to create a .txt file to run the program pls let me know Thanks in advance for the help //agalloc.c // maintains list of agents infile,...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 3? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
Hi, (C programming) I am looking to write a program that will encrypt a text file...
Hi, (C programming) I am looking to write a program that will encrypt a text file automatically once program has opened, and have the option to decrypt it in a menu in my program. I have multiple text files that I need to encrypt, and would like if the program could encrypt all of them at once. I would also only like to decrypt the a text file once the name has been entered into a scanf function.
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
I have a question about C++ programming Language class. I am confused with these terms and...
I have a question about C++ programming Language class. I am confused with these terms and what they are for. 1. Unix 2. Terminal 3. Git 4. CLOC 5. Linux Please explain each words and what's their jobs for C++. Also, if you know some good sources/websites, could you please provide me a link where I can learn how to use Unix, Terminal, Git, etc.
Hi i am doing an assignment on the financial crisis. I am up to the conclusion...
Hi i am doing an assignment on the financial crisis. I am up to the conclusion and i am having trouble writing it can you please write up a summary of the financial crisis. Thanks
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT