Found 7401 Articles for C++

C++ Program to Implement Adjacency Matrix

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

12K+ Views

The adjacency matrix of a graph is a square matrix of size V x V. The V is the number of vertices of the graph G. In this matrix in each side V vertices are marked. If the graph has some edges from i to j vertices, then in the adjacency matrix at ith row and jth column it will be 1 (or some non-zero value for weighted graph), otherwise that place will hold 0.The complexity of Adjacency Matrix representation:The adjacency matrix representation takes O(V2) amount of space while it is computed. When graph has maximum number of edges and ... Read More

C++ Program to Implement Adjacency List

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

10K+ Views

The adjacency list representation of a graph is linked list representation. In this representation we have an array of lists The array size is V. Here V is the number of vertices. In other words, we can say that we have an array to store V number of different lists. If a list header is vertex u, then it signifies that it will hold all of the adjacent vertices of u.The complexity of Adjacency List representationThis representation takes O(V+2E) for undirected graph, and O(V+E) for directed graph. If the number of edges are increased, then the required space will also ... Read More

C++ Program to Implement Stack

Nitya Raut
Updated on 30-Jul-2019 22:30:25

8K+ Views

In this program we will see how to implement stack using C++. A stack is an abstract data structure that contains a collection of elements. Stack implements the LIFO mechanism i.e. the element that is pushed at the end is popped out first. Some of the principle operations in the stack are −Push - This adds a data value to the top of the stack.Pop - This removes the data value on top of the stackPeek - This returns the top data value of the stackA program that implements a stack using array is given as follows.Input: Push elements 11, ... Read More

C++ Program to Evaluate an Expression using Stacks

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

3K+ Views

For solving mathematical expression, we need prefix or postfix form. After converting infix to postfix, we need postfix evaluation algorithm to find the correct answer.Here also we have to use the stack data structure to solve the postfix expressions.From the postfix expression, when some operands are found, pushed them in the stack. When some operator is found, two items are popped from stack and then operation is performed in correct sequence. After that the result is also pushed in the stack for future use. After completing the whole expression, the final result is also stored in the stack top.Input: Postfix ... Read More

C++ program to Convert a Decimal Number to Binary Number using Stacks

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

3K+ Views

In this problem, we will see how to convert a decimal number to binary numbers using stacks. As we know that the decimal numbers can be converted using binary after dividing it by 2 and taking the remainder. We take the remainder from last to first, so we can easily use the stack data structure to do that.Input: Decimal number 13 Output: Binary number 1101AlgorithmStep 1: Take a number in decimal Step 2: while the number is greater than 0: Step 2.1: Push the remainder after dividing the number by 2 into stack. Step 2.2: set the number as number ... Read More

C++ Program to Check for balanced paranthesis by using Stacks

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

8K+ Views

Here we will discuss how to check the balanced brackets using stacks. We not only check the opening and closing brackets but also check the ordering of brackets. For an example we can say that the expression "[{} () {()}]" it is correct, but "{[}]" it is not correct.Input: Some expression with brackets "{()}[]" Output: They are balancedAlgorithmStep 1: Define a stack to hold brackets Step 2: Traverse the expression from left to right Step 2.1: If the character is opening bracket (, or { or [, then push it into stack Step 2.2: If the character is closing bracket ... Read More

Replace substring with another substring C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

2K+ Views

Here we will see how to replace substring with another substring. It replaces the portion of the string that begins at character pos and spans len characters.The structure of the replace function is like below:string& replace (size_t pos,  size_t len,  const string& str,  size_t subpos,  size_t sublen);The parameters are pos: It is an insertion point, str : It is a string object, len : It contains information about number of characters to erase.AlgorithmStep 1: Get the main string, and the string which will be replaced. And the match string Step 2: While the match string is present in the main string: Step 2.1: Replace it with the given ... Read More

Replace part of a string with another string in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

5K+ Views

Here we will see how to replace a part of a string by another string in C++. In C++ the replacing is very easy. There is a function called string.replace(). This replace function replaces only the first occurrence of the match. To do it for all we have used loop. This replace function takes the index from where it will replace, it takes the length of the string, and the string which will be placed in the place of matched string.Input: A string "Hello...Here all Hello will be replaced", and another string to replace "ABCDE" Output: "ABCDE...Here all ABCDE will ... Read More

Remove Trailing Zeros from string in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

3K+ Views

IN this program we will see how to remove the trailing zeros from a string in C++. Sometimes some string may contain trailing zeros like "00023054". After executing this program, it will return "23054" only. The initial zeros are removed.Input: A string with trailing zeros “000023500124” Output: “23500124”AlgorithmStep 1: Get the string Step 2: Count number of trailing zeros n Step 3: Remove n characters from the beginning Step 4: return remaining string.Example Code Live Demo#include using namespace std; main() {    string my_str = "000023500124";    int num = 0;    cout

Remove spaces from std::string in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

14K+ Views

In this program we will see how to remove the spaces from a std::string in C++. To remove this we will use the remove() function. With this remove() function it takes the beginning and end of the iterator, then takes the third argument that will be deleted from that iterator object.Input: A string "This is C++ Programming Language" Output: "ThisisC++ProgrammingLanguage"AlgorithmStep 1: Get the string Step 2: Remove spaces from the given string using remove() function. Step 3: Return string.Example Code Live Demo#include #include using namespace std; main() {    string my_str = "This is C++ Programming Language";    cout

Advertisements