Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>;
- using namespace std;
- class List {
- struct Node{
- int date_;
- Node* next_;
- };
- Node* head;
- public:
- List();
- void add(int value);
- int size() const;
- int get(int index);
- void del(int index);
- void disp();
- ~List();
- };
- List::List(){
- head = 0;
- }
- List::~List(){
- Node* it = head;
- while ( head != 0 ){
- it = head -> next_;
- delete head;
- head = it;
- }
- }
- void List::add(int value){
- Node* n = new Node;
- n -> next_ = 0;
- n -> date_ = value;
- if ( head == 0 ) {
- head = n;
- } else {
- Node* it = head;
- while (it -> next_ != 0)
- it = it -> next_;
- it -> next_ = n;
- }
- }
- void List::del(int index){
- Node* it = head;
- if ( index == 0 ){
- head = head -> next_;
- delete it;
- } else {
- Node* n = it;
- int i = 0;
- while ( (i < index-2 ) && ( it -> next_ != 0) ){
- it = it -> next_;
- ++i;
- }
- n = it -> next_;
- it -> next_ = n -> next_;
- delete n;
- }
- }
- int List::size() const{
- Node* it = head;
- int i = 0;
- while ( it != 0 ){
- it = it -> next_;
- ++i;
- }
- return i;
- }
- int List::get(int index){
- Node* it = head;
- int i = 0;
- while ( ( i < index ) && ( it -> next_ != 0 ) ){
- it = it -> next_;
- ++i;
- }
- return it -> date_;
- }
- void List::disp(){
- Node* it = head;
- int i = 0;
- while ( it != 0 ) {
- cout << it -> date_ << " ";
- ++i;
- it = it -> next_;
- }
- cout << "size = " << i << endl;
- }
- int main (){
- List l;
- for (int i = 1; i < 10000; i++){
- l.add(i);
- std::cout << i << " ";
- }
- // l.disp();
- // l.del(0);
- // l.disp();
- int n;
- std::cin >> n;
- l.~List();
- }
Add Comment
Please, Sign In to add comment