#include <iostream> #include <stdlib.h> using namespace std; typedef struct node{ int data; struct node* next; }n,*pn; pn create_s_list(int length){ int data; pn head,node,p; head=(pn)malloc(sizeof(n)); //分配空间 p=(pn)malloc(sizeof(n)); //分配空间 cout<<"input(1):"; cin>>data; p->data=data; head->next=p; for(int i=1;i<length;i++) { node=(pn)malloc(sizeof(n)); //分配空间 cout<<"input("<<i+1<<"):"; cin>>data; node->data=data; node->next=NULL; p->next=node; p=p->next; } return head; } void MTF(pn head,int data){ pn pre; pre=head->next; pn node; node=head->next; if(node->data==data) return; node=node->next; while(node->next!=NULL){ if(node->data==data){ pre->next=node->next; node->next=head->next; head->next=node; return; } pre=pre->next; node=node->next; } } void output_data_s_list(pn head){ pn p=head->next; cout<<"data: "; while(p->next!=NULL){ //输出数据 cout<<'['<<p->data<<']'<<" -> "; p=p->next; } cout<<'['<<p->data<<']'<<endl; } int main(){ pn head=create_s_list(10); cout<<"input element to search:"; int data; cin>>data; MTF(head,data); output_data_s_list(head); }
为什么没有Parent树高度的算法说明