Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Here is a single linked list of a structure with name and gender information like the one below. Figure 1 (in mixed order, both gender and noun). Complete the given program to edit The linked list in Figure 1 is thus female names (in ascending alphabetical order) appears first, followed by male names (in descending alphabetical order) (as below) Figure 2). How do i do this?

What I have tried:

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Creating the struct
struct Student {
	char name[50];
	char gender;
	struct Student* next;
};

struct Student* insertNode(char* name,
	char gender, struct Student* list)
{
	struct Student *s;
	//Allocate memory for node (malloc operation)
	s = (struct Student* )malloc(sizeof(struct Student));
	if (s == NULL)  {
		printf("Memory allocation failed.");
		return list;
	}
	strcpy(s->name, name);
	s->gender = gender;
	s->next = list; //list is the start of the list
	list = s;

	return list;
}
//Sorting function
struct Student* sortList(struct Student* list)
{
	//Write your code here
}

//Printing function
void printList(struct Student * list)
{
	while (list != NULL) {
		printf("%s\t%c\n", list->name, list->gender);
		list = list->next;
	}
}

int main()
{
	//Creating an Initial Node Variable
	struct Student* head = NULL;
	//Call to functions
	head = insertNode("Cenk", 'M', head);
	head = insertNode("Ceyda", 'F', head);
	head = insertNode("Esra", 'F', head);
	head = insertNode("Okan", 'M', head);
	head = insertNode("Tugce", 'F', head);
	head = insertNode("Mehmet", 'M', head);
	head = insertNode("Ayse", 'F', head);
	head = insertNode("Merve", 'F', head);
	head = insertNode("Sedat", 'M', head);
	head = insertNode("Ahmet", 'M', head);
	//call to sorting function
	head = sortList(head);

	printList(head);

    return 0;
}
Posted
Updated 22-Dec-22 7:01am
v2
Comments
Richard Deeming 22-Dec-22 6:07am    
You code it by putting your code where the "write your code here" comment is.

Nobody here is going to do your homework for you. If you don't know how to start, then talk to your teacher.

Try with starting some Learn C tutorial or some video tutorial.

Than you may visit some linked list tutorial but be sure that you understand the architecture and principles. When not you may fail to answer to some questions of your teacher.

some tips: use functions and structs with understandable names and install some IDE like Visual Studio..
 
Share this answer
 
The prototype returns a pointer to a sorted list? Just like insertNode(), it would probably make more sense to implement the head as a reference. The return value could then be saved.
C
sortList(struct Student** head);

There are obviously two lists here, each to be sorted alphabetically. It would probably make sense to declare a separate head for each list. Then iterate through the original list and insert the current element into the appropriate list. Finally, concatenate the two lists and assign the new head, done.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900