《程序员面试白皮书》关于链表的问题编程实践

来源:互联网 发布:xp限制安装软件 编辑:程序博客网 时间:2024/04/30 12:21

//node.h

<span style="font-size:18px;">#ifndef _NODE_HEAD_FILE_#define _NODE_HEAD_FILE_typedef struct _node {int val;struct _node* next;}Node;Node* make_list(int);void print_node(Node*);Node* make_list_without_head(int);void print_node_without_head(Node*);Node* reorder_list_without_head(Node*, int);Node* find_middle_element(Node*);Node* find_K_to_last_element(Node*, unsigned);Node* circle_existing_return_entry_point(Node*);Node* make_list_without_head_loop_exists(int);Node* rotate_right_k_nodes(Node*, unsigned);Node* reverse_single_linklist(Node*);Node* swap_adjacent_nodes(Node*);Node* swap_adjacent_nodes_version2_0(Node*);Node* merge_2_sorted_linklist(Node*, Node*);void reverse_traverse(Node*);Node* list_add_on(Node*, Node*);#endif //_NODE_HEAD_FILE_~</span>

//node.cpp

#include "node.h"#include <cstdlib>#include <iostream>using namespace std;Node* make_list(int num) {Node* head = (Node*)malloc(sizeof(Node));head->next = nullptr;Node* p = nullptr;Node* q = head;for (int i = 0; i < num; ++i){p = (Node*)malloc(sizeof(Node));cout << "Input data:" << endl;int data;cin >> data;p->val = data;p->next = q->next;q->next = p;q = p;}return head;}Node* make_list_without_head(int num) {Node* head = (Node*)malloc(sizeof(Node));head->next = nullptr;Node* p = nullptr;Node* q = head;for (int i = 0; i < num; ++i){p = (Node*)malloc(sizeof(Node));cout << "Input data:" << endl;int data;cin >> data;p->val = data;p->next = q->next;q->next = p;q = p;}return head->next;}Node* make_list_without_head_loop_exists(int num) {Node* head = (Node*)malloc(sizeof(Node));head->next = nullptr;Node* p = nullptr;Node* q = head;Node* r = nullptr;for (int i = 0; i < num; ++i){p = (Node*)malloc(sizeof(Node));cout << "Input data:" << endl;int data;cin >> data;p->val = data;p->next = q->next;q->next = p;r = q;q = p;}q->next = r;return head->next;}void print_node(Node* head) {if (head == nullptr) {cout << "No data at all\n" << endl;}else {Node* p = head->next;while (p != nullptr) {cout << p->val;p = p->next;}cout << endl;}}void print_node_without_head(Node* head) {if (head == nullptr) {cout << "No data at all" << endl;}else {Node* p = head;while (p != nullptr) {cout << p->val;p = p->next;}cout << endl;}}Node* reorder_list_without_head(Node* head, int x) {if (head == nullptr) {return nullptr;}Node* new_head = nullptr;Node* aDummy = new Node();Node* bDummy = new Node();Node* aCurr = aDummy;Node* bCurr = bDummy;while (head != nullptr) {Node* next = head->next;head->next = nullptr;if (head->val < x) {aCurr->next = head;aCurr = head;}else {bCurr->next = head;bCurr = head;}head = next;}aCurr->next = bDummy->next;new_head = aDummy->next;delete aDummy;delete bDummy;return new_head;}Node* find_middle_element(Node* head) {if (head == nullptr) {return nullptr;}Node* fast = head;Node* last = head;while (fast != nullptr && fast->next != nullptr) {fast = fast->next->next;last = last->next;}return last;}Node* find_K_to_last_element(Node* head, unsigned int k) {if (head == nullptr || k < 0) {return nullptr;}Node* fast = head;Node* last = head;for (unsigned i = 0; i < k; ++i) {fast = fast->next;if (fast == nullptr) {return nullptr;}}while (fast->next != nullptr) {fast = fast->next;last = last->next;}return last;}Node* circle_existing_return_entry_point(Node* head) {if (head == nullptr) {return nullptr;}Node* fast = head;Node* last = head;while (fast != nullptr && fast->next != nullptr) {fast = fast->next->next;last = last->next;if (fast == last) {break;//loop exists or reach the end}}if (fast == nullptr || fast->next == nullptr) {return nullptr;}last = head;while (fast != last) {fast = fast->next;last = last->next;}return fast;//or return last}Node* rotate_right_k_nodes(Node* head, unsigned k) {if (head == nullptr || k <= 0) {return head;}unsigned length = 0;Node* p = head;while (p->next != nullptr) {++length;p = p->next;}//p is the tail of linklistp->next = head;//construct a loop circleNode* new_tail = head;//find new tailk %= length;//importantint pos = length - k - 1;while (pos-- >= 0) {new_tail = new_tail->next;}//find new headNode* new_head = new_tail->next;new_tail->next = nullptr;return new_head;}Node* reverse_single_linklist(Node* head) {if (head == nullptr) {return nullptr;}Node* prev = nullptr;//save pointer to work doneNode* next = nullptr;while (head != nullptr) {next = head->next;//save pointer to rest nodes for node 'head'head->next = prev;prev = head;head = next;}return prev;}Node* swap_adjacent_nodes(Node* head) {if (head == nullptr) {return nullptr;}Node* dum = new Node();dum->next = head;Node* prev = dum;Node* n1 = head;Node* n2 = nullptr;Node* new_head = nullptr;Node* temp = nullptr;while (n1 != nullptr && n1->next != nullptr) {n2 = n1->next;temp = prev->next;prev->next = n1->next;n1->next = temp;n1->next = n2->next;n2->next = temp;prev = n1;//caused bugsn1 = prev->next;//caused bugs}new_head = dum->next;delete dum;return new_head;}Node* swap_adjacent_nodes_version2_0(Node* head) {if (head == nullptr) {return nullptr;}Node* dum = new Node();dum->next = head;Node* prev = dum;Node* n1 = head;Node* n2 = nullptr;while (n1 != nullptr && n1->next != nullptr) {n2 = n1->next;prev->next = n2;n1->next = n2->next;n2->next = n1;prev = n1;n1 = prev->next;}Node* new_head = dum->next;delete dum;return new_head;}Node* merge_2_sorted_linklist(Node* head1, Node* head2) {if (head1 == nullptr && head2 == nullptr) {return nullptr;}if (head1 == nullptr && head2 != nullptr) {return head2;}if (head1 != nullptr && head2 == nullptr) {return head1;}Node* dum = new Node();Node* p = dum;Node* new_head;while (head1 != nullptr && head2 != nullptr) {if (head1->val <= head2->val) {p->next = head1;head1 = head1->next;}else {p->next = head2;head2 = head2->next;}p = p->next;}if (head1 == nullptr) {p->next = head2;}if (head2 == nullptr) {p->next = head1;}new_head = dum->next;delete dum;return new_head;}void reverse_traverse(Node* head) {if (head == nullptr) {return;}reverse_traverse(head->next);cout << head->val << endl;}Node* list_add_on(Node* head1, Node* head2) {if (head1 == nullptr && head2 == nullptr) {return nullptr;}if (head1 != nullptr && head2 == nullptr) {return head1;}if (head1 == nullptr && head2 != nullptr) {return head2;}Node* dum = new Node();Node* curr = dum;Node* new_head = nullptr;//int carry = 0;int add_on = 0;int sum = 0;while (head1 != nullptr && head2 != nullptr) {sum = head1->val + head2->val + add_on;add_on = sum / 10;curr->next = new Node();curr->next->val = sum % 10;head1 = head1->next;head2 = head2->next;curr = curr->next;}Node* rest = (head1 == nullptr ? head2 : head1);while (rest != nullptr) {sum = rest->val + add_on;curr->next = new Node();curr->next->val = sum % 10;add_on = sum / 10;curr = curr->next;rest = rest->next;}if (add_on > 0) {curr->next = new Node();curr->next->val = add_on;}new_head = dum->next;delete dum;return new_head;}

//main.cpp(每一个注释之间对应一个用例)

#include <cstdlib>#include <iostream>#include "node.h"using namespace std;int main() {Node* head;//head = make_list_without_head(4);//print_node_without_head(head);/*head = reorder_list_without_head(head, 3);//print_node_without_head(head);*//*Node *p = find_middle_element(head);if (p != nullptr) {cout << p->val << endl;}*//*Node* p = find_K_to_last_element(head, 2);if (p != nullptr) {cout << p->val << endl;}*//*head = make_list_without_head_loop_exists(5);//head = make_list_without_head(5);//print_node_without_head(head);Node* p = circle_existing_return_entry_point(head);if (p != nullptr) {cout << p->val << endl;}else {cout << "No loop exists" << endl;}*//*head = make_list_without_head(5);print_node_without_head(head);head = rotate_right_k_nodes(head, 2);print_node_without_head(head);*//*head = make_list_without_head(5);print_node_without_head(head);head = reverse_single_linklist(head);print_node_without_head(head);*//*head = make_list_without_head(5);print_node_without_head(head);//head = swap_adjacent_nodes(head);head = swap_adjacent_nodes_version2_0(head);print_node_without_head(head);*//*Node* head1 = make_list_without_head(7);//Node* head2 = make_list_without_head(5);Node* head3 = nullptr;head = merge_2_sorted_linklist(head1, head3);print_node_without_head(head);*//*head = make_list_without_head(5);reverse_traverse(head);*/        /*Node* head1, *head2;head1 = make_list_without_head(4);head2 = make_list_without_head(6);head = list_add_on(head1, head2);print_node_without_head(head);        */return 0;}


0 0