Data Structure-Dynamic Linked List (C ++)

Source: Internet
Author: User

Data Structure-Dynamic Linked List (C ++)

Define a node:

#include 
 
  using namespace std;typedef int T;struct Node{T data;Node* next;Node(const T& d):data(d), next(NULL){}operator T(){return data;}};int main(){Node a(10), b(20);cout << "a=" << a << ", b=" << b << endl;return 0;}
 
The preceding operator is overloaded. First, convert type a to type T (that is, int). in Java, the toString type is actually strongly converted to the string type.

Output a simple linked list

#include 
 
  using namespace std;typedef int T;struct Node{    T data;    Node* next;    Node(const T& d):data(d), next(NULL){}    operator T(){        return data;    }   };int main(){    Node a(10), b(20), c(30), d(40), e(50);    a.next = &b;     b.next = &c;     c.next = &d;     Node *p = &a;     while(p != NULL){        cout << *p << ' ';        p = p->next;    }    cout << endl;    return 0;}
 
Add an element to the linked list

# Include
 
  
Using namespace std; typedef int T; struct Node {T data; Node * next; Node (const T & d): data (d), next (NULL) {} operator T () {return data ;}; // output linked table void showlist (Node * p) {while (p! = NULL) {cout <* p <''; p = p-> next;} cout <endl ;}int main () {Node a (10 ), B (20), c (30), d (40), e (50);. next = & B; B. next = & c; c. next = & d; showlist (& a); // Add a Node * & p = B. next; // obtain B. next pointer alias e. next = p; p = & e; showlist (& a); // Add another Node * k = new Node (70); Node * & r = c. next; k-> next = r; r = k; return 0 ;}
 

A linked list implemented by C ++ is as follows:

# Include
 
  
Using namespace std; typedef int T; class List {struct Node {T data; Node * next; // T () Zero initialization Node (const T & d = T ()): data (d), next (0) {}}; Node * head; // head pointer int len; public: List (): head (NULL), len (0) {} // insert to any position. // 1. Locate the pointer pn pointing to that position in the linked list. // 2. Point the next member and pn of the new node to the same place // 3. Let pn point to the new node void insert (const T & d, int pos) {Node * & pn = getptr (pos); Node * p = new Node (d); p-> next = pn; pn = p; len ++ ;} // return the length of the linked list int size () const {return len;} // insert void push_back (const T & d) {insert (d, size ());} // find the Node * & getptr (int pos) {if (pos <0 | pos> size () pos = 0; if (pos = 0) return head; Node * p = head; for (int I = 1; I
  
   
Next; return p-> next;} // insert void push_front (const T & d) {insert (d, 0);} // traverse void travel () const {Node * p = head; while (p! = NULL) {cout <p-> data <''; p = p-> next;} cout <endl ;}// clear void clear () {while (head! = NULL) {Node * p = head-> next; delete head; head = p;} len = 0 ;}~ List () {clear ();} // Delete by position // 1. Find the pointer pointing to that position in the linked list // 2. Save the pointer another // 3. Point the pointer to the next node // 4. Release the dynamic memory void erase (int pos) of the pointer) {if (pos <0 | pos> = size () return; Node * & pn = getptr (pos); Node * p = pn; pn = pn-> next; delete p; -- len;} // locate int find (const T & d) const {int pos = 0; Node * p = head; while (p) based on the element) {if (p-> data = d) return pos; p = p-> next; ++ pos;} return-1 ;} // Delete void remove (const T & d) {int pos; while (pos = find (d) based on the element ))! =-1) erase (pos) ;}}; int main () {List l; l. push_front (5); l. push_front (8); l. push_front (20); // insert 9l in 2nd locations. insert (9, 2); l. travel (); return 0 ;}
  
 

We can see that if we want to insert a node, we need to find the pointer pointing to this position (or the previous node). For example, the p-> next pointer is what we need to find. To delete a node, you must find a pointer to the node.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.