#include <iostream>
#include <stdexcept>
#define eleType double
using namespace std;
struct ListNode {
eleType data;
ListNode* next;
ListNode(eleType x):data(x),next(nullptr){}
};
class LinkedList {
private:
ListNode* head;
int size;
public:
LinkedList():head(nullptr),size(0){}
~LinkedList();
void insert(int index, eleType value);
void remove(int index);
ListNode* find(eleType value);
ListNode* get(int index);
void update(int index, eleType value);
void print();
};
LinkedList::~LinkedList() {
ListNode* curr = head;
while (curr) {
ListNode* temp = curr;
curr = curr->next;
delete temp;
}
}
void LinkedList::insert(int index, eleType value) {
if (index<0 || index>size) {
throw std::out_of_range(“invalid position”);
}
ListNode* newNode = new ListNode(value);
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
ListNode* curr = head;
for (int j = 0; j < index – 1; j++) {
curr = curr->next;
}
newNode->next=curr->next;
curr->next=newNode;
}
size++;
}
void LinkedList::remove(int index) {
if (index < 0 || index >= size) {
throw std::out_of_range(“invalid position”);
}
if (index == 0) {
ListNode* temp = head;
head=temp->next;
delete temp;
}
else {
ListNode* curr = head;
for (int j = 0; j < index-1; j++) {
curr = curr->next;
}
ListNode* temp=curr->next;
curr->next = temp->next;
delete temp;
}
size–;
}
ListNode* LinkedList::find(eleType value) {
ListNode* curr = head;
while (curr != nullptr && curr->data != value) {
curr = curr->next;
}
return curr;
}
ListNode* LinkedList::get(int index) {
if(index<0||index>=size){
std::out_of_range(“invalid position”);
}
ListNode* curr = head;
for (int j = 0; j < index; j++) {
curr=curr->next;
}
return curr;
}
void LinkedList::update(int index, eleType value) {
if (index < 0 || index >= size) {
throw std::out_of_range(“invalid position”);
}
get(index)->data = value;
}
void LinkedList::print() {
ListNode* curr = head;
while (curr) {
cout << curr->data << ” “;
curr = curr->next;
}
cout << endl;
}
int main() {
LinkedList mylist;
for (int i = 0; i < 10; i++) {
mylist.insert(i, i * 100);
}
mylist.print();
cout<< mylist.find(500)->data<<endl;
cout<< mylist.get(2)->data<<endl;
mylist.insert(0, 666);
mylist.print();
mylist.update(1, 555);
mylist.print();
mylist.remove(0);
mylist.print();
mylist.remove(1);
mylist.print();
return 0;
}
1.A+B for Input-Output Practice
| A+B for Input-Output Practice (IV) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 207939 Accepted Submission(s): 107841 Problem Description Your task is to Calculate the sum of some integers. Input Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed. Output For each group of input integers you should output their sum in one line, and with one line of output for each line in input. Sample Input 4 1 2 3 4 5 1 2 3 4 5 0 Sample Output 10 15 Author lcy |
#include <iostream>
#include <stdexcept>
#define eleType double
using namespace std;
struct ListNode {
eleType data;
ListNode* next;
ListNode(eleType x) :data(x), next(nullptr) {}
};
class LinkedList {
private:
ListNode* head;
int size;
public:
LinkedList() :head(nullptr), size(0) {}
~LinkedList();
void insert(int index, eleType value);
void remove(int index);
ListNode* find(eleType value);
ListNode* get(int index);
void update(int index, eleType value);
void print();
};
LinkedList::~LinkedList() {
ListNode* curr = head;
while (curr) {
ListNode* temp = curr;
curr = curr->next;
delete temp;
}
}
void LinkedList::insert(int index, eleType value) {
if (index<0 || index>size) {
throw std::out_of_range(“invalid position”);
}
ListNode* newNode = new ListNode(value);
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
ListNode* curr = head;
for (int j = 0; j < index – 1; j++) {
curr = curr->next;
}
newNode->next = curr->next;
curr->next = newNode;
}
size++;
}
void LinkedList::remove(int index) {
if (index < 0 || index >= size) {
throw std::out_of_range(“invalid position”);
}
if (index == 0) {
ListNode* temp = head;
head = temp->next;
delete temp;
}
else {
ListNode* curr = head;
for (int j = 0; j < index – 1; j++) {
curr = curr->next;
}
ListNode* temp = curr->next;
curr->next = temp->next;
delete temp;
}
size–;
}
ListNode* LinkedList::find(eleType value) {
ListNode* curr = head;
while (curr != nullptr && curr->data != value) {
curr = curr->next;
}
return curr;
}
ListNode* LinkedList::get(int index) {
if (index < 0 || index >= size) {
std::out_of_range(“invalid position”);
}
ListNode* curr = head;
for (int j = 0; j < index; j++) {
curr = curr->next;
}
return curr;
}
void LinkedList::update(int index, eleType value) {
if (index < 0 || index >= size) {
throw std::out_of_range(“invalid position”);
}
get(index)->data = value;
}
void LinkedList::print() {
ListNode* curr = head;
while (curr) {
cout << curr->data << ” “;
curr = curr->next;
}
cout << endl;
}
int main() {
LinkedList mylist;
eleType x;
while (cin >> x && x!=0) {
eleType sum = 0;
for (int j = 0; j < x; j++) {
eleType num;
cin >> num;
sum+= num ;
}
cout << sum<<endl;
}
return 0;
}
//英雄哥版
#include <iostream>
#include <stdexcept>
#define eleType double
using namespace std;
struct ListNode {
eleType data;
ListNode* next;
ListNode(eleType x) :data(x), next(nullptr) {}
};
class LinkedList {
private:
ListNode* head;
int size;
public:
LinkedList() :head(nullptr), size(0) {}
~LinkedList();
void insert(int index, eleType value);
void remove(int index);
ListNode* find(eleType value);
ListNode* get(int index);
void update(int index, eleType value);
void print();
eleType sum();
};
LinkedList::~LinkedList() {
ListNode* curr = head;
while (curr) {
ListNode* temp = curr;
curr = curr->next;
delete temp;
}
}
void LinkedList::insert(int index, eleType value) {
if (index<0 || index>size) {
throw std::out_of_range(“invalid position”);
}
ListNode* newNode = new ListNode(value);
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
ListNode* curr = head;
for (int j = 0; j < index – 1; j++) {
curr = curr->next;
}
newNode->next = curr->next;
curr->next = newNode;
}
size++;
}
void LinkedList::remove(int index) {
if (index < 0 || index >= size) {
throw std::out_of_range(“invalid position”);
}
if (index == 0) {
ListNode* temp = head;
head = temp->next;
delete temp;
}
else {
ListNode* curr = head;
for (int j = 0; j < index – 1; j++) {
curr = curr->next;
}
ListNode* temp = curr->next;
curr->next = temp->next;
delete temp;
}
size–;
}
ListNode* LinkedList::find(eleType value) {
ListNode* curr = head;
while (curr != nullptr && curr->data != value) {
curr = curr->next;
}
return curr;
}
ListNode* LinkedList::get(int index) {
if (index < 0 || index >= size) {
std::out_of_range(“invalid position”);
}
ListNode* curr = head;
for (int j = 0; j < index; j++) {
curr = curr->next;
}
return curr;
}
void LinkedList::update(int index, eleType value) {
if (index < 0 || index >= size) {
throw std::out_of_range(“invalid position”);
}
get(index)->data = value;
}
void LinkedList::print() {
ListNode* curr = head;
while (curr) {
cout << curr->data << ” “;
curr = curr->next;
}
cout << endl;
}
eleType LinkedList::sum() {
eleType ret = 0;
ListNode* curr = head;
while (curr != NULL) {
ret += curr->data;
curr = curr->next;
}
return ret;
}
int main() {
eleType n;
while (cin >> n && n) {//modify the x in only one word
LinkedList mylist;
for (int j = 0; j < n; j++) {
eleType num;
cin >> num;
mylist.insert(j, num);
}
cout << mylist.sum()<<endl;
}
return 0;
}