Archive for category C++ code
Doubly Linked List (C++)
This code is an implementation and test driver of a doubly linked list. This list is pointer based and uses dynamic memory.
Sorted Doubly Linked List with Insertion and Deletion
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Dllist
{
private:
typedef struct Node
{
string name;
Node* next;
Node* prev;
};
Node* head;
Node* last;
public:
Dllist()
{
head = NULL;
last = NULL;
}
bool empty() const { return head==NULL; }
friend ostream& operator<<(ostream& ,const Dllist& );
void Insert(const string& );
void Remove(const string& );
};
void Dllist::Insert(const string& s)
{
// Insertion into an Empty List.
if(empty())
{
Node* temp = new Node;
head = temp;
last = temp;
temp->prev = NULL;
temp->next = NULL;
temp->name = s;
}
else
{
Node* curr;
curr = head;
while( s>curr->name && curr->next != last->next) curr = curr->next;
if(curr == head)
{
Node* temp = new Node;
temp->name = s;
temp->prev = curr;
temp->next = NULL;
head->next = temp;
last = temp;
// cout<<" Inserted "<<s<<" After "<<curr->name<<endl;
}
else
{
if(curr == last && s>last->name)
{
last->next = new Node;
(last->next)->prev = last;
last = last->next;
last->next = NULL;
last->name = s;
// cout<<" Added "<<s<<" at the end "<<endl;
}
else
{
Node* temp = new Node;
temp->name = s;
temp->next = curr;
(curr->prev)->next = temp;
temp->prev = curr->prev;
curr->prev = temp;
// cout<<" Inserted "<<s<<" Before "<<curr->name<<endl;
}
}
}
}
ostream& operator<<(ostream& ostr, const Dllist& dl )
{
if(dl.empty()) ostr<<" The list is empty. "<<endl;
else
{
Dllist::Node* curr;
for(curr = dl.head; curr != dl.last->next; curr=curr->next)
ostr<<curr->name<<" ";
ostr<<endl;
ostr<<endl;
return ostr;
}
}
void Dllist::Remove(const string& s)
{
bool found = false;
if(empty())
{
cout<<" This is an empty list! "<<endl;
return;
}
else
{
Node* curr;
for(curr = head; curr != last->next; curr = curr->next)
{
if(curr->name == s)
{
found = true;
break;
}
}
if(found == false)
{
cout<<" The list does not contain specified Node"<<endl;
return;
}
else
{
// Curr points to the node to be removed.
if (curr == head && found)
{
if(curr->next != NULL)
{
head = curr->next;
delete curr;
return;
}
else
{
delete curr;
head = NULL;
last = NULL;
return;
}
}
if (curr == last && found)
{
last = curr->prev;
delete curr;
return;
}
(curr->prev)->next = curr->next;
(curr->next)->prev = curr->prev;
delete curr;
}
}
}
int main()
{
Dllist d1;
int ch;
string temp;
while(1)
{
cout<<endl;
cout<<" Doubly Linked List Operations "<<endl;
cout<<" ------------------------------"<<endl;
cout<<" 1. Insertion "<<endl;
cout<<" 2. Deletion "<<endl;
cout<<" 3. Display "<<endl;
cout<<" 4. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: cout<<" Enter Name to be inserted : ";
cin>>temp;
d1.Insert(temp);
break;
case 2: cout<<" Enter Name to be deleted : ";
cin>>temp;
d1.Remove(temp);
break;
case 3: cout<<" The List contains : ";
cout<<d1;
break;
case 4: system("pause");
return 0;
break;
}
}
Open Text File the Right Way. (C++)
This code is a simple example of how to safely open a text file in C++. Notice the very important check that the file was opened correctly. This covers a range of errors, most commonly file not found.
// Read integers from file and print sum.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main() {
int sum = 0;
string word;
ifstream inFile;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> word) {
cout << word << " ";
}
inFile.close();
return 0;
}
Press any key to Continue…(C++)
this snippet of code is an alternative to system(“pause”) in MS Visual Studio.
/*
* File: SystemPause.cpp
* Author: B Turley
*
* This function can be used as a substitute for 'system("pause")'
* in Microsoft Visual Studio
*
* Created on November 10, 2009, 1:17 AM
*/
#include <iostream>
#include <limits>
using namespace std;
void pause()
{
cout<<"Press any key to Continue...";
cin.sync();
cin.ignore(numeric_limits<streamsize> ::max(), '\n');
}
int main(int argc, char** argv) {
pause();
return (0);
}
String to Int function (C++)
This little piece of code can convert a string of decimal characters into an integer value. If you seek to convert a string into a double, see this post.
/*
* A function to convert a string into an integer
*/
#include <string>
int StringToInt (std::string str)
{
int total = 0;
int length = str.length(); // the length of the string
int x = 1; // this is our multiplier, used to convert each digit into tens, units, etc
for (int i = 1; i < str.length(); i++)
x *= 10; // initialise x correctly
for (int i = 0; i < length; i++)
{ // loop through the string
// 48 is the base value (ASCII)
// multiply it by x to get it into tens, units, etc
total += (static_cast <int> (str[i]) - 48) * x;
x /= 10; // divide x by 10, to reduce the units by one
}
return total; // return the value as an integer
}
/** EXAMPLE USAGE **/
#include <iostream>
int main ()
{
int t = StringToInt("1234") + 300;
std::cout < < t; // should be 1534 (1234 + 300 = 1534)
std::cin.get ();
return EXIT_SUCCESS;
}