c语言 单向链表的归并排序的实现,单链表基本操作+归并排序(C/C++实现)

#include

using namespace std;

struct Data_type{

char name[15];

int score;

void out(){

printf("(%s:%d)",name,score);

}

};

bool operator ==(Data_type l, Data_type r){

if(l.score!=r.score) return 0;

if(strlen(l.name)!=strlen(r.name))return 0;

int n=strlen(l.name);

for(int i=0;i

if(l.name[i]!=r.name[i])return 0;

}

return 1;

}

bool operator

if(l.score!=r.score) return l.score>r.score;

int n=max(strlen(l.name), strlen(r.name));

for(int i=0;i

if(l.name[i]!=r.name[i])return l.name[i]

}

return 0;

}

struct Node{

Data_type data;

Node* next;

};

struct link_list{

Node* head;

int SIZE;

void bulid(){

head= new Node;

head->next= NULL;

SIZE=0;

}

int size(){

return SIZE;

}

bool push(Data_type x, int k){

if(size()

Node* now= head;

while(k--){

now= now->next;

}

Node* temp= new Node;

temp->data= x;

temp->next= now->next;

now->next= temp;

SIZE++;

return 1;

}

void push_front(Data_type data){

push(data, 0);

}

void push_back(Data_type data){

push(data, size());

}

Node* find(int pos){

if(size()

Node* ret= head;

while(pos--){

ret= ret->next;

}

return ret;

}

vector query(Data_type data){

vectorret;

int cnt=0;

Node* now= head;

while(now->next!=NULL){

now= now->next;

cnt++;

if(now->data==data){

ret.push_back(cnt);

}

}

return ret;

}

void pop(int pos){

if(pos<1||pos>size()){

printf("error\n");

return ;

}

Node* now= head;

pos--;

while(pos--){

now= now->next;

}

Node* temp= now->next;

now->next= now->next->next;

delete(temp);

SIZE--;

}

void pop_front(){

pop(1);

}

void pop_back(){

pop(size());

}

void print(Node* x){

while(x!=NULL){

printf("-->");

x->data.out();

x= x->next;

}

}

void print(){

Node* now= head;

while(now->next!=NULL){

now= now->next;

printf("--> ");

now->data.out();

}

printf("\n");

}

void clear(Node* x){

if(x==NULL) return;

clear(x->next);

delete(x);

SIZE--;

}

void clear(){

clear(head);

}

Node* find_inv(int pos){

if(pos>size()) return NULL;

Node* ret= head, *now= head;

while(pos--){

now= now->next;

}

while(now!=NULL){

now= now->next;

ret= ret->next;

}

return ret;

}

void reverse(){

if(size()<=0) return ;

Node* now= head->next;

while(now->next!= NULL){

Node* temp= now->next;

now->next= now->next->next;

temp->next= head->next;

head->next= temp;

}

}

Node* Get_mid(Node* x){

Node* mid=x, *p=x;

while(p->next!=NULL&&p->next->next!=NULL){

p= p->next->next;

mid= mid->next;

}

Node* ret= mid->next;

mid->next= NULL;

return ret;

}

Node* merge(Node* x, Node* y){

Node* ret=NULL, *tail=NULL;

while(x!=NULL&&y!=NULL){

if(x->data < y->data){

Node* temp= x->next;

if(ret==NULL){

ret= tail= x;

ret->next= NULL;

}

else {

tail->next= x;

tail= tail->next;

tail->next= NULL;

}

x=temp;

}

else{

Node* temp= y->next;

if(ret==NULL){

ret= tail= y;

ret->next= NULL;

}

else {

tail->next= y;

tail= tail->next;

tail->next= NULL;

}

y=temp;

}

}

if(x!=NULL){

tail->next= x;

}

else{

tail->next= y;

}

return ret;

}

Node* sort(Node* x){

if(x->next==NULL) return x;

Node* mid= Get_mid(x);

Node* p1= sort(x);

Node* p2= sort(mid);

return merge(p1, p2);

}

void sort(){

head->next= sort(head->next);

}

};

int main(){

link_list ans;

ans.bulid();

Data_type temp;

temp.name[0]='s';

temp.name[1]='a';

temp.name[2]='m';

temp.name[3]='y';

temp.name[4]='1';

temp.name[5]='\0';

temp.score=5;

ans.push_front(temp);

temp.name[4]='2';

temp.score=15;

ans.push_front(temp);

temp.name[4]='3';

temp.score=150;

ans.push_back(temp);

ans.print();

ans.reverse();

ans.sort();

ans.print();

return 0;

}

/*

小目标:

1.创建链表 ok

2.在链表头部插入元素x ok

3.在链表尾部插入元素x ok

4.在链表第k个元素后插入元素x ok

5.查询当前链表长度 ok

6.查询链表第k个元素 ok

7.查询链表中所有值为x的元素,返回vector ok

8.删除链表中的第k个元素(将空间释放) ok

9.删除链表头 ok

10删除链表尾 ok

11.清空链表 ok

12.链表翻转 ok

13.顺次输出整个链表 ok

14.查找链表中倒数第k个元素。(另写) ok

15.链表排序(O(nlogn)) ok

*/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值