Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Optimization of single linked list (13)

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report