Implement the Stack separately in C + +

Source: Internet
Author: User

with C language of the way implementation stack:

#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;

}

with C + + data abstraction by way of implementation stack:

#include <iostream>

using namespace Std;

Class Stack

{

struct Link

{

int Data_;

Link* Next_;

Link (int data, link* next):d Ata_ (data), Next_ (next)

{

}

};

Public

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 (int& data)

{

if (Empty ())

{

return false;

}

link* tmp = Head_;

data = head_->data_;

Head_ = head_->next_;

Delete tmp;

--size_;

return true;

}

Private

Link* Head_;

int size_;

};

int main (void)

{

Stack stack;

int i;

for (i = 0; i < 5; i++)

{

Stack. Push (i);

}

while (!stack. Empty ())

{

Stack. Pop (i);

cout << i << "";

}

cout << Endl;

return 0;

}

Implement the Stack separately in 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.