链表节点的数据结构为
struct node { int data; struct node* next; }
1.创建链表
1)前插法
void create_list() { int len; head=(node*)malloc(sizeof(node)); head->next=NULL; cout<<"length:"; cin>>len; while(len--) { node*p=(node*)malloc(sizeof(node)); p->next=NULL; cout<<"data:"; cin>>p->data; p->next=head->next; head->next=p; } }
2)尾插法
void create_list2() { int len; head=(node*)malloc(sizeof(node)); head->next=NULL; node* t=head; cout<<"length:"; cin>>len; while(len--) { node*p=(node*)malloc(sizeof(node)); p->next=NULL; cout<<"data:"; cin>>p->data; t->next=p; t=p; } }
2.链表转置
void reverse_list() { if(head==NULL || head->next==NULL) return; node* p=head->next; node* q=p->next; node* r=NULL; p->next=NULL; while(q!=NULL) { r=q->next; q->next=p; p=q; q=r; } head->next=p; }
3.删除最大(最小节点)
void delmax() { if(head==NULL || head->next==NULL) return; node* maxpre=head; node* maxp=head->next; node* cur=head; int ma=head->next->data; while(cur->next!=NULL) { if(cur->next->data>ma) { maxpre=cur; maxp=cur->next; ma=maxp->data; } cur=cur->next; } maxpre->next=maxp->next; free(maxp); }
4.插入排序
void insert_sort() { node* p=head->next; head->next=NULL; while(p!=NULL) { node* q=head; while(q->next!=NULL && q->next->data<p->data) q=q->next; node *r=p->next; p->next=q->next; q->next=p; p=r; } }
5.建立哈希表,用链接法解决冲突
struct hash_node { int data; struct hash_node* next; }; hash_node* HASH[N]; void hash_init() { int i; for(i=0;i<N;i++) { hash_node* h=(hash_node*)malloc(sizeof(hash_node)); h->next=NULL; HASH[i]=h; } } int hash_func(int key) { return key%1003; } void hash_insert(int key) { int h=hash_func(key); hash_node* t=(hash_node*)malloc(sizeof(hash_node)); t->data=key; t->next=NULL; hash_node* p=HASH[h]; while(p->next!=NULL) { p=p->next; } p->next=t; } int hash_search(int key) { int h=hash_func(key); hash_node* p=HASH[h]->next; while(p!=NULL && p->data!=key) p=p->next; return p!=NULL; } void hash_delete(int key) { if(hash_search(key)) { int h=hash_func(key); hash_node* t=HASH[h]; while(t->next!=NULL && t->next->data!=key) t=t->next; hash_node* p=t->next; t->next=p->next; free(p); } }
6.字典树
#define N 26 struct trie_node { int data; struct trie_node* next[N]; }; trie_node* root; //factory method trie_node* new_trie_node() { trie_node* t=(trie_node*)malloc(sizeof(trie_node)); for(int i=0;i<N;i++) t->next[i]=NULL; return t; } void trie_init() { root=new_trie_node(); } void trie_insert(char *str) { int len=strlen(str); trie_node* t=root; for(int i=0;i<len;i++) { trie_node* p=new_trie_node(); t->next[str[i]-'a']=p; if(i==len-1) p->data=1; else p->data=0; t=p; } } int trie_search(char *str) { trie_node* t=root; int len=strlen(str); for(int i=0;i<len;i++) { if(t->next[str[i]-'a']==NULL) return 0; else t=t->next[str[i]-'a']; } return t->data; } void trie_delete(char *str) { if(trie_search(str)) { trie_node* t=root; int len=strlen(str); for(int i=0;i<len;i++) t=t->next[str[i]-'a']; t->data=0; } }
7.图的邻接表表示、BFS、DFS
struct node { int n; //the id of node int color; //whether retrieved node* next; //adjacent list }; vector<node*> v; queue<node*> q; node* new_node(void) { node* t=(node*)malloc(sizeof(node)); t->next=NULL; t->color=0; } void graph_init(int sz) { while(!v.empty()) v.pop_back(); for(int i=0;i<sz;i++) { node* t=new_node(); t->n=i; v.push_back(t); } } void BFS(int i) { while(!q.empty()) q.pop(); q.push(v[i]); v[i]->color=1; while(!q.empty()) { node* t=q.front(); q.pop(); cout<<t->n<<endl; node* p=t->next; while(p!=NULL) { if(v[p->n]->color==0) q.push(v[p->n]),v[p->n]->color=1; p=p->next; } v[t->n]->color=2; } } void DFS(int i) { node* p=v[i]->next; v[i]->color=1; while(p!=NULL) { if(v[p->n]->color==0) DFS(p->n); p=p->next; } v[i]->color=2; cout<<v[i]->n<<endl; } //DFS for(int k=0;k<sz;k++) { if(v[k]->color==0) DFS(k); }
8.网传的微软面试题:判断链表是否存在环路
用两个指针,一个每次前进一步,另一个每次前进两步,如果存在环路,一定会相交,且时间复杂度为O(n)
int check(node* head) { if(head==NULL || head->next==NULL) return 0; node* p1=head; node* p2=head->next; while(p1!=NULL && p2!=NULL) { if(p1==p2) return 1; p1=p1->next; if(p2->next==NULL) break; else { p2=p2->next->next; } } return 0; }
9.将两个按升序排序的链表合并,且合并后的链表也是排序的
//we assume that l1,l2 and l3 are not null void merge_list(node* l1,node* l2,node* l3) { node* p1=l1->next,*p2=l2->next,*p3=l3; node* t; while(p1!=NULL && p2!=NULL) { if(p1->data < p2->data) { t=p1; p1=p1->next; } else { t=p2; p2=p2->next; } p3->next=t; p3=t; } while(p1!=NULL) { t=p1; p1=p1->next; p3->next=t; p3=t; } while(p2!=NULL) { t=p2; p2=p2->next; p3->next=t; p3=t; } p3->next=NULL; }
待续。。