提交 f9153162 编写于 作者: cosmicing's avatar cosmicing

上传新文件

上级 d1d17c57
#include <iostream>
#include <list>
#include<algorithm>
#include<stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
typedef int DataType;
class ListNode {
public:
DataType data;
ListNode *next;
ListNode() : data(0), next(nullptr) {}
ListNode(DataType x) : data(x), next(nullptr) {}
ListNode(DataType x, ListNode *next) : data(x), next(next) {}
};
class LinkList {
public:
ListNode *m_Head;
LinkList() {
m_Head = nullptr;
}
~LinkList()
{
DeleteAll();
}
void Initiate();
void DeleteAll();
void listHeadCreate();
ListNode * listHeadCreate(vector<int> nums);
void printList();
ListNode * reverse(ListNode *pHead);
ListNode* swapPairs(ListNode* head);
};
void LinkList::DeleteAll() {
ListNode *pNode = m_Head;
ListNode *nextNode;
while(pNode != nullptr) {
nextNode = pNode->next;
delete pNode;
pNode = nextNode;
}
m_Head = nullptr;
}
void LinkList::Initiate() {
DeleteAll();
m_Head = nullptr;
}
ListNode * LinkList::listHeadCreate(vector<int> nums) {
DeleteAll();
ListNode *newNode,*pNode,*rNode;
pNode = nullptr;
for(int i = 0; i< nums.size(); i++) {
ListNode *newNode = new ListNode;
newNode->data = nums[i];
//cout << nums[i] << endl;
newNode->next = nullptr;
if(pNode == nullptr) {
pNode = newNode;
rNode = newNode;
} else {
rNode->next = newNode;
rNode = newNode;
}
}
rNode->next = nullptr;
return pNode;
}
void LinkList::listHeadCreate() {
DeleteAll();
ListNode *newNode,*pNode,*rNode;
pNode = nullptr;
for(int i = 0;i<=10;i++) {
ListNode *newNode = new ListNode;
newNode->data = i;
newNode->next = nullptr;
if(pNode == nullptr) {
pNode = newNode;
rNode = newNode;
} else {
rNode->next = newNode;
rNode = newNode;
}
}
rNode->next = nullptr;
m_Head = pNode;
}
ListNode * LinkList::swapPairs(ListNode *headNode) {
//终止条件:链表只剩一个节点或者没节点了,没得交换了.返回的是已经处理好的链表
if(headNode == nullptr || headNode->next == nullptr) {
return headNode;
}
//一共三个节点:headNode, nextNode, swapPairs(nextNode->next)
//下面的任务便是交换这3个节点中的前两个节点
ListNode *nextNode = headNode->next;
headNode->next = swapPairs(nextNode->next);
nextNode->next = headNode;
//根据第二步:返回给上一级的是当前已经完成交换后,即处理好了的链表部分
return nextNode;
}
ListNode *LinkList::reverse(ListNode *pHead) {
if(pHead == nullptr) {
return nullptr;
}
ListNode *tmp = nullptr;
ListNode *pre = nullptr;
while(pHead) {
tmp = pHead->next;
pHead->next = pre;
pre = pHead;
pHead = tmp;
}
return pre;
}
void LinkList::printList() {
ListNode *pNode;
//pNode = m_Head->next;
pNode = m_Head;
cout << "LinkList traversal:" << endl;
cout << " ";
while(pNode != nullptr) {
cout << pNode->data << " ";
pNode = pNode->next;
}
cout << endl;
}
vector<int> Str2Int(char *str) {
vector<int> nums;
const char *split = ",";
char *p = strtok(str,split);
int val;
while(p!=nullptr) {
sscanf(p,"%d",&val);
nums.push_back(val);
p = strtok(nullptr, split);
}
cout << "Str2Int output:";
for(int i = 0; i< nums.size(); i++)
{
cout << nums[i] << " ";
}
cout << endl;
return nums;
}
string StrProcess(string str) {
int pos1 = str.find("[");
int pos2 = str.find("]");
string newStr = str.substr(pos1+1, pos2-pos1-1);
//newStr.erase(std::remove(newStr.begin(), newStr.end(), ','), newStr.end());
return newStr;
}
#define N 200
int main() {
#if 0
char str[1000] = { 0 };
cin.getline(str, 1000);
cout << str << endl;
#else
char str[N] = {"head = [1,2,3,4,5,6,7,8,9,10]"};
#endif
string data(str); //String 就是这个指针, 因此需要赋值字符串.
data = StrProcess(data);
cout << data << endl;
int i;
for(i=0;i<data.length();i++)
{
str[i] = data[i];
}
str[i] = '\0';
char *p = str;
vector<int> nums;
nums = Str2Int(p);
LinkList *sol = new LinkList;
sol->Initiate();
cout << "----------Create begin--------------" << endl;
sol->m_Head = sol->listHeadCreate(nums);
sol->printList();
cout << "----------Create end--------------" << endl;
#if 0
ListNode *dummy = new ListNode(0);
dummy->next = sol->m_Head;
sol->printList();
sol->swapPairs(dummy);
sol->m_Head = dummy->next;
sol->printList();
#else
sol->printList();
cout << "----------swapPairs begin--------------" << endl;
//如果只执行:
// sol->swapPairs(sol->m_Head);
//而不对m_Head重新赋值:
// sol->m_Head = sol->swapPairs(sol->m_Head);
//那么由于m_Head还指向原来的位置,就会从第二个节点开始打印,从而打印时少了一个元素;
sol->m_Head = sol->swapPairs(sol->m_Head);
sol->printList();
cout << "----------swapPairs end--------------" << endl;
#endif
cout << "----------reverse begin--------------" << endl;
//这里要像上面的swapPairs一样:记得给m_Head重新赋值;
//否则reverse也会出错
sol->m_Head = sol->reverse(sol->m_Head);
sol->printList();
cout << "----------reverse end--------------" << endl;
return EXIT_SUCCESS;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册