C + + Primer Learning Notes _26_ class and Data Abstraction (12)--using C and C + + to implement a chain stack

Source: Internet
Author: User

The following is achieved by using C and C + + to implement a chain stack (linked list implementation), from which the idea of data encapsulation abstraction is realized:


"C + + implementation"

#include <iostream>using namespace Std;class stack{private:struct Link {int data_;        Link *next_;    Link (int data, link *next): Data_ (data), Next_ (next) {}};p Ublic:stack (): Head_ (0), Size_ (0) {} ~stack ()        {Link *tmp;            while (head_) {tmp = Head_;            Head_ = head_->next_;        Delete tmp;        }} void Push (const int data) {link *node = new Link (data, head_);        Head_ = node;    ++size_;    } bool Empty () {return (Size_ = = 0);        } bool Pop () {if (Empty ()) {return false;        } Link *tmp = Head_;        Head_ = head_->next_;        Delete tmp;        --size_;    return true;        } int Top () {if (Empty ()) {return false;    } return head_->data_;    }private:link *head_; int size_;};          int main (void) {stack stack;    int i; for (i = 0; i < 5; i++) {stack.     Push (i); } while (!stack. Empty ()) {cout << stack.        Top () << ""; Stack.    Pop ();    } cout << Endl; return 0;}

Operation Result:

4 3 2) 1 0



"C Language Implementation"

#include <stdio.h> #include <stdlib.h> #include <assert.h>struct link{int data; struct Link *next;};    struct stack{struct Link *head; int size;};    void Stackinit (struct Stack *stack) {stack->head = NULL; stack->size = 0;}    void Stackpush (struct Stack *stack, const int data) {struct Link *node;    node = (struct link) malloc (sizeof (struct link));    ASSERT (node! = NULL);    Node->data = data;    Node->next = stack->head;    stack->head = node; ++stack->size;} int stackempty (struct Stack *stack) {return (Stack->size = = 0);}    int Stackpop (struct Stack *stack, int *data) {if (Stackempty (Stack)) {return 0;    } struct Link *tmp = stack->head;    *data = stack->head->data;    Stack->head = stack->head->next;    Free (TMP);    --stack->size; return 1;}    void Stackcleanup (struct Stack *stack) {struct Link *tmp;        while (stack->head) {tmp = stack->head; Stack->head = stack->head->next;    Free (TMP); } stack->size = 0;}    int main (void) {struct stack stack;    Stackinit (&stack);    int i;    for (i = 0; i < 5; i++) {Stackpush (&stack, i); } while (!        Stackempty (&stack)) {Stackpop (&stack, &i);    printf ("%d", I);    } printf ("\ n"); return 0;}

Operation Result:

4 3 2) 1 0


The output is consistent, contrasting different ways of writing, you can experience the differences between the two languages.


Reference:

C + + Primer Fourth Edition
Effective C + + 3rd

http://blog.csdn.net/jnu_simba/article/details/9284587

http://blog.csdn.net/zjf280441589/article/details/24671691

C + + Programming specification

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer Learning Notes _26_ class and Data Abstraction (12)--using C and C + + to implement a chain stack

Related Article

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.