How to use a database in a C ++ Program

Source: Internet
Author: User

In this tutorial, I suppose everyone knows how to use C ++ classes for work, because all my data structures should be based on them. I have encountered a tutorial on data structures, but it is difficult to find a document written using OOP. Therefore, this will mainly focus on writing data structures with a class.

Stack

A stack is the most common data structure when writing code. It has a simple concept and is easy to write. In this case, there are five books piled up on the table. You want to add one. What should I do? You just need to put the book on the top. What if you want to get 3rd books from this pile of books? You only need one next book to move to the top until the first 3rd books are at the top. Then take 3rd books and place others at the top.

You have noticed that I use the top word. That's right. The top is critical to the stack. The stack can only add data from the top, and the stack is also from the top. That's simple. So what is the use of stack? The stack is used in every process. Each process has a stack, and the data and address are retrieved/added from the stack. The stack top rules also match here. ESP Register adds a pointer pointing to the top of the stack. In any case, explaining how the stack in the process works is beyond the scope of this tutorial. Let's start writing the data structure. Before you start, remember some stack terms. Insert a new element into the stack and delete the element from the stack to become the output stack.

The following is a reference clip:

 
 
  1.  #include   
  2. using namespace std;   
  3. #define MAX 10 // MAXIMUM STACK CONTENT   
  4. class stack   
  5. {   
  6. private:   
  7. int arr[MAX]; // Contains all the Data   
  8. int top; //Contains location of Topmost Data pushed onto Stack   
  9. public:   
  10. stack() //Constructor   
  11. {   
  12. top=-1; //Sets the Top Location to -1 indicating an empty stack   
  13. }   
  14. void push(int a) // Push ie. Add Value Function   
  15. {   
  16. top++; // increment to by 1   
  17. if(top   
  18. {   
  19. arr[top]=a; //If Stack is Vacant store Value in Array   
  20. }   
  21. else   
  22. {   
  23. cout<<"STACK FULL!!"<   
  24. top--;   
  25. }   
  26. }   
  27. int pop() // Delete Item. Returns the deleted item   
  28. {   
  29. if(top==-1)   
  30. {   
  31. cout<<"STACK IS EMPTY!!!"<   
  32. return NULL;   
  33. }   
  34. else   
  35. {   
  36. int data=arr[top]; //Set Topmost Value in data   
  37. arr[top]=NULL; //Set Original Location to NULL   
  38. top--; // Decrement top by 1   
  39. return data; // Return deleted item   
  40. }   
  41. }   
  42. };   
  43. int main()   
  44. {   
  45. stack a;   
  46. a.push(3);   
  47. cout<<"3 is Pushed\n";   
  48. a.push(10);   
  49. cout<<"10 is Pushed\n";   
  50. a.push(1);   
  51. cout<<"1 is Pushed\n\n";   
  52. cout<   
  53. cout<   
  54. cout<   
  55. return 0;   

Output:

3 is Pushed

10 is Pushed

1 is Pushed

1 is Popped

10 is Popped

3 is Popped

We can clearly see that the data in the last stack is the first data in the stack. This is why the stack is LIFO (Last In First Out, Last In First Out ). I guess you understand why.

Let's see how to compile and execute this program. First, we create a variable named top so that it is at the top of the stack. Value-1 indicates that the stack is empty. When there is data input, top automatically adds 1 and stores the data in the arr array. This data structure has a disadvantage. We can only put up to 10 elements.

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.