NaN Quantitavity – Quant Trading, Statistical Learning, Coding and Brainstorming

2012-11-02, 4:49 PM, Friday

Reverse a C-style String

Filed under: C++ — weekendsunny @ 4:49 PM
// Reverse a C-Style String
#include <iostream>
using namespace std;

void reverse(char *str) {
    char * end = str;
    char tmp;
    if (str) {
        while (*end) {
            ++end;
        }
        --end;
        while (str < end) {
            tmp = *str;
            *str++ = *end;
            *end-- = tmp;
        }
    }
} 

int main(){
    char* str = new char[6];
    strcpy(str, "Hello");
    // char* str = "Hello" doesn't work. Cz here str is const type.
    cout<<str<<endl;
    reverse(str);
    cout<<str<<endl;
    delete[] str;

    return 0;
}
//Highlighted at http://tohtml.com/cpp/
//Bred 3 + C++

STL Stack and Queue

Filed under: C++ — weekendsunny @ 11:37 AM
// Read words and print them in reverse order;
// Using STL Stack or Queue;
// http://www.cplusplus.com/reference/stl/stack/
//Highlighted at http://tohtml.com/cpp/

#include <iostream>
#include <stack>
#include <queue>
#include <string>
using namespace std;

int main() {

    // stack<string> allWords;
    queue<string> allWords;
    string word;

    while (cin >> word) {
        allWords.push(word);
    }

    cout << "Number of words = " << allWords.size() << endl;

    while (!allWords.empty()) {
       // cout << allWords.top() << endl;
       cout << allWords.front() << endl;
       allWords.pop();
    }
    return 0;
}

2012-11-01, 9:58 PM, Thursday

Stack/Queue Implementation in Cpp

Filed under: C++ — weekendsunny @ 9:58 PM
#include <iostream>
#include <string>
using namespace std;
typedef int Object;

class Node{
    public:
        Object data;
        Node*  next;

        Node (Object item){
            data = item;
            next = NULL;
        }
        ~Node(){ delete next;}
};

class Stack{
public:
    Node* head;

    Object pop(){
        if (head != NULL){
            Object item = head->data;
            head = head->next;
            return item;
        }
        return NULL;
    }

    void push (Object item){
        Node* t = new Node (item);
        t->next = head;
        head     = t;
    }

};

class Queue{
public:
    Node* head;
    Node* tail;

    void push (Object item){
        Node* t = new Node (item);
        if(tail!=NULL){
            tail->next = t;
            tail    = t;
        }
        else{
            head=tail=t;
        }
    }

    Object pop(){
        if (head != NULL){
            Object item = head->data;
            head = head->next;
            return item;
        }
        return NULL;
    }
};

int main()
{
    Stack* st=new Stack();
    Queue* qu=new Queue();
    Object temp;
    while (cin >> temp){
        st->push(temp);
        qu->push(temp);
    }

    cout<<"Stake (FILO): ";
    while(st->head !=NULL){
        cout<< st->pop();
    }
    cout<<endl;

    cout<<"Queue (FIFO): ";
    while(qu->head !=NULL){
        cout<< qu->pop();
    }
    cout<<endl;

    return 0;
}
//Highlighted at http://tohtml.com/cpp/
//Bred 3 + C++

Create a free website or blog at WordPress.com.

Design a site like this with WordPress.com
Get started