## Data Structures and Algorithms C++

**Data Structures**** and Algorithms C++ –** means arranging or organizing the elements in a particular way. When we say we have to arrange elements, those elements can be organized in different forms. For example, socks can be arranged in various different ways. You can just keep it in your cupboard all messed up. Or you can keep it neatly folded. The best way can be folding and arranging them color wise. So for searching a particular pair of socks, the third arrangement is perfect.

In a similar way of organization of socks, Data can be also organized in different ways or forms. These different ways of organizing data are called as Data Structure. Let’s see a formal definition of a data structure and the data structures and algorithms basics.

**Data Structures And Algorithms C++:**

**The logical or mathematical model of a particular organization of data.**

OR

**It is a particular way of organizing data in a computer so that it can be used.**

Similarly to socks; different organization of list data structures and algorithms C++ available is –

**Array****Linked List****Stack****Queue****Tree****Graph****Hash Table****Heap****Records****Tables**

These data structures and algorithms C++ are very important while programming. A good programmer always gives emphasis on data structure rather than code. Each programming language works on various data structures and algorithms in C++. Data structures that are available in C++ are as follows.

4.5 (5,639 ratings)

View Course

**Array****Linked List****Stack****Queue****Tree****Graph****Hash Table****Heap**

### Let’s discuss this one by one:

**#1 Array**

Array is a simplest type of data structures and algorithms C++. The array is defined as a Fix-size sequential collection of data elements of the same data type. E.g. a0=12, a1=21,a2=14,a3=15….We can represent one-dimensional array as shown in figure:

Where

0,1,2,3…..n is called subscript or index

a[1],a[2],…a[n] is called subscript variable

It can be 1-Dimensional, 2-Dimensional, 3-Dimensional and so on multi-Dimensional.

In memory array stores into contiguous memory locations.

The lowest address corresponds to the first element

The highest address corresponds to the last element

We can declare 1-D (1-Dimensional) array in **C++** as follows

dataType arrayName[arraySize];

E.g. int num[5];

Initializing Array in C++

num={23, 10, 12, 3, 6};

We can combine declaration and initialization into a single statement as follows.

int num={23, 10, 12, 3, 6};

When we want to dynamically allocate the size of an array then we should new operator as follows

int * a= new int[size];

**The disadvantage **of the array are insertion and deletion of elements is slow as in ordered array and its fixed size storage.

**#2 Linked List**

List refers to a linear collection of items. A linked list is a series of connected nodes (data element) as shown in figure 3. Header node points to the first node of the list and the last node points to NULL indicated byÆ. As each node contains at least.

- A piece of data (any type)
- Pointer to the next node in the list

Linked List is represented in memory using two arrays. One array stores Information called info that is data to be stored and other stores next-pointer field called LINK that is an address of the next node.

**An advantage of a linked list over an array:**

Both an array and a linked list are representations of a list of items in memory. The important difference is the way in which the items are linked together. The main limitation of the array is element insertion into array and element deletion from the ordered array are difficult as rest elements have to be move. Insertion and deletion of elements from a linked list are very simple.

**Note:**Become a C++ Developer

Learn to design and customize programs for various platforms. Code, test, debug, and implement software applications. Develop skills to ensure applications run smoothly.

**Types of Linked List are:**

**1. Singly linked list**: contains only one linked field which holds the address of next node in the list and info filed which holds information to be stored.

**2. Single Circular Linked List **is a single list only but the last node of the list contains the address of the first node instead of null. That is content of head and next field of the last node are same.

**3. The doubly linked list** contains two linked field previous and next. A previously linked field which holds an address of the previous node in the list and next linked field holds the address of the next node in the list and info filed holds the information to be a store.

**4. Double Circular Linked List **is doubly linked list but next field of the last node contains the address of the first node instead of null.

**Implementation of linked list in C++ **involves the creation of node, deletion of a node from the list, insertion of a newly created node into the list and searching a node with a particular key.

Code for **creation of the node** is given as follows:

**Inserting a node into the list **involves three cases

1. **Inserting a node at the beginning** means inserting the newly created node as starting node. For inserting a node at the beginning first have created a new node and make new node point to old start, and then update start to point to new node as shown in below figure:

Code for **inserting a node at the beginning:**

2. **Inserting a node at the tail** means inserting the newly created node as the last node. For inserting the node as a tail node have to create a new node and make old last node point to the new node and then update tail to point to new node.

3. **Inserting a node at a given position** involves the creation of new temp node, Then have to find the position of insertion of the newly created node.

Code for insertion of the node at a given a position:

**Deleting a node from list **involves removing a node from existing list. Deletion of the node from link list is simple than inserting a node into the list. In C++ code for deletion of the node is given as follows:

**Traversing a node** with a particular key (value) from a list will search a node from the list whose info will match with the key of a given node. The following C++ code will traverse a list. data structures and algorithms C++

**#3 Stack**

A stack is a list of elements in which an element may be inserted or deleted only at one end, called the top of the stack. Consider the example of a tower of Hanoi. Here when we have to insert a disc we have to insert it from the top only and similarly removal of the disc is takes place from the top only.

Stack uses LIFO principle means it works in Last in First Out order. That is the last element added to the stack is the first element of removal. So there are four basic operations that can be performed on the stack:

**Isempty:**This operation sees whether the stack is empty.**Push**: This operation adds a new item to stack.**Pop:**This operation removes an item from the stack item added most recently.**Top:**This operation returns item that was added to stack most recently.

The following figure is an example of the stack where insertion into stack and removal from a stack of the item takes place from the top of the stack and nowhere else.

**Stack overflow**

The condition resulting from trying to push an element onto a full stack.

**Stack underflow**

The condition resulting from trying to pop an empty stack.

Here we have shown some push and pop operations on the stack. Suppose initially stack is empty then we added F, A, M, R, N. Then pop two times and push N, H, B, T, K, O, P.

**Implementation of Stack: **

It can be implemented using an array or linked list both.

Following given code is depicts how the stack is implemented in C++ using Class. Here have defined one class named as a stack in which created an array named as a stick with dynamic size and two main functions push and pop.

Stack Overflow: When top>=size-1

Stack underflow: When top <0

**Related Articles:-**

Here are some articles that are related to the data structures and algorithms C++ which will help you to get the more detail about the Algorithms C++ and the data structures and algorithms so kindly go through the link that is given below. if you like the article data structures and algorithms C++ then give us your valuable comment.