[C/C ++] list linked list function implementation model

Source: Internet
Author: User
# Include <iostream> using namespace STD; typedef int t; class list {struct node {t data; node * Next; node (const T & D, node * Next = NULL ): data (D), next (next) {}}; node * head; int Len; public :~ List () {clear ();} List (): Head (), Len () {} int size () const {return Len;} void travel () const {node * P = head; while (p) {cout <p-> data <''; P = p-> next;} cout <Endl ;} void clear () {node * P = head, * q; while (p) {q = p-> next; Delete P; P = Q;} head = NULL, len = 0;} node * & getptr (int n) {If (n <0 | n> Len) n = Len; If (n = 0) return head; node * P = head; For (INT I = 1; I <n; I ++) P = p-> next; return p-> next ;} void insert (const T & D, int N) {node * & P = getptr (n); node * q = new node (d, p); P = Q; ++ Len ;} void erase (int n) {node * & P = getptr (n); If (p) {node * q = P; P = p-> next; Delete Q; -- Len ;}} void remove (const T & D) {node * & P = find (d); If (p) {node * q = P; P = p-> next; Delete Q; -- Len ;}} node * & find (const T & D) {If (LEN = 0 | head-> DATA = d) return head; node * P = head; while (p-> next & P-> next-> data! = D) P = p-> next; return p-> next;} void Update (const T & oldv, const T & newv) {node * P = find (oldv ); if (p) {P-> DATA = newv;} T & Front () {return head-> data;} T & back () {return getptr (len-1) -> data;} void push_front (const T & D) {insert (D, 0);} void push_back (const T & D) {insert (D, Len );} void pop_front () {erase (0);} void pop_back () {erase (len-1);} void sort () {} void reverse () {} T & operator [] (INT index) Throw (const char *) {If (index <0 | index> = Len) throw ""; return getptr (INDEX) -> data ;}}; int main () {list l; L. insert (1, 0); L. insert (2, 0); L. insert (3, 0); L. insert (4, 4); L. insert (5, 4); L. insert (6, 6); L. travel (); L. erase (3); L. remove (2); L. travel (); L. update (5, 6); L. travel (); For (INT I = 0; I <L. size (); I ++) cout <L [I] <','; cout <Endl; return 0 ;}

 

PS: for the interview and test, or want to familiarize yourself with the list of children's shoes

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.