C++ Stack Example: LIFO Stack

Hello! In this guide, I want to tell you about one of the most widely used data structures in C++. This is a type called Stack.

The stack is a data structure organized on the principle of LIFO (last in – first out). It’s usually compared with a gun clip – the shooting starts from the last bullet. It looks like this:

So, let’s start the implementation:

First we should create our data structure:

struct number {
	int value; // value of the element
	number *next; // pointer to the next element
}

Create two pointers of number type:

number *top = NULL, *p;

Now assume that we will read the values from the text file:

do {
	p = new number; // create a new element
	p->next = top; // create the pointer to the previous element
	file >> p->value; // read the value from the file
	top = p; // move the stack’s head
} while (top->value); // repeat the cycle until the document is over
Now we are able to read our stack. Let’s try to insert some value inside the stack:
p = top; // use a temporary pointer for the head of the stack
while (p->value != 7 || p->next != NULL) { // move to the needed value
	p = p->next;
}
number *new_el = new number; // create a new element
new_el->value = 15; // set the value
new_el->next = p->next; // create a pointer to the next element
p->next = new_el; // create a pointer to the previous element

Now let’s try to delete some element:
while (p->value != 7 || p->next != NULL) { // move to the needed value
	p = p->next;
}
number *p2 = p->next; // save the address of the element to the new pointer
p->next = p->next->next; // assign it with the next element
delete p2; // delete element

Now we are able to create a dynamic data structure called stack, read it from the text file, insert some values inside of it and delete them.

Thanks for the attention!

Our C stack example gives you the opportunity to complete your own homework successfully. But don’t hand in our sample as your own assignment. The goal of our example is to help you with your homework of a similar topic (by the way, you can check one more C++ sample here). If you can’t do your task, Assignment.EssayShark.com can do it for you. Don’t leave incomplete tasks, as there are experts who can deal with them easily.

Our service always remains safe – your personal data will be secure and never passed to third parties. Each student can use our service, as we have affordable prices. We are available 24/7, so you can contact us any time you need. When you have a lot of work to do, leave it to our site. Our experts can deal with any topic and assignment of any difficulty.

When you are placing an order on our site, mention your requirements and set the deadline. Testimonials on our site show that we do everything possible to satisfy our customers’ needs. The job of our service is to help students solve their problems with homework.

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.