文章

5

粉丝

50

获赞

2

访问

1.3k

头像
为什么程序不对劲,有大佬看看吗
P1015 贵州大学机试题
发布于2024年3月11日 19:20
阅读数 225

#include<stdio.h>
#include<string>
#include <stdlib.h>
#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;
int s[10];

struct node {
	int element;
	struct node *next;
};

struct node* create() {
	struct node* head, *now, *pre;
	for (int i = 1; i <= 5; i++) {
		now = (struct node*)malloc(sizeof(node));
		now->element = s[i];
		if (i == 1)
		{
			head = now;
			pre = now;
			continue;
		}
		pre = now;
	}
	return head;
}

void print(struct node *head) {
	struct node *p;
	p = head;
	while (p != NULL){
		cout << p->element << endl;
		p = p->next;
	}
}

int main() {
	for (int i = 1; i <= 5; i++)
		cin >> s[i];
	sort(s, s + 5);
	struct node* head;
	head = create();
	print(head);
	return 0;
}

	

 

登录查看完整内容


登录后发布评论

2 条评论
snake
2024年3月11日 19:36

排序是s+1,s+6

还少了两句

now->next=NULL;

pre->next=now;

赞(0)

周小黑02477 : 回复 snake: ac了,感谢老哥,帮我解答几次了,是官方吗

2024年3月11日 20:03