#include <iostream>#include <sstream>#include <vector>#include <string>usingnamespacestd;
// Get the second parameter if the command is push_back or push_frontint getSecondParam(conststring& str){
istringstream iss(str); // To split the str by ' 'string s;
string val;
int flag = 1;
while (iss >> s)
{
if (flag == 2) // Get the second parameter
{
val = s;
break;
}
flag++;
}
unsignedint j;
int data = 0;
for (j = 0; j < val.size(); j++){
int num = 1;
for (unsignedint k = 1; k < val.size()-j; k++)
num *= 10;
data += (val[j]-48) * num;
}
return data;
}
// create a linear list classclass linear_List{
public:
linear_List() {create_List(); } // constractor
~linear_List() {clear_List(); } // destractorvoid create_List();
void push_front(constint& d);
void push_back(constint& d);
void pop_front();
void pop_back();
void reverse();
int max();
private:
struct ListNode{
int data;
ListNode* next;
ListNode(constint& d) : data(d), next(NULL) {}
};
ListNode* head;
void clear_List(){
ListNode* p = head;
while (p){
ListNode* q = p->next;
delete p;
p = q;
}
}
};
// Create a Listvoid linear_List::create_List(){
head = NULL;
}
// insert node in the front of the listvoid linear_List::push_front(constint& d){
ListNode* p = new ListNode(d);
p->next = head;
head = p;
}
// insert node at the end of the listvoid linear_List::push_back(constint& d){
if (head == NULL)
{
head = new ListNode(d);
return ;
}
ListNode* p = new ListNode(d);
ListNode* q = head;
while(q->next)
q = q->next;
q->next = p;
}
// delete the head node from the listvoid linear_List::pop_front(){
if (head == NULL){
cout << "Error" << endl;
return ;
}
ListNode* p = head;
cout << head->data << endl;
head = head->next;
delete p;
p = NULL;
}
// delete the tail node from the listvoid linear_List::pop_back(){
if (head == NULL){
cout << "Error" << endl;
return ;
}
ListNode* p = head;
if (p->next == NULL){
cout << head->data << endl;
head = NULL;
delete p;
p = NULL;
return ;
}
while (p->next->next)
p = p->next;
ListNode* q = p;
p = p->next;
q->next = NULL;
cout << p->data << endl;
delete p;
p = NULL;
delete q;
q = NULL;
}
// Reverst the linear list and print itvoid linear_List::reverse(){
if(head == NULL || head->next == NULL)
return ;
ListNode* p = head;
ListNode* q = head->next;
ListNode* r;
head->next = NULL;
while(q){
r = q->next;
q->next = p;
p = q;
q = r;
}
head = p;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
delete p;
p = NULL;
delete q;
q = NULL;
delete r;
r = NULL;
}
// Get the max value in the listint linear_List::max(){
if (head == NULL){
return0;
}
int max_val = head->data;
ListNode* p = head->next;
while(p){
if (p->data > max_val)
max_val = p->data;
p = p->next;
}
return max_val;
}
int main(){
int n;
cin >> n;
while (cin.get() != '\n')
continue;
vector<string> str(n);
for (int i = 0; i < n; i++){
getline(cin, str[i]);
}
linear_List iList;
for (int i = 0; i < n; i++){
if (str[i] == "pop_front")
iList.pop_front();
elseif (str[i] == "pop_back")
iList.pop_back();
elseif (str[i] == "reverse")
iList.reverse();
elseif (str[i] == "max")
cout << iList.max() << endl;
elseif (str[i].find("push_front") != string::npos){
iList.push_front(getSecondParam(str[i]));
}
elseif (str[i].find("push_back") != string::npos){
iList.push_back(getSecondParam(str[i]));
}
elsecout << "No such function." << endl;
}
return0;
}