Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Anyone have standard C-code that does the following. Must use linked list: This

ID: 3767463 • Letter: A

Question

Anyone have standard C-code that does the following. Must use linked list:

This program is an exercise in using linked lists. The user will enter a number of strings and the program will display the list in alphabetical order. Running the program may display: Your program will accept all input using the fgets()) function to allow white space in the input. This function will be discussed in class. The program will accept names from the user and insert each name into a linked list. When a new name is entered into the list it is placed in a position which maintains the list in alphabetical order -- e.g. Mata Hari would be placed between John Lennon and Superman if those names were already in the list. When the user is done entering names the list will be traversed displaying the names in list (and hence alphabetical) order.

Explanation / Answer

#include <stdio.h>
typedef struct Node
{
char name[60];
struct Node *next;

}Node;
void push(struct Node** head_ref, char* new_data)
{
/* allocate node */
struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));

/* put in the data */
strcpy(new_node->name,new_data);

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}

int main() {
struct Node* head = NULL;
char c;
char name[60];
while(1)
{
printf("Enter a new name(y/n)? ");
scanf("%c",&c);
if(c=='y' || c=='Y')
{
printf(" name: ");
fgets(name,60,stdin);
push(&head, name);
}
else
{
while(head->next!=NULL)
{
printf(" %s",head->name);
head=head->next;
}
break;
}
}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote