Note: This period of time in the school recruit, asked a lot of linked list of questions, although can be completely good written out, but it took a lot of time, rage I put the basic list of all the operations are rewritten again
Note: Suitable for the learning chain list and have some experience in the chain of operation of the students look, the code can be all through, you can rest assured that the use of
header file: List.h
#include <iostream> #include <assert.h> using namespace std;
#include <vector> struct Node {int value;
struct Node *next;
Node (int v=0): value (v), next (NULL) {}};
Class List {public:void insert (int value=0) {Node *node=buynode (value);
if (head==null) {head=node;
Return
}//Head plug node->next=head->next;
node->next=head;
Head=node;
} Void Show (node *head) {node *cur=head;
while (cur) {cout<<cur->value<< "";
cur=cur->next;
} cout<<endl;
} void Print () {print (head);
}//Linked list inverse void Resver () {resver (head);
///from the tail print chain table void Print_tail () {print_tail (head);
Delete a list void remove (int value) {Remove (Head,value) in O (1) time;
}//List of reciprocal K nodes node* get_k (int k) {return get_k (head,k);
//Find two linked list of public nodes node* Find_comm_node (list *list1,list *list2) {if (List1 ==null | | list2 ==null) return NULL;
Return Find_comm_node (List1->head,list2->head); ///Merge two ordered list Node* Megre (List *head,list *phead) {if (Head==null | | | phead==null) return NULL;
Return Megre (Head->head,phead->head);
}//List merge the second way node* megre (list *head,list *phead) {if (Head==null | | phead==null) return NULL;
Return Megre (Head->head,phead->head);
//Returns the length of the list int size () {return size (head);
///By value insert bool Insert_value (int value) {return insert_value (head,value);
} private:node* Find_value (node *head,int value) {node *cur=head;
while (cur && cur->value!=value) cur=cur->next;
return cur;
BOOL Insert_value (node *head,int value) {node *key=find_value (head,value);
if (key==null) return false;
Node *node=buynode (value);
node->next=key->next;
key->next=node;
return true;
int size (Node *head) {int len=0;
while (head) {++len;
head=head->next;
return Len;
} node* Megre (Node *head,node *phead) {if (Head==null | | | phead==null) return NULL;
Node *root=null; NoDe *cur=null;
while (head && phead) {if (Head->value <= phead->value) {if (cur==null) Cur=head;
else {cur->next=head;
cur=cur->next;
} if (Root==null) root=cur;
head=head->next;
}else {if (cur = = NULL) cur=phead;
else {cur->next=phead;
cur=cur->next;
} if (Root==null) root=cur;
phead=phead->next;
} if (Head==null) cur->next=phead;
else cur->next=head;
return root;
}/////////////////////////node* Megre (Node *head,node *phead) {if (Head==null | | | phead==null) return NULL;
Vector<node *> v;
Node *fisrt=head;
Node *second=phead;
while (fisrt && second) {if (Fisrt->value <= second->value) {Node *tmp=fisrt;
V.push_back (TMP);
fisrt=fisrt->next;
}else {Node *tmp=second;
V.push_back (TMP);
second=second->next;
} Node *root=v[0]; Node *cur=root;
int size=v.size ();
for (int i=1; i<size; ++i) {cur->next=v[i];
cur=cur->next;
} if (Fisrt==null) cur->next=second;
else cur->next=fisrt;
return root;
int Getlen (node *head) {node *cur=head;
int len=0;
while (cur) {cur=cur->next;
len++;
return Len;
} node* getcur (Node *head,int len) {while (len--) {head=head->next;
return head; } node* Getcommnode (Node *head,node *phead) {while (head && phead && head!=phead) {HEAD=HEAD-&G
T;next;
phead=phead->next;
return head;
} node* Find_comm_node (Node *head,node *phead) {if (Head==null | | | phead==null) return NULL;
int Head_len=getlen (head);
int Phead_len=getlen (phead);
Node *cur=null;
if (Head_len > Phead_len) {cur=getcur (Head,head_len-phead_len);
Cur=getcommnode (Cur,phead);
}else {cur=getcur (Phead,phead_len-head_len);
Cur=getcommnode (Cur,head);
return cur; Node *Get_k (Node *head,int k) {if (head==null) return NULL;
Node *cur=head;
for (int i=0; i<k; ++i) {if (cur==null) return cur;
cur=cur->next;
Node *result=head;
while (cur!=null) {result=result->next;
cur=cur->next;
return result;
} void Remove (node *head,int value) {node *cur=head; while (cur!=null) {//delete non-tail node if (cur->value ==value && cur->next!=null) {node *TMP=CUR-&G
T;next;
cur->value=tmp->value;
cur->next=tmp->next;
Delete tmp;
Break
} if (Cur->next ==null) break;
cur=cur->next;
///delete the condition of the tail node if (cur->next ==null) {node *cur=head;
while (Cur->next!=null && cur->next->value!=value) cur=cur->next;
Node *tmp=cur->next;
cur->next=null;
Delete tmp;
} void Print_tail (Node *&head) {if (head==null) return;
else {print_tail (head->next); Cout<
Test file: main.cpp
#include "list.h" void Fun () {list T;
int value=0;
while (Cin>>value,value!=-1) {T.insert (value);
} t.print ();
cout<< "Size:" <<t.size () <<endl; /*while (cin>>value && value!=-1) {if (T.insert_value (value)) cout<< "Insert Success" <<endl
;
else cout<< "Insert Failed" <<endl;
T.print ();
}*/List mylist;
while (Cin>>value,value!=-1) {Mylist.insert (value);
} mylist.print ();
List tmp; Node *cur=tmp.
Megre (&t,&mylist);
Tmp.show (cur);
/* List mylist;
while (Cin>>value,value!=-1) {Mylist.insert (value);
} mylist.print ();
List tmp;
Node *cur=tmp.megre (&t,&mylist);
Tmp.show (cur);
T.resver ();
T.print ();
T.print_tail ();
cout<< "Please enter value:";
cin>>value;
T.remove (value);
int k=0;
cout<< "Please enter K:";
cin>>k;
Node *head=t.get_k (k);
if (head!=null) cout<