02-线性结构2. Reversing Linked List (25)

来源:互联网 发布:网络电视需要盒子么 编辑:程序博客网 时间:2024/06/05 03:46
#include <iostream>#include <string>#include <vector>#include <utility>using namespace std;struct Record{string firstLoca;int NeedFirst;string NextLoca;Record *Next;};void free(Record *buff){//if (buff->Next == nullptr)//return;//free(buff->Next);//delete buff;Record *Temp = buff->Next;while (Temp){delete buff;buff = Temp;Temp = Temp->Next;}}void MyRevese(Record* &ptrFirst, Record* &ptrLast){if (ptrFirst == nullptr)return;Record* rd = ptrLast->Next;Record* p = ptrFirst->Next;Record* p1 = ptrFirst;while (p != rd){Record* Temp = p->Next;p->Next = p1;p1 = p;p = Temp;}ptrFirst->Next = p;swap(ptrFirst, ptrLast);}int main(){string first;int nSize;int NeedFirst;cin >> first >> nSize >> NeedFirst;vector<Record> vec(nSize);for (int i = 0; i < nSize; ++i){string strTemp;int loca;cin >> strTemp;cin >> loca;vec[loca - 1].NeedFirst = loca;vec[loca - 1].firstLoca = strTemp;vec[loca - 1].Next = nullptr;cin >> strTemp;vec[loca - 1].NextLoca = strTemp;}Record *headptr = new Record({ vec[0].firstLoca, vec[0].NeedFirst, vec[0].NextLoca, vec[0].Next });Record *lastptr = headptr;for (auto begin = vec.begin() + 1; begin != vec.end(); ++begin){lastptr->Next = new Record({ begin->firstLoca, begin->NeedFirst, begin->NextLoca, begin->Next });lastptr = lastptr->Next;}Record *Test = headptr;while (--NeedFirst > 0)Test = Test->Next;MyRevese(headptr, Test);Test = headptr;while (nSize-- > 0){cout << Test->firstLoca << " " << Test->NeedFirst << " " << Test->NextLoca << endl;Test = Test->Next;}free(headptr);return 0;}
在vs2013上运行的好好的,可是不知道为什么在pat上说段错误。
0 0
原创粉丝点击