Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CellularList.h Source File

CellularList.h

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef CELLULAR_LIST_H_
00019 #define CELLULAR_LIST_H_
00020 
00021 #include <stddef.h>
00022 
00023 namespace mbed {
00024 
00025 /** Class CellularList
00026  *
00027  *  Templated linked list class for common usage.
00028  *
00029  */
00030 template <class T> class CellularList
00031 {
00032 private:
00033     T *_head, *_tail;
00034 public:
00035     CellularList()
00036     {
00037       _head=NULL;
00038       _tail=NULL;
00039     }
00040 
00041     T* add_new()
00042     {
00043       T *temp=new T;
00044       if (!temp) {
00045           return NULL;
00046       }
00047       temp->next = NULL;
00048       if (_head == NULL) {
00049         _head = temp;
00050       } else {
00051         _tail->next=temp;
00052       }
00053       _tail = temp;
00054 
00055       return _tail;
00056     }
00057 
00058     void delete_last()
00059     {
00060         T* previous = NULL;
00061         T *current=_head;
00062 
00063         if (!current) {
00064             return;
00065         }
00066 
00067         while (current->next != NULL) {
00068             previous=current;
00069             current=current->next;
00070         }
00071 
00072         if (previous) {
00073             _tail=previous;
00074             previous->next=NULL;
00075         } else {
00076             _head = NULL;
00077             _tail = NULL;
00078         }
00079 
00080         delete current;
00081     }
00082 
00083     void delete_all()
00084     {
00085         T *temp = _head;
00086         while (temp) {
00087             _head = _head->next;
00088             delete temp;
00089             temp = _head;
00090         }
00091     }
00092 
00093 
00094     T *get_head()
00095     {
00096         return _head;
00097     }
00098 };
00099 
00100 } // namespace mbed
00101 
00102 #endif // CELLULAR_LIST_H_