Skip to main content

Posts

Showing posts from 2021

Creating multiple stacks using vector with templates in C++

   Problem statement is to create a set of stacks that acts like a pile of plates. Pile of plates can withstand certain height. When it can't withstand we have to create a new set. Implement similar behavior in the code for stack. Use template to implement type independent stack. #include <iostream> #include "vector" #include "string" using namespace std; template<typename a> class setofStacks{     vector<a> setofStacks;     int sizeofEachStack;   public:     void push(a value);     a pop();     a popAt(int index); }; template<typename a> void setofStacks<a>::push(a value){     setofStacks.push_back(value); } template<typename a> a setofStacks<a>::pop(){     a val = setofStacks.back();     setofStacks.pop_back();     return val; } template<typename a> a setofStacks<a>::popAt(int inde...

Partitioning a linked list around a value X.

 /******************************************************************************                               Online C++ Compiler.                Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> using namespace std; struct LinkedlistNode{     LinkedlistNode *next;     int val; }; int main() {     LinkedlistNode *n1 = new LinkedlistNode();     n1->val = 3;     LinkedlistNode *n2 = new LinkedlistNode();     n2->val = 5;     n1->next = n2;     LinkedlistNode *n3...

String builder

 Following is the code for string builder: /******************************************************************************                               Online C++ Compiler.                Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include "bits/stdc++.h" using namespace std; class stringbuilder{ private:     vector<string> words;     vector<char> complete_string;     string concatenate_string() public:     void insert_words(string s);     string get_string(); }; void stringbuilder::insert_word...