Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Optimization of single linked list (13)

Shulou Source: shulou.com Published: 2022-06-02 19:20:25 09月11日 Update

We implemented a single linked list before, so how do we traverse every data element in a single linked list? It can be done directly with a for loop. Let's take a look at the current method of traversing a single linked list based on our implementation. The main.cpp is as follows

# include # include "LinkList.h" using namespace std;using namespace DTLib;int main () {LinkList list; for (int item0; inext = toDel- > next; delete toDel; next; delete toDel; bool set bool set (int I, const Te) {bool ret = ((0 next- > value = e;} return ret) } T get (int I) const {T ret; if (get (I, ret)) {return ret;} else {THROW_EXCEPTION (IndexOutOfBoundsException, "Invaild parameter i to get element...");}} bool get (int I, Te) const {bool ret = ((0 next- > value) } return ret;} int find (const Te) const {int ret =-1; int I = 0; Node* node = massively header.next; while (node) {if (node- > value = = e) {ret = I; break } else {node = node- > next; iTunes;}} return ret;} int length () const {return m_header.next;} void clear () {while (m_header.next) {Node* toDel = m_header.next M_header.next = toDel- > next; delete toDel;} m_length = 0;} bool move (int I, int step = 1) {bool ret = (0 0); if (ret) {m_current = position (I)-> next; m_step = step;} return ret } bool end () {return (m_current = = NULL);} T current () {if (! end ()) {return current-> value;} else {THROW_EXCEPTION (INvalidOPerationException, "No value at current position...") }} bool next () {int I = 0; while ((I

< m_step) && !end() ) { m_current = m_current->

Next; iTunes;} return (I = = m_step);} ~ LinkList () {clear ();}};} # endif / / LINKLIST_H

Main.cpp source code

# include # include "LinkList.h" using namespace std;using namespace DTLib;int main () {LinkList list; for (int iTuno; I)

Tags: Time cursor result move complexity complexity loop output element function efficiency data code time facet encapsulation position even number variable point to Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi NVidia vpn OPPO Reno