Code:
//linkstack.hpp//Stack class #pragma once#include "linklist.hpp" template <typename T> Class Linkstack{public:linkstack (); ~linkstack ();p ublic:int Clear (); int push (t &t); int pop (t &t); int Top (t &T); int size ();p rotected:linklist<t> *m_list;}; Template <typename T>linkstack<t>::linkstack () {m_list = new linklist < T >;} Template <typename T>linkstack<t>::~linkstack () {clear ();d elete m_list;m_list = NULL;} Template <typename t>int linkstack<t>::clear () {T t;while (M_list->getlen () > 0) {m_list->del (0, T) ;} return 0;} Template <typename t>int linkstack<t>::p ush (t &t) {return M_list->insert (t, 0);} Template <typename t>int Linkstack<t>::p op (t &t) {return M_list->del (0, t);} Template <typename t>int linkstack<t>::top (t &t) {return m_list->get (0, t);} Template <typename t>int linkstack<t>::size () {return M_list->getlen ();}
Test procedure for main.cpp//chain storage Stack class # include <iostream> #include <cstdio> #include "linkstack.hpp" using namespace std; struct Student{char name[32];int age;}; void Play () {Student S1, s2, s3;s1.age = 21;s2.age = 22;s3.age = 23; Linkstack<student> ls; Create stack//into stack Ls.push (S1), Ls.push (S2); Ls.push (S3);//Get stack top element student Tmp;ls.top (TMP); cout << "Top of Stack:" << t Mp.age << endl;cout << "size of Stack:" << ls.size () << endl;//out of Stack while (Ls.size () > 0) {ls.po P (TMP);} Ls.clear ();} int main () {play (); return 0;}
The design and implementation of the linked list class see my other two articles: C + + linked list template class, linear chain-based storage design and implementation-API implementation
linklist.h//List class #pragma once#include <iostream> #include <cstdio>using namespace Std;template < TypeName T>struct Node{t T; Node<t> *next;}; Template <typename T>class linklist{public:linklist (); ~linklist ();p ublic:int Clear (); int insert (T &t, int POS); int get (int pos, T &t), int del (int pos, T &t), int getlen ();p rotected:node<t> *header;int length;}; Template <typename t>linklist<t>::linklist () {header = new Node < T >; header->next = null;length = 0; }template <typename t>linklist<t>::~linklist () {node<t> *tmp = Null;while (header) {tmp = header-> Next;delete header;header = tmp;}} Template <typename t>int linklist<t>::clear () {~linklist (); Linklist (); return 0;} Template <typename t>int Linklist<t>::insert (T &t, int pos) {node<t> *cur = null;//fault-tolerant handling of POS if (pos >= length) {pos = length;} cur = header;for (int i = 0; i < POS; ++i) {cur = cur->next;} Cache the T node of the upper application to the container Node<t> *node = new node < T >; node->next = null;node->t = t; Cache t into the container node->next = Cur->next;cur->next = Node;++length;return 0;} Template <typename t>int linklist<t>::get (int pos, T &t) {node<t> *cur = null;if (pos >= length) { return-1;} cur = header;for (int i = 0; i < POS; ++i) {cur = cur->next;} t = cur->next->t; Assign the node of POS position to Treturn 0;} Template <typename t>int linklist<t>::d el (int pos, T &t) {node<t> *cur = null;if (pos >= length) { return-1;} cur = header;for (int i = 0; i < POS; ++i) {cur = cur->next;} node<t> *ret = Null;ret = Cur->next;t = ret->t; Put the cached node to the upper application t//Delete operation Cur->next = Ret->next;--length;delete ret; Note that the memory is freed because the insert is new Node<t>return 0;} Template <typename t>int Linklist<t>::getlen () {return length;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Stack class-Chained storage