February 3rd Tuesday 2009 (二月 三日 火曜日)

本文介绍了一个使用C++模板实现的栈数据结构。该栈通过链表的方式进行元素的存储和管理,并提供了基本的栈操作如压栈(push)和出栈(pop)。文章通过一个简单的字符栈实例展示了如何使用这个自定义栈。

 //stack.cpp
#include <iostream>

using namespace std;

template <class T> class Stack_Frame {
private:
    Stack_Frame *next;
    T content;
public:
    Stack_Frame() {
        next = NULL;
    }

    Stack_Frame(const T& obj) {
        content = obj;
        this->next = NULL;
    }

    T getContent() { return content; }
    void setContent(T& t) { content = t; }

    Stack_Frame *getNext() { return next; }
    void setNext(Stack_Frame *p) { next = p; }
};

template <class U> class Stack{
    private:
        Stack_Frame<U> *top;
    public:
        Stack() {
            top = NULL;
        }

        ~Stack() {
            Stack_Frame<U> *p = NULL;
            Stack_Frame<U> *q = NULL;

            if (top) {
                for (p = top; p; ) {
                    q = p;
                    p = p->getNext();
                    delete q;
                }
            }

            top = NULL;
        }

        void push(U obj) {
            Stack_Frame<U>* pframe = new Stack_Frame<U>(obj);
            pframe->setNext(top);
            top = pframe;
        }

        void pop(U *p) {
            Stack_Frame<U>* pframe = NULL;

            if (top) {
                pframe = top;
                top = top->getNext();

                (*p) = pframe->getContent();
            }
        }
};

int main() {
    char ch;
    Stack<char> cstack;

    for (ch = 'a'; ch <= 'z'; ch++) {
        cstack.push(ch);
    }

    ch = 0;
    for (int i = 0; i < 26; i++) {
        cstack.pop(&ch);
        cout<<ch<<" ";
    }
    cout<<endl;

    return 0;
}

/* * fn CMM_RET oal_sys_setTZ(TIME_OBJ *pNewObj, const struct tm *pDstStart, const struct tm *pDstEnd, const char *pTimeZone) * brief Set system timezone environment variable * * param[in] pNewObj - The time obj. * param[in] pDstStart - DaylightSavings start time. * param[in] pDstend - DaylightSavings end time. * param[in] pTimeZone - The local time zone. * * return CMM_RET */ CMM_RET oal_sys_setTZ(DEV2_TIME_OBJ *pNewObj, const struct tm *pDstStart, const struct tm *pDstEnd, const char *pTimeZone) { struct timeval tv; struct timeval *setTv = NULL; struct timezone tz; int tzHour = 0, tzMin = 0; #if (defined(__GLIBC__) && !defined(__UCLIBC__)) const char* mon_names[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; const char* week_counts[][2] = { {"",">=1"}, /* 1st*/ {"",">=8"}, /* 2nd */ {"",">=15"}, /* 3rd */ {"",">=22"}, /* 4th */ {"last",""} /* last */ }; const char* wday_names[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; int startWeekCount = 0, endWeekCount = 0; char tzInfo[BUFLEN_512] = {0}; char *pTmp = tzInfo; FILE *fp = NULL; if (pNewObj->daylightSavingsUsed) { /* set the week count as the UI wants */ startWeekCount = pNewObj->X_TP_DaylightSavingsStartWeekCount; endWeekCount = pNewObj->X_TP_DaylightSavingsEndWeekCount; pTmp += sprintf(pTmp, "Rule MYR minimum maximum - %s %s%s%s %02d:0:0 1:00 D\n", mon_names[pDstStart->tm_mon], week_counts[startWeekCount-1][0], wday_names[pDstStart->tm_wday], week_counts[startWeekCount-1][1], pDstStart->tm_hour); pTmp += sprintf(pTmp, "Rule MYR minimum maximum - %s %s%s%s %02d:0:0 0 S\n", mon_names[pDstEnd->tm_mon], week_counts[endWeekCount-1][0], wday_names[pDstEnd->tm_wday], week_counts[endWeekCount-1][1], pDstEnd->tm_hour); pTmp += sprintf(pTmp, "Zone localtime %s MYR C%%sT\n", pTimeZone); } else { pTmp += sprintf(pTmp, "Zone localtime %s - CST\n", pTimeZone); } fp = fopen(__GLIBC_TZ_FILE_PATH__, "w+"); if (fp == NULL) { CMM_ERR("Open TZ file error!"); return CMM_ERROR; } if (fwrite(tzInfo, strlen(tzInfo), 1, fp) < 0) { CMM_ERR("Write TZ file error!"); fclose(fp); return CMM_ERROR; } fclose(fp); /* create /var/tmp/localtime */ util_execSystem(__FUNCTION__, "zic -d /var/tmp %s", __GLIBC_TZ_FILE_PATH__); #else int startWeekCount = 0, endWeekCount = 0; char tzInfo[BUFLEN_128] = {0}; char tmpTimeZone[BUFLEN_128] = {0}; char *pTmp = tzInfo; FILE *fp = NULL; /* Get the entire timezone */ cstr_strncpy(tmpTimeZone, pTimeZone, BUFLEN_128); tmpTimeZone[0] ^= 6; /* Change the leading symbol, &#39;+&#39; -> &#39;-&#39; or &#39;-&#39; -> &#39;+&#39; */ pTmp += sprintf(pTmp, "GMT%s", tmpTimeZone); /* DaylightSavings is enable */ if (pNewObj->daylightSavingsUsed) { /* set the week count as the UI wants */ startWeekCount = pNewObj->X_TP_DaylightSavingsStartWeekCount; endWeekCount = pNewObj->X_TP_DaylightSavingsEndWeekCount; /* Get the dst info, like "DST,Mmon.cnt.wday/hh,Mmon.cnt.wday/hh */ pTmp += sprintf(pTmp, "DST,M%d.%d.%d/%d,M%d.%d.%d/%d", \ pDstStart->tm_mon + 1, startWeekCount, pDstStart->tm_wday, pDstStart->tm_hour,\ pDstEnd->tm_mon + 1, endWeekCount, pDstEnd->tm_wday, pDstEnd->tm_hour); } *pTmp++ = &#39;\n&#39;; *pTmp = &#39;\0&#39;; /* Write the tzInfo to the file: /etc/TZ */ fp = fopen(__UCLIBC_TZ_FILE_PATH__, "w+"); if (fp == NULL) { CMM_ERR("Open TZ file error!"); return CMM_ERROR; } if (fwrite(tzInfo, strlen(tzInfo), 1, fp) < 0) { CMM_ERR("Write TZ file error!"); fclose(fp); return CMM_ERROR; } fclose(fp); #endif /* liuyuxuan add for parental control V2 */ memset(&tv, 0, sizeof(struct timeval)); memset(&tz, 0, sizeof(struct timezone)); gettimeofday(&tv, &tz); sscanf(pTimeZone, "%d:%d", &tzHour, &tzMin); if(tzMin > 0 && tzHour < 0) { tzMin = -tzMin; } tz.tz_minuteswest = -(MINUTES * tzHour + tzMin); settimeofday(setTv, &tz); /* liuyuxuan add end */ return CMM_OK; } 是在哪里设定夏令时
10-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值