链表

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>
#define O(x) cout<<#x<<" "<<x<<endl;
#define B cout<<"Breakpoint"<<endl;
using namespace std;
int read(){
	int x = 1,a = 0;char ch = getchar();
	while (ch < '0'||ch > '9'){if (ch == '-') x = -1;ch = getchar();}
	while (ch >= '0'&&ch <= '9'){a = a*10+ch-'0';ch = getchar();}
	return x*a;
}
int T,n;
struct node{
	int data;
	struct node *nextptr;
}n1,n2;
struct node *create_a_list(){
	int num = read();
	struct node *curptr;
	struct node *headptr = NULL,*lastptr = NULL;
	while (num != -1){
		curptr = (struct node*)malloc(sizeof(struct node));
		curptr -> data = num;
		num = read();
		if (headptr == NULL) headptr = curptr,lastptr = curptr;
		else lastptr -> nextptr = curptr,lastptr = curptr;
	}
	lastptr -> nextptr = NULL;
	return headptr;
}
void destory(node *head){
	struct node *tmpptr = head;
	while (head != NULL){
		free(head);
		head = head -> nextptr;
	}
}
node *insert(node *head,int val){
	node *tmphead = head;
	node *curptr = (struct node*)malloc(sizeof(struct node));
	curptr -> data = val,curptr -> nextptr = NULL;
	if (head == NULL){
		head = curptr;
		return head;
	}
	node *preptr = head,*nowptr = head -> nextptr;
	if (preptr -> data >= val){
		curptr -> nextptr = head;
		return curptr;
	}
	if (nowptr == NULL){
		preptr -> nextptr = curptr;
		return preptr;
	}
	head = head -> nextptr;
	while (head -> nextptr != NULL){
		if (nowptr -> data >= val&&preptr -> data <= val){
			preptr -> nextptr = curptr;
			curptr -> nextptr = nowptr;
			break;
		}
		head = head -> nextptr;
		preptr = nowptr,nowptr = head;
	}
	if (val > head -> data) head -> nextptr = curptr;
	return tmphead;
}
int findval(node *head,int val){
	node *tmphead = head;
	while (tmphead != NULL){
		if (tmphead -> data == val) return 1;
		tmphead = tmphead -> nextptr;
	}
	return 0;
}
node *sort_list(node *head1,node *head2){
	node *tmpnode1 = head1,*tmpnode2 = head2;
	while (tmpnode1 != NULL){
	//	cout<<"---"<<tmpnode1 -> data<<endl;
		tmpnode2 = insert(tmpnode2,tmpnode1 -> data);
		tmpnode1 = tmpnode1 -> nextptr;
	}
	return tmpnode2;
}
signed main(){
	struct node *head = create_a_list();
	struct node *tmphead = head,*tmp1head = head;
	while (head != NULL){
		printf("%d\n",head -> data);
		head = head -> nextptr;
	}
	node *head2;
	head = sort_list(tmphead,head2);
	while (head != NULL){
		printf("%d\n",head -> data);
		head = head -> nextptr;
	}
//	destory(head);
//	while (head != NULL){
//		printf("%d\n",head -> data);
//		head = head -> nextptr;
//	}
}
posted @ 2023-02-23 16:08  小又又yyyy  阅读(7)  评论(0编辑  收藏  举报