C언어 구조체 문제2
문제6
工资单处理
(1) 编写函数:有两个单向链表,头指针分别为list1、list2,链表中每一结点包含员工号(员工号为关键字段,不重复)、姓名、工资基本信息,请编一函数,把两个链表拼组成一个链表,并返回拼组后的新链表,按员工号升序排列。
(2)编写函数:利用指针数组,实现按照工资进行升序排列的功能。返回排序完成的指针数组
(3)编写一程序,分别输出按员工号排序后的新链表。
假设链表list1初始化内容为:
{002, name002,3000}, {005, name005,2500}, {003, name003,3500}
链表list2初始化内容为:
{006, name006,2800}, {004, name004,3700}, {001, name001,3000}, {007, name007, 3600},
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _employee {
struct employee* next;
int num;
char name[20];
int salary;
} employee;
void addNode(employee* target, int num, char name[], int salary) {
employee* curr = malloc(sizeof(employee));
curr->next = target->next;
target->next = curr;
strcpy_s(curr->name, 20, name);
curr->num = num;
curr->salary = salary;
}
void sortList(employee* target, int num,char name[],int salary) {
employee* curr = malloc(sizeof(employee));
employee* p = malloc(sizeof(employee));
curr = target->next;
p = target;
if (curr == NULL) {
addNode(target, num, name, salary);
}
else {
while (1) {
if (curr == NULL) {
addNode(p, num, name, salary);
break;
}
if ((curr->num) >num) {
addNode(p, num, name, salary);
break;
}
else {
p = curr;
curr = curr->next;
}
}
}
}
employee *combination(employee* list1,employee*list2) {
employee* list = malloc(sizeof(employee));
employee* curr1 = malloc(sizeof(employee));
employee* curr2 = malloc(sizeof(employee));
curr1 = list1->next;
curr2 = list2->next;
list->next = NULL;
while (curr1 != NULL) {
sortList(list,curr1->num,curr1->name,curr1->salary);
curr1 = curr1->next;
}
employee* curr3 = malloc(sizeof(employee));
curr3 = list->next;
while (curr2 != NULL) {
sortList(list, curr2->num, curr2->name, curr2->salary);
curr2 = curr2->next;
}
return list;
}
int main(void) {
employee* list1 = malloc(sizeof(employee));
employee* list2 = malloc(sizeof(employee));
employee* list = malloc(sizeof(employee));
list1->next = NULL;
list2->next = NULL;
list->next = NULL;
addNode(list1, 2, "name002", 3000);
addNode(list1, 5, "name005", 2500);
addNode(list1, 3, "name003", 3500);
addNode(list2, 6, "name006", 2800);
addNode(list2, 4, "name004", 3700);
addNode(list2, 1, "name001", 3000);
addNode(list2, 7, "name007", 3600);
list =combination(list1, list2);
employee* curr = malloc(sizeof(employee));
curr = list->next;
while (curr != NULL) {
printf("%d %s %d\n", curr->num, curr->name, curr->salary);
curr = curr->next;
}
return 0;
}
실행결과
문제7
链表解环
按照要求,编写完成函数程序。
一段可运行的程序如下:其中,函数create()生成一个长度随机的环链表如下图所示(链表中每一结点数据域也是随机值),该链表最后一个节点的next指针随机的指向了链表内的某一节点。
1. 解环函数openloop()(10分)
形参是环链表头指针head,openloop函数将链表解环,使之恢复成为一个正常的单链表如下图所示。
2. 输出链表函数print()(10分)
输出解环后的单向链表到屏幕。
#include<stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
struct node
{
int num;
struct node* next;
};
struct node* create();
void openloop(struct node*);
void print(struct node*);
void main()
{
struct node* head;
srand(time(NULL));
head = create();
openloop(head);
print(head);
}
struct node* create()
{
int n = 0;
struct node* p1, * p2, * head;
int i;
int randomIndex;
head = NULL;
p1 = NULL;
p2 = NULL;
while (n == 0) //环链的长度随机
n = ((int)rand()) % 20;
for (i = 0; i < n; i++)
{
p1 = (struct node*) malloc(sizeof(struct node));
p1->next = NULL;
p1->num = ((int)rand()); //每节点内容随机
if (i == 0)
head = p1;
else
p2->next = p1;
p2 = p1;
}
if (head != NULL) //当链表不为空时,随机成环
{
randomIndex = ((int)rand()) % n; //随机成环的节点
p2 = head;
for (i = 0; i < randomIndex; i++)
p2 = p2->next;
p1->next = p2;
}
return head;
}
답:
void openloop(struct node*head) {
struct node* p[20] = { 0 };
struct node* curr = malloc(sizeof(struct node));
curr = head->next;
int flag = 1;
int n = 0;
while (flag) {
if (n == 0) {
p[0] = malloc(sizeof(struct node));
p[0]->next = curr;
n++;
curr = curr->next;
}
else {
for (int i = 0; i < n; i++) {
if ((p[i]->next) == (curr->next)) {
curr->next = NULL;
flag = 0;
break;
}
else if (i == n - 1) {
p[n] = malloc(sizeof(struct node));
p[n]->next = curr;
n++;
curr = curr->next;
break;
}
}
}
}
}
void print(struct node* head) {
struct node* curr = malloc(sizeof(struct node));
curr = head->next;
while (curr != NULL) {
printf("%d ->", curr->num);
curr = curr->next;
}
printf("NULL");
}
실행화면
Author And Source
이 문제에 관하여(C언어 구조체 문제2), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@sungmin-choi/C언어-구조체-문제2저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)