How the database is used in C + + programs

Source: Internet
Author: User
Tags data structures

In this tutorial, I assume that everyone knows how to work with C + + classes because all of my data structures have to be based on them. I've had a tutorial on data structures, but it's hard to find a script that uses OOP. Therefore, this will mainly focus on using a class to write data structures.

Stack

When writing code, the stack is the most commonly used data structure. Its concept is simple, writing is also relatively simple. There is a situation in which there are piles of 5 books on the table, and you want to add one. What should I do? Just put the book on the top. What if you want to take the 3rd book out of this pile of books? All you have to do is move the book to the top until the 3rd book is at the top. Then take the 3rd book and put the other on top.

You've noticed that I use the word top. Yes, top (stack top) is critical to the stack. The stack only allows data to be added from the top, and the stack/fallback stack is also from the top of the stack. It's so simple. What's the use of the stack? The stack is used in every process. Each process is a stack where data and addresses are removed from the stack/added in. The stack top rule is also consistent here. The ESP Register adds a pointer to the top of the stack. Anyway, it's beyond the scope of this tutorial to explain how the stack in the process works, so let's start writing data structures. Before you begin, please remember some of the stack terms. Inserting a new element into the stack becomes the stack, and removing the element from the stack becomes the stack.

The following is a reference fragment:
#include
using namespace Std;
#define MAX//MAXIMUM STACK CONTENT
Class Stack
{
Private
int Arr[max]; Contains all the Data
int top; Contains location of topmost Data pushed onto Stack
Public
Stack ()//constructor
{
Top=-1; Sets the top Location to-1 indicating a empty stack
}
void push (int a)//push ie. ADD Value Function
{
top++; Increment to by 1
if (top
{
Arr[top]=a; If Stack is vacant store Value in Array
}
Else
{
cout<< "STACK full!!" <
top--;
}
}
int pop ()//Delete Item. Returns the deleted item
{
if (top==-1)
{
cout<< "STACK is EMPTY!!!" <
return NULL;
}
Else
{
int Data=arr[top]; Set topmost Value in data
Arr[top]=null; Set Original Location to NULL
top--; Decrement Top by 1
return data; Return deleted item
}
}
};
int main ()
{
Stack A;
A.push (3);
cout<< "3 is pushed\n";
A.push (10);
cout<< "is pushed\n";
A.push (1);
cout<< "1 is pushed\n\n";
cout<
cout<
cout<
return 0;
}

The output is:

3 is pushed

The IS pushed

1 is pushed

1 is popped

The IS popped

3 is popped

We can clearly see the last stack of data first out of the stack. This is why the stack is a LIFO (LIFO, last in first). I guess you understand why, too.

Let's see how to compile and execute this program. We start by creating a variable called top, which places it on the stack. Assignment-1 indicates that the stack is empty. When there is data entry, top automatically adds 1, and the data is stored in the ARR array. There is a drawback to this data structure. We can only put 10 elements at a maximum.

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.