Stack Linked List Implementation

Source: Internet
Author: User

1) list. h


 

/** List_2.cpp ** Created on: August 2, 2013 * Author: Huang Dongdong * constantly strives to have a girlfriend like Zhang Zetian ...... */# Include <iostream> using namespace std; typedef int T; class List {struct Node {T data; Node * next; Node (const T & d = T ()): data (d), next (0) {}}; Node * head; int len; public: List (): head (NULL), len (0 ){}~ List () {clear ();} void insert (const T & d, int pos) {Node * & pn = getptr (pos ); node * p = new Node (d); p-> next = pn; pn = p; ++ len;} void push_front (const T & d) {insert (d, 0);} List & push_back (const T & d) {insert (d, size (); return * this;} void clear () {Node * p; while (head! = NULL) {p = head-> next; delete head; head = p ;}} void erase (int pos) {if (pos <0 | pos> = size () {return;} Node * & pn = getptr (pos); Node * p = pn; pn = pn-> next; delete p; -- len;} void remove (const T & d) {int pos; while (pos = find (d ))! =-1) {erase (pos) ;}} void set (int pos, const T & d) {if (pos <0 | pos> = size ()) {return;} getptr (pos)-> data = d;} 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;} int find (const T & d) {Node * p = head; int pos = 0; while (p) {if (p-> data = d) {return pos ;} P = p-> next; pos ++;} return-1;} const int & front () {if (empty () {throw "null ";} return head-> data;} int back () {if (empty () {throw "null";} Node * p = head; while (p-> next! = NULL) {p = p-> next;} return (* p ). data;} bool empty () {return head = NULL;} int size () {return len;} void travel () {Node * p = head; while (p! = NULL) {cout <p-> data <''; p = p-> next ;}cout <endl ;}; // int main () {// List l; // l. push_front (5); // 5 // l. push_front (8); // 8 5 // l. push_front (20); // 20 8 5 // l. insert (9, 2); // 20 8 9 5 // l. insert (6,100); // 6 20 8 9 5 // l. insert (7,-10); // 7 6 20 8 9 5 // l. insert (1, 2); // 7 6 1 20 8 9 5 // 7 6 1 20 8 9 5 10 15 // l. push_back (10 ). push_back (15 ). travel (); // l. erase (0); // 6 1 20 8 9 5 10 15 // l. erase (l. size ()-1); // 6 1 20 8 9 5 10 // l. erase (2); // 6 1 8 9 5 10 // l. travel (); // l. push_back (6); // 6 1 8 9 5 10 6 // l. insert (6, 3); // 6 1 8 6 9 5 10 6 // l. travel (); // l. remove (6); // 1 8 9 5 10 // l. travel (); // l. set (0,666); // 666 8 9 5 10 // l. set (4,789); // 666 8 9 5 789 // l. set (l. find (9), 123); // 666 8 123 5 789 // l. set (1,777); // 666 777 123 5 789 // l. travel (); // c Out <l. front () <".... "<l. back () <',' <l. size () <endl; // while (! L. empty () {// l. erase (0); // cout <"size:" <l. size () <endl; //}/** list_2.cpp ** Created on: August 2, 2013 * Author: huang Dongdong * constantly strives to have a girlfriend like Zhang Zetian ...... */# Include <iostream> using namespace std; typedef int T; class List {struct Node {T data; Node * next; Node (const T & d = T ()): data (d), next (0) {}}; Node * head; int len; public: List (): head (NULL), len (0 ){}~ List () {clear ();} void insert (const T & d, int pos) {Node * & pn = getptr (pos ); node * p = new Node (d); p-> next = pn; pn = p; ++ len;} void push_front (const T & d) {insert (d, 0);} List & push_back (const T & d) {insert (d, size (); return * this;} void clear () {Node * p; while (head! = NULL) {p = head-> next; delete head; head = p ;}} void erase (int pos) {if (pos <0 | pos> = size () {return;} Node * & pn = getptr (pos); Node * p = pn; pn = pn-> next; delete p; -- len;} void remove (const T & d) {int pos; while (pos = find (d ))! =-1) {erase (pos) ;}} void set (int pos, const T & d) {if (pos <0 | pos> = size ()) {return;} getptr (pos)-> data = d;} 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;} int find (const T & d) {Node * p = head; int pos = 0; while (p) {if (p-> data = d) {return pos ;} P = p-> next; pos ++;} return-1;} const int & front () {if (empty () {throw "null ";} return head-> data;} int back () {if (empty () {throw "null";} Node * p = head; while (p-> next! = NULL) {p = p-> next;} return (* p ). data;} bool empty () {return head = NULL;} int size () {return len;} void travel () {Node * p = head; while (p! = NULL) {cout <p-> data <''; p = p-> next ;}cout <endl ;}; // int main () {// List l; // l. push_front (5); // 5 // l. push_front (8); // 8 5 // l. push_front (20); // 20 8 5 // l. insert (9, 2); // 20 8 9 5 // l. insert (6,100); // 6 20 8 9 5 // l. insert (7,-10); // 7 6 20 8 9 5 // l. insert (1, 2); // 7 6 1 20 8 9 5 // 7 6 1 20 8 9 5 10 15 // l. push_back (10 ). push_back (15 ). travel (); // l. erase (0); // 6 1 20 8 9 5 10 15 // l. Erase (l. size ()-1); // 6 1 20 8 9 5 10 // l. erase (2); // 6 1 8 9 5 10 // l. travel (); // l. push_back (6); // 6 1 8 9 5 10 6 // l. insert (6, 3); // 6 1 8 6 9 5 10 6 // l. travel (); // l. remove (6); // 1 8 9 5 10 // l. travel (); // l. set (0,666); // 666 8 9 5 10 // l. set (4,789); // 666 8 9 5 789 // l. set (l. find (9), 123); // 666 8 123 5 789 // l. set (1,777); // 666 777 123 5 789 // l. travel (); // cout <l. front () <".... "<l. back () <',' <L. size () <endl; // while (! L. empty () {// l. erase (0); // cout <"size:" <l. size () <endl ;//}

 

 

2) stack. cpp


 

* Stack_1.cpp ** Created on: August 2, 2013 * in order to have a girlfriend like Zhang Zetian and keep working hard ...... * Come on... Fighting ..... */# Include <iostream> # include "list_2.h" using namespace std; class Stack {List l; public: void push (const T & d) {l. push_front (d);} T pop () {T t = l. front (); l. erase (0); return t;} const T & top () {return l. front ();} bool empty () {return l. empty ();} bool full () {return false;} void clear () {l. clear ();} int size () {return l. size () ;}; int main () {Stack s; s. push (2); s. push (4); s. push (6); s. Push (8); s. push (10); while (! S. empty () {cout <s. pop () <endl ;}}/** stack_1.cpp ** Created on: August 2, 2013 * in order to have a girlfriend like Zhang Zetian and keep working hard ...... * Come on... Fighting ..... */# Include <iostream> # include "list_2.h" using namespace std; class Stack {List l; public: void push (const T & d) {l. push_front (d);} T pop () {T t = l. front (); l. erase (0); return t;} const T & top () {return l. front ();} bool empty () {return l. empty ();} bool full () {return false;} void clear () {l. clear ();} int size () {return l. size () ;}; int main () {Stack s; s. push (2); s. push (4); s. push (6); s. push (8 ); S. push (10); while (! S. empty () {cout <s. pop () <endl ;}}

 

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.