Just tell me your ideas about it.
LL.h is:
LL.cpp is:Code:struct Node{ int val; Node *next; }; class LL{ public: LL(); void PushFront(int aVal); void PushBack(int aVal); int PopFront(); int PopBack(); int ReadFront() const; int ReadBack() const; bool IsEmpty(); ~LL(); private: Node *back; Node *front; };
stdafx.h is:Code:#include "LL.h" #include "stdafx.h" LL::LL() { front = back = new Node; } void LL::PushFront(int aVal) { front->next = new Node; front = front->next; front->val = aVal; } void LL::PushBack(int aVal) { Node *t; back->val = aVal; t = new Node; t->next = back; back = t; } int LL::PopFront() { int temp = front->val; delete front; Node* it = back; while(it->next != front) it = it->next; front = it; return temp; } int LL::PopBack() { int temp = back->next->val; Node *it = back->next; delete back; back = it; return temp; } int LL::ReadFront() const { return front->val; } int LL::ReadBack() const { return back->val; } bool LL::IsEmpty() { return (back == front); } LL::~LL() { while(back != front) { Node *temp = back->next; delete back; back = temp; } delete front; }
And there is test program that runs only on Windows.Code:// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include <stdio.h> #include <windows.h> #include <list> #include <iostream> #include "LL.h" using namespace std;
Code:#include "stdafx.h" int main(int argc, char* argv[]) { using namespace std; LL ll; list <int> v; LARGE_INTEGER fp, fp2; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) ll.PushFront(i); QueryPerformanceCounter(&fp2 ); cout <<"PushFront: "<< fp2.LowPart - fp.LowPart << endl; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) v.push_front(i); QueryPerformanceCounter(&fp2 ); cout <<"push_front: "<< fp2.LowPart - fp.LowPart << endl; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) ll.PushBack(i); QueryPerformanceCounter(&fp2 ); cout <<"PushBack: "<< fp2.LowPart - fp.LowPart << endl; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) v.push_back(i); QueryPerformanceCounter(&fp2 ); cout <<"push_back: "<< fp2.LowPart - fp.LowPart << endl; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) ll.PopFront(); QueryPerformanceCounter(&fp2 ); cout <<"PopFront: "<< fp2.LowPart - fp.LowPart << endl; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) v.pop_front(); QueryPerformanceCounter(&fp2 ); cout <<"pop_front: "<< fp2.LowPart - fp.LowPart << endl; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) ll.PopBack(); QueryPerformanceCounter(&fp2 ); cout <<"PopBack: "<< fp2.LowPart - fp.LowPart << endl; QueryPerformanceCounter(&fp ); for(int i = 0 ; i < 1000 ; i++) v.pop_back(); QueryPerformanceCounter(&fp2 ); cout <<"pop_back: "<< fp2.LowPart - fp.LowPart << endl; return 0; }


