【发布时间】:2022-06-17 04:52:38
【问题描述】:
所以我需要以 2 种方式对这个双向链表进行排序,按姓氏排序 z-a 和 年龄越来越大。我在互联网上找不到任何对我的案子有帮助的东西。我很愚蠢,我自己尝试过,但没有结果。 我不能使用冒泡排序。
【问题讨论】:
标签: c
所以我需要以 2 种方式对这个双向链表进行排序,按姓氏排序 z-a 和 年龄越来越大。我在互联网上找不到任何对我的案子有帮助的东西。我很愚蠢,我自己尝试过,但没有结果。 我不能使用冒泡排序。
【问题讨论】:
标签: c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PersonalData
{
char *name;
char *surname;
int age;
struct PersonalData *next;
struct PersonalData *prev;
}Personal_Data;
void NewNode(Personal_Data **head)
{
char namev2[20], surnamev2[30];
char *name, *surname;
int agev2;
printf("Give name: ");
scanf("%s", namev2);
name = (char*)malloc(sizeof(char)*(strlen(namev2)));
strcpy(name, namev2);
printf("Give surname: ");
scanf("%s", surnamev2);
surname = (char*)malloc(sizeof(char)*(strlen(surnamev2)));
strcpy(surname, surnamev2);
printf("Give age: ");
scanf("%d", &agev2);
if(*head==NULL)
{
*head = (Personal_Data *)malloc(sizeof(Personal_Data));
(*head)->name=name;
(*head)->surname=surname;
(*head)->age = agev2;
(*head)->prev = NULL;
(*head)->next = NULL;
}
else
{
Personal_Data *nowy;
nowy=(Personal_Data *)malloc(sizeof(Personal_Data));
nowy->name=name;
nowy->surname=surname;
nowy->age = agev2;
nowy->prev = NULL;
nowy->next = (*head);
(*head)->prev=nowy;
*head=nowy;
}
}
void show_them_all(Personal_Data *head)
{
int c=1;
Personal_Data *i;
printf("[id] [name surname] [age]\n");
for (i = head; i != NULL; i = i->next)
{
printf("[%d] [%s %s] [%i]", c, i->name, i->surname, i->age );
c++;
printf("\n");
}
}
int main(void){
Personal_Data *head;
head = (Personal_Data *)malloc(sizeof(Personal_Data));
head = NULL;
int option;
printf("\n\n program to manage studentów\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("[0] exit program\n");
printf("[1] person data\n");
printf("[2] show all persons\n");
printf("[3] sorted z-a by surname\n");
printf("[4] sorted growingly by age\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
int value=0,value2=0;
while (1) {
printf("\nChose option between 0-4: ");
scanf("%i", &option);
switch (option) {
case 0:
printf("See you next time!");
return 0;
break;
case 1:
NewNode(&head);
break;
case 2:
show_them_all(head);
break;
case 3:
printf("Sorted by surname");
break;
case 4:
printf("Sorted by age");
break;
}
}
return 0;
}
【讨论】: