Queues & Stacks

Queues & Stacks

Though I’ve been programming in C/C++ for several years now, I’ve never understood how to build a queue or stack. I used to resort to some of the included libraries in Borland C++. But things have changed… I’m going to be moving over to a new O/S, and I want to build a template or class that for both queues and stacks so that I can both learn how they work as well as become overly dependant on the classes. :-)Any ideas would be greatly appreciated.Thank you,Raymond

Queues and stacks are both implemented quite easily in C/C++. The are particularly well suited to C++, which allows you to encapsulate the implementation behind a simple interface.

I won’t write them for you, there are plenty of books around that will do that, but try to give you some ideas to get started. First, both classes could store the data in a simple array. You’ll need to allocate memory from your array, perhaps using malloc, realloc, etc. so that you can resize the array as needed.

In the case of a stack, when an item is “pushed,” expand the array by one and add the pushed item to the end of the array. When it gets “popped,” return the last item and shorten the array by one.

In the case of a queue, the size of the array will probably be fixed. You’ll need two indexes to signify the head and tail of the data. Upon initialization, both indexes will be equal. When an item is added to the queue, put it where the tail points, and increment the tail. When an item is read from the queue, return the value where the head points and increment the head. Be sure to test for the end of the array when incrementing pointers. When a pointer is incremented past the end of the array, just set it to the beginning of the array.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as