Skip to content

Commit

Permalink
Merge pull request wangzheng0822#2 from wangzheng0822/master
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
Hkesd authored Oct 10, 2018
2 parents b10378f + b1244bb commit ce72454
Show file tree
Hide file tree
Showing 32 changed files with 1,455 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# editor files
.vscode
9 changes: 9 additions & 0 deletions c-cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# main files
main.*

# executives
a.out

# objective files
*.o
*.obj
118 changes: 118 additions & 0 deletions c-cpp/06_linkedlist/palindromeList/LinkedList.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Author: TripleZ<[email protected]>
* Date: 2018-10-10
* Brief: Linked list class.
*/

#ifndef _LINKEDLIST_HPP_
#define _LINKEDLIST_HPP_

#include <cstdio>
#include "ListNode.hpp"

class LinkedList {
public:
int size;
int length;
ListNode *head;
LinkedList();
LinkedList(int size);
~LinkedList();
ListNode* FindElem(int elemVal);
bool DeleteElem(ListNode *elem);
bool DeleteLastElem();
bool InsertElemAtFront(int elemVal);
bool InsertElemAtBack(int elemVal);
void PrintList();
};

LinkedList::LinkedList() {
this -> head = new ListNode();
this -> head->next = nullptr;
this -> head->val = -1;
this -> size = 10; // default
this -> length = 0;
}

LinkedList::LinkedList(int size) {
this -> head = new ListNode();
this -> head->next = nullptr;
this -> head->val = -1;

this -> size = size;
this -> length = 0;
}

LinkedList::~LinkedList() {
ListNode *p, *q;
p = this -> head;
while(p -> next != nullptr) {
q = p -> next;
p -> next = p -> next -> next;
delete q;
}
delete head;
this -> head = nullptr;
this -> length = 0;
}

ListNode* LinkedList::FindElem(int elemVal) {
ListNode *p;
for (p = this -> head; p != nullptr; p = p -> next) {
if (p -> val == elemVal) {
return p;
}
}
return nullptr;
}

bool LinkedList::DeleteElem(ListNode *elem) {
ListNode *prev, *next;
for (prev = this -> head; prev -> next != elem; prev = prev -> next);
next = elem -> next;
prev -> next = next;
delete elem;
this -> length --;
return true;
}

bool LinkedList::DeleteLastElem() {
ListNode *prev, *elem;
for (prev = this -> head; prev -> next -> next != nullptr; prev = prev -> next) ;
elem = prev -> next;
prev -> next = nullptr;
delete elem;
this -> length --;
return true;
}

bool LinkedList::InsertElemAtFront(int elemVal) {
ListNode *newNode = new ListNode();
newNode -> val = elemVal;
newNode -> next = this -> head -> next;
this -> head -> next = newNode;
this -> length ++;
return true;
}

bool LinkedList::InsertElemAtBack(int elemVal) {
ListNode *newNode = new ListNode();
newNode -> val = elemVal;
ListNode *end;
for (end = this -> head; end -> next != nullptr; end = end -> next);
end -> next = newNode;
newNode -> next = nullptr;
this -> length ++;
return true;
}

void LinkedList::PrintList() {
ListNode *elem;
printf("List: ");
for (elem = this -> head -> next; elem -> next != nullptr; elem = elem -> next) {
printf("%d - ", elem -> val);
}
printf("%d\n", elem -> val);
}

#endif
16 changes: 16 additions & 0 deletions c-cpp/06_linkedlist/palindromeList/ListNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Author: TripleZ<[email protected]>
* Date: 2018-10-10
* Brief: ListNode class.
*/

#ifndef _LISTNODE_HPP_
#define _LISTNODE_HPP_

class ListNode {
public:
int val;
ListNode *next;
};

#endif
92 changes: 92 additions & 0 deletions c-cpp/06_linkedlist/palindromeList/palindromeList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Author: TripleZ<[email protected]>
* Date: 2018-10-10
* Brief: Check a list whether is palindrome.
*/

#include <cstdio>
#include "LinkedList.hpp"

bool CheckPalindromeList(LinkedList *list) {
// 使用快慢指针找链表中点
ListNode *slow, *fast, *mid2;
slow = list -> head;
fast = list -> head;
while (fast -> next != nullptr) {
slow = slow -> next;
fast = fast -> next;
if (fast -> next != nullptr) {
fast = fast -> next;
mid2 = slow -> next;
} else {
mid2 = nullptr;
}
}

// 从中点向后逆转链表(区分奇偶情况)
ListNode *mid = slow;
ListNode *elem, *prev, *save;
if (mid2 == nullptr) { // odd
elem = mid;
prev = mid -> next;
} else { // even
elem = mid2;
prev = mid2 -> next;
mid2 -> next = nullptr;
}
save = prev -> next;
mid -> next = nullptr;
while (save != nullptr) {
prev -> next = elem;
elem = prev;
prev = save;
save = save -> next;
}
prev -> next = elem;

ListNode *end = prev;
ListNode *front = list -> head -> next;

// 从头尾同时遍历比较,检测链表是否为回文
bool palindrome = true;
while (front != end) {
// printf("%d, %d\n", front -> val, end -> val);
if (front -> val != end -> val) {
palindrome = false;
break;
}
front = front -> next;
end = end -> next;
}

palindrome ? printf("The list is palindrome~\n") : printf("The list is not palindrome!\n");

return palindrome;
}

int main(int argc, char const *argv[]) {

int init[] = {1, 2, 3, 2, 1};
LinkedList *list = new LinkedList(5);
for (int i = 0; i < 5; i++) {
list -> InsertElemAtBack(init[i]);
}
list -> PrintList();

CheckPalindromeList(list); // true

list -> InsertElemAtFront(5);
CheckPalindromeList(list); // false


int init2[] = {1, 2, 3, 3, 2, 1};
LinkedList *list2 = new LinkedList(10);
for (int i = 0; i < 6; i++) list2 -> InsertElemAtBack(init2[i]);
list2 -> PrintList();
CheckPalindromeList(list2);

list2 -> InsertElemAtBack(4);
CheckPalindromeList(list2);

return 0;
}
22 changes: 22 additions & 0 deletions c-cpp/07_linkedlist/linked_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 单链表
*
* Author: Liam Huang (Liam0205)
*/

#ifndef LINKEDLIST_LINKED_LIST_H_
#define LINKEDLIST_LINKED_LIST_H_

#include <memory>

template <typename T>
struct Node {
using ptr_t = std::shared_ptr<Node<T>>;
T data;
ptr_t next;

Node(T data_) : data(data_), next(nullptr) {}
Node() : next(nullptr) {}
};

#endif // LINKEDLIST_LINKED_LIST_H_
Loading

0 comments on commit ce72454

Please sign in to comment.