import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = scanner.nextInt();
Arrays.sort(a);
for (int i : a) System.out.print(i+" ");
}
}
} #include <stdio.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *insert(node *head,int t)
{
node *pre=head; //pre指示前序结点
node *n=pre->next;
while(n!=NULL)
{
if(n->data<t)
{
pre=n;
n=n->next;
}
else break;
}
node *ins=(node *)malloc(sizeof(node));
ins->data=t;
pre->next=ins;
ins->next=n;
return head;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
node *head=(node *)malloc(sizeof(node));
head->next=NULL;
for(int i=0;i<n;i++)
{
int t;
scanf("%d",&t);
head=insert(head,t);
}
while(head->next!=NULL)
{
printf("%d ",head->next->data);
head=head->next;
}
printf("\n");
}
} /** \没说不可以用辅助空间,那就先排序后赋值 * * \param * \param * \return * */ #include <iostream> #include <algorithm> using namespace std; struct LNode{ LNode* next=nullptr; int value; }; LNode* create_link(LNode* head,int a[],int n); void print_link(LNode* head); int main(){ int n; cin>>n; LNode* head=new LNode; int* arr=new int[n]; for(int i=0;i<n;++i) cin>>arr[i]; sort(arr,arr+n); print_link(create_link(head,arr,n)); return 0; } LNode* create_link(LNode* head,int a[],int n){ LNode* node=new LNode; head->next=node; LNode* p=node; for(int i=0;i<n;++i){ p->value=a[i]; LNode* node=new LNode; p->next=node; p=p->next; } return head; } void print_link(LNode* head){ LNode *p=head->next; while(p->next!=nullptr){ cout<<p->value<<' '; p=p->next; } delete p; }
#include <iostream>
#include <memory>
using namespace std;
struct node{
int data;
shared_ptr<node> next;
node():data(0),next(nullptr){}
node(const int& d):data(d),next(nullptr){}
};
typedef shared_ptr<node> list;
//带头节点的链表插入排序;
void insert_sort(list& l,const int& d){
list no = make_shared<node>(d);//待插入节点
list pre = l;//前驱
list now = l->next;//当前节点
while (now != nullptr && now->data <d){//找到插入位置
pre = now;
now = now->next;
}
//插入
no->next = now;
pre->next = no;
}
void print(const list& l){//打印
list now = l->next;
if(now != nullptr) cout<<now->data;
now = now->next;
while (now != nullptr){
cout<<' '<<now->data;
now = now->next;
}
cout<<endl;
}
int main(){
int n;
int a;
while(cin>>n){
list my_list = make_shared<node>();
for(int i=0;i<n;i++){
cin>>a;
insert_sort(my_list,a);
}
print(my_list);
}
return 0;
} 插入排序,智能指针应用
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n, arr[1001];
while(scanf("%d", &n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d", &arr[i]);
}
sort(arr, arr+n);
for(int i=0;i<n;i++){
if(i==n-1) cout<<arr[i]<<endl;
else
cout<<arr[i]<<" ";
}
}
}
#include <cstdio>
#include <climits>
struct node
{
int data;
node *next;
node(int n) : data(n), next(NULL) {};
};
void insert(node *&head, int num)
{
node *pre_p = head;
node *p = pre_p->next;
while (p)
{
if (p->data > num)
{
break;
}
p = p->next;
pre_p = pre_p->next;
}
pre_p->next = new node(num);
if (p != NULL)
{
pre_p->next->next = p;
}
}
void print_list(node *&head)
{
node *p = head->next;
while (true)
{
printf("%d ", p->data);
p = p->next;
if (p->next == NULL)
{
break;
}
}
printf("%d\n", p->data);
}
void del_list(node *&head)
{
node *p = head;
while (p)
{
node *t = p;
p = p->next;
delete t;
}
}
int main()
{
int n, num;
node *head;
while (scanf("%d", &n) != EOF)
{
head = new node(INT_MIN);
for (int i = 0; i < n; i++)
{
scanf("%d", &num);
insert(head, num);
}
print_list(head);
del_list(head);
}
return 0;
}
while(fscanf(STDIN,"%d",$input)){
$input_array = explode(" ",trim(fgets(STDIN)));
sort($input_array);
echo implode(" ",$input_array)."\n";
}
#include<stdio.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode* next;
}LNode,*list;
list InitList(){
list L;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
return L;
}
void insert(LNode* L,int n){
LNode *p;
p=L->next;
list pre=L;
list q;
while(p){
if(p->data<n){
pre=p;
p=p->next;
}
else break;
}
q=(LNode*)malloc(sizeof(LNode));
q->data=n;
pre->next=q;
q->next=p;
}
void print(list L){
list p=L->next;
while(p->next){
printf("%d ",p->data);
p=p->next;
}
printf("%d",p->data);
}
int main(){
int n,arr[1001];
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
list L;
L=InitList();
for(int i=0;i<n;i++){
insert(L,arr[i]);
}
print(L);
free(L);
}
return 0;
}
//建立升序链表
#include<stdio.h>
#include<stdlib.h>
typedef struct LinkNode{
int data;
struct LinkNode *next;
}LinkNode,*LinkList;
int flag;
void Insert(LinkList phead,int data){
LinkList pn = phead->next;
LinkList pre = phead;
LinkList tmp =(LinkList)malloc(sizeof(LinkList));
tmp->data = data;
tmp->next = NULL;
//寻找插入点 1位于链中 2位于链尾
while(pn && pn->data<data){
pre = pn;
pn=pn->next;
}
tmp->next = pn;
pre->next = tmp;
}
void travel(LinkList phead){
LinkList pn = phead->next;
while(pn){
if(!flag){
printf("%d",pn->data);
flag = 1;
}else printf(" %d",pn->data);
pn=pn->next;
}
}
int main(){
int num,data;
LinkList phead=(LinkList)malloc(sizeof(LinkList));
while(scanf("%d",&num)!=EOF){
flag=0;
phead->next = NULL;
phead->data = num;
for(int i=0;i<num;i++){
scanf("%d",&data);
Insert(phead,data);
}
travel(phead);
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct LNode{
int data;
struct LNode* next;
}LNode;
/* 尾插法创建 带头节点单链表 */
LNode * CreateList(LNode *L,int n){
int x;
L=(LNode*)malloc(sizeof(LNode)); //建立头结点
LNode *s,*r=L; //r为表尾指针
for(int i=0;i<n;i++){
scanf("%d",&x);
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s; // r 指向新的表尾节点
}
r->next=NULL; //千万不要忘了,最后把尾指针指向NULL
return L;
}
/* 将单链表按升序重排 */
LNode* InSort(LNode *L){
LNode *p = L->next;
LNode *pre = NULL;
LNode *r = p->next; //r保持p的后继节点指针,以保证不断链
p->next = NULL; //构造只含一个数据的有序表
p = r;
while(p!=NULL){
r = p->next; //保持p的后继节点指针
pre = L;
while(pre->next!=NULL && pre->next->data < p->data){
pre = pre->next; //在有序表中查找插入 *p 的前驱节点 *pre
}
p->next = pre->next; //将 *p 头插法插入 *pre之后
pre->next = p;
p = r; //扫描原单链表中剩下的结点
}
return L;
}
/* 打印单链表 */
void Print(LNode *L){
LNode *p = L->next;
while(p->next != NULL){
printf("%d ",p->data);
p = p->next;
}
printf("%d",p->data);
}
int main(){
int n;
LNode *L=NULL;
while(scanf("%d",&n) != EOF){
L = CreateList(L,n);
L = InSort(L);
Print(L);
printf("\n");
}
return 0;
}
#include<iostream>
using namespace std;
typedef struct LinkNode
{
int data;
struct LinkNode *next = NULL;
}*LinkList;
void insert(LinkList P, int data)
{
while (P->next && P->next->data<data)
P = P->next;
LinkList T = new LinkNode;
T->data = data;
T->next = P->next;
P->next = T;
}
int main()
{
int n, data;
LinkList List = new LinkNode;
cin >> n;
for (int i = 0; i < n && cin >> data; ++i)
insert(List, data);
LinkList P = List->next;
do cout << P->data << ' ';
while(P = P->next);
}
#include <iostream>
using namespace std;
struct LinkNode {
int val;
LinkNode *next;
LinkNode(int val, LinkNode* next): val(val), next(next) {}
};
int main() {
int n; cin >> n;
LinkNode* dummy = new LinkNode(-1, nullptr);
for (int i = 0; i < n; i++) {
int x; cin >> x;
LinkNode* pre = dummy;
while (pre->next && pre->next->val < x) {
pre = pre->next;
}
pre->next = new LinkNode(x, pre->next);
}
while (dummy->next) {
cout << dummy->next->val << " ";
dummy = dummy->next;
}
}
挑战最简洁代码,感觉写链表题目的时候,最直接的写法就是最好的写法。
#include <cstddef>
#include <iostream>
using namespace std;
struct Node { //链表结点
int data; //数据域
Node* next{}; //指针链域
Node(int data): data(data) {} //构造函数
};
//向链表中插入结点,head为头指针,x为待插入数据
void insert(Node*& head, int x) {
if (head == nullptr || x <= head->data) {
Node* newHead = new Node(x);
newHead->next = head;
head = newHead;
} else {
for (Node* p = head; p; p = p->next) {
if (x > p->data && (p->next == nullptr || x <= p->next->data)) {
Node* node = new Node(x);
node->next = p->next;
p->next = node;
return;
}
}
}
}
int main() {
int n;
while (cin >> n) {
Node* list = nullptr;
while (n--) {
int data;
cin >> data;
insert(list, data);
}
for (Node* p = list; p; p = p->next) {
cout << p->data;
p->next == nullptr ? cout << endl : cout << " ";
}
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
typedef struct linknode{
struct linknode * next;
int value;
}linknode,*linklist;
int main() {
int n=0;
scanf("%d",&n);
int a[n];
linklist l=(linklist)malloc(sizeof(linknode));
linknode *t=l;
linknode* t1=l;
t->value=-1;
for(int i=0;i<n;i++){
int temp;
scanf("%d ",&temp);
linknode *ln=(linklist)malloc(sizeof(linknode));
ln->value=temp;
while(t->value<temp&&t!=NULL){
t1=t;
t=t->next;
}
if(t==NULL){
t1->next=ln;
ln->next=NULL;
t=l;t1=l;
}else {
t1->next=ln;
ln->next=t;
t=l;t1=l;
}
}
t=t->next;
while(t!=NULL){
printf("%d ",t->value);
t=t->next;
}
return 0;
} #include <iostream>
#include <vector>
using namespace std;
class LinkNode {
public:
int val;
LinkNode* next;
LinkNode(int data) : val(data), next(NULL) {}
LinkNode() {}
};
int main() {
int n;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) {
int cur = 0;
cin >> cur;
v.push_back(cur);
}
for (int i = 0; i < v.size(); i++) {
for (int j = v.size() - 1; j > i; j--) {
if (v[j] < v[j - 1]) {
int temp = v[j];
v[j] = v[j - 1];
v[j - 1] = temp;
}
}
}
LinkNode* head = new LinkNode;
LinkNode* p = head;
for (int i = 0; i < v.size(); i++) {
LinkNode* cur = new LinkNode(v[i]);
p->next = cur;
p = cur;
}
while (head->next != NULL) {
cout << head->next->val << " ";
head = head->next;
}
}
// 64 位输出请用 printf("%lld")