I study in the second year, programming, C++. Please tell about stacks. How is the program written?
Answer 1, Authority 100%
Stack – data structure with access to elements on the LIFO principle (Last in First Out – the last came – the first came out). The data is added to the beginning (the end to whom it is convenient), from there and are extracted. To implement this structure, it is enough to have only two functions and a pointer to the top:
push (item); // Adds the data to the stack
pop (); // Removes the last element
Queue – the structure of data with access to elements on the FIFO principle (first in first out – the first came – the first came out). The data is added to the end, and retrieved from the beginning. To quickly add and remove the data, you will need two pointers, one to the beginning, queue, the second to its end. Functions are used to work with queue:
enqueue (item); // adds a new element in the queue
dequeue (); // removes an element from the queue
To work with pointers in C++, you need to familiarize yourself, for example, with this Article (one of the first in the search).
Then you need to create a data element of your stack (queue). For example:
struct data {
int p;
int c;
}
After that, get a stack cell. For example:
struct element {
Data Element_data;
Element * Next; // pointer to the next element
}
The pointer to the next element is necessary in order to have our data in the stack (queue) had some sequence and were somehow related.
After that, you write the necessary functions, get pointers to the beginning (and end, if necessary). For example, for Push ():
// Somewhere in the code
Element * TOP; // Initialized in NULL
// ...
Void Push (Data NewData) {
// Create our Element structure and pointer to it
Element * NewElement = New Element ();
// element_data = newdata
Element- & gt; element_data = newdata;
// Next = TOP - here we set a bundle between two elements of the stack
Element- & gt; next = TOP;
// Top = pointer to our structure
Top = NewElement;
}
everything else by analogy. At C++, I did not go for a long time (check nowhere), so I apologize to all the guru in advance, correct if he was injected.
Answer 2, Authority 7%
I would describe stack as a package with a collection of coins laid in a stack. From 1 to 10. To gain access to 5, you need to remove 10, then 9, 8, 7, 6 and only then we get the 5th. Well, this personally my IMHO, when it was easier for me to remember so. The rest are only technical questions, and I can not disagree with Dex Ohm …