Data structure--Dynamic linked list (C + +)

Source: Internet
Author: User

Define a node:

#include <iostream>using namespace Std;typedef int t;struct node{t data; Node* Next; Node (const t& D):d Ata (d), next (NULL) {}operator T () {return data;}}; int main () {Node A (ten), B (+), cout << "a=" << a << ", b=" << b << endl;return 0;}
The above operator overloads first strongly convert the type A to the T type (that is, int), and ToString in Java is actually similar to a strongly converted string type.

Output a simple linked list

#include <iostream>using namespace Std;typedef int t;struct node{    T data;    Node* Next;    Node (const t& D):d Ata (d), next (NULL) {}    operator T () {        return data;    }   }; int main () {    Node A (Ten), B (+), C (+), D (+), E (+);    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 list

#include <iostream>using namespace Std;typedef int t;struct node{t data; Node* Next; Node (const t& D):d Ata (d), next (NULL) {}operator T () {return data;}};/ /Output list void Showlist (node* p) {while (P! = NULL) {cout << *p << ';p = p->next;} cout << Endl;} int main () {Node A (ten), B (+), C (+), D (+), E (+), A.next = &b;b.next = &c;c.next = &d;showlist (&a);//Add a section Point node* & p = b.next;//The alias of the b.next pointer e.next = P;p = &e;showlist (&a);//Add another node node* k = new node (70); node*& r = C.next;k->next = R;r = K;return 0;}

A list of C + + implementations is as follows:

#include <iostream>using namespace Std;typedef int t;class list{struct node{t data; Node * NEXT;//T () 0 initializes node (const t& d=t ()):d ATA (d), next (0) {}}; Node * HEAD; Head pointer int len;public:list (): Head (NULL), Len (0) {}//is inserted anywhere//1, a pointer to that location is found in the list PN//2, the next member of the new node and the PN point to the same place//3, Then 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++;} Returns the list length int size () Const{return Len; End plug void push_back (const t& D) {insert (d, size ());} Find a pointer to a specified position in the linked list node*& getptr (int pos) {if (pos<0 | | pos>size ()) pos = 0;if (pos==0) return head; node* p = head;for (int i=1; i<pos; i++) p = P->next;return P->next;} Pre-inserted 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;} empty void Clear () {while (head!=null) {Node * p = head->next;delete head;head = p;} len = 0;} ~list () {clear ();} Delete by location//1, find the pointer to that position in the list//2, save the pointer to a copy//3, let that fingerPin points to the next node//4, releasing the pointer's dynamic memory void erase (int pos) {if (pos<0 | | pos>=size ()) return; Node *& pn = getptr (POS); Node * p = PN;PN = Pn->next;delete P;--len;} Find the position based on the element int find (const t& d) Const{int pos = 0; node* p = head;while (p) {if (P->data==d) return pos;p = P->next;++pos;} return-1;} remove void Remove (const t& d) {int Pos;while (pos = find (d))! =-1) Erase (POS) according to the element;}}; int main () {List l;l.push_front (5); L.push_front (8); L.push_front (20);//Insert 9l.insert (9, 2) in 2nd position; L.travel (); return 0;}

As can be seen, if we want to insert a node, we need to find a pointer to that position (or the previous node), such as the P->next pointer is what we need to find. Deleting a node also requires finding a pointer to that node.

Data structure--Dynamic linked list (C + +)

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.