分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
// 反转单链表的循环算法(C++)#include <iostream>#define NULL 0using namespace std;struct Node{ char data; Node* next;};Node* create(){ Node* head = NULL; Node* rear = head; Node* p; // The pointer points to new created node. char tmp; do { cout << "Please input positive integer or #:"; cin >> tmp; if(tmp != -1) { p = new Node; p->data = tmp; p->next = NULL; if(head == NULL) { head = p; } else { rear->next = p; } rear = p; } } while(tmp != '#'); return head;}void print(Node* head){ cout << "The current list is: "; Node* p = head; if(head != NULL) { do { cout << p->data << cout << ' '; p = p->next; } while(p != NULL); } cout << "\r\n";}void reverse(Node*& head) // Use & here since the function body changed the head pointer.{ if(head == NULL) { return; } Node*pre, *cur, *ne; pre = head; cur = head->next; while(cur) { ne = cur->next; // Store next pointer. cur->next = pre; // Reverse the current code pointer. pre = cur; cur = ne; } head->next = NULL; head = pre;}int main(){ Node* list = create(); print(list); reverse(list); print(list); return 0;}// Output:/*Please input positive integer or #:1Please input positive integer or #:5Please input positive integer or #:8Please input positive integer or #:3Please input positive integer or #:2Please input positive integer or #:7Please input positive integer or #:9Please input positive integer or #:fPlease input positive integer or #:gPlease input positive integer or #:tPlease input positive integer or #:YPlease input positive integer or #:BPlease input positive integer or #:#The current list is: 10FC0C3E8 50FC0C3E8 80FC0C3E8 30FC0C3E8 20FC0C3E8 70FC0C3E8 90FC0C3E8 f0FC0C3E8 g0FC0C3E8 t0FC0C3E8 Y0FC0C3E8 B0FC0C3E8 #0FC0C3E8The current list is: #0FC0C3E8 B0FC0C3E8 Y0FC0C3E8 t0FC0C3E8 g0FC0C3E8 f0FC0C3E8 90FC0C3E8 70FC0C3E8 20FC0C3E8 30FC0C3E8 80FC0C3E8 50FC0C3E8 10FC0C3E8*/
// 反转单链表的递归算法(C++)#include <iostream>#define NULL 0using namespace std;struct Node{ char data; Node* next;};Node* create(){ Node* head = NULL; Node* rear = head; Node* p; // The pointer points to the new created node. char tmp; do { cout << "Please input integer or char '#':"; cin >> tmp; if(tmp != '#') { p = new Node; p->data = tmp; p->next = NULL; if(head == NULL) { head = p; } else { rear->next = p; } rear = p; } } while(tmp != '#'); return head;}void print(Node* head){ cout << "The current list is: "; Node* p = head; if(head != NULL) { do { cout << p->data << cout << ' '; p = p->next; } while(p != NULL); } cout << "\r\n";}Node* reverseSingleLinkedListRecursive(Node* p, Node*& head){ if(p == NULL || p->next == NULL) { head = p; return p; } Node* tmp = reverseSingleLinkedListRecursive(p->next, head); tmp->next = p; p->next = NULL; // To prevent forming a ring. return p;}int main(){ Node* list = create(); print(list); reverseSingleLinkedListRecursive(list, list); print(list); return 0;}// Output:/*Please input integer or char '#':1Please input integer or char '#':2Please input integer or char '#':4Please input integer or char '#':6Please input integer or char '#':3Please input integer or char '#':8Please input integer or char '#':7Please input integer or char '#':9Please input integer or char '#':0Please input integer or char '#':#The current list is: 10F69C3E8 20F69C3E8 40F69C3E8 60F69C3E8 30F69C3E8 80F69C3E8 70F69C3E8 90F69C3E8 00F69C3E8The current list is: 00F69C3E8 90F69C3E8 70F69C3E8 80F69C3E8 30F69C3E8 60F69C3E8 40F69C3E8 20F69C3E8 10F69C3E8*/
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/2e19ffb3303a490d20ea5d00dd397156.gif)