天天看點

m

// Linklist.h: interface for the Linklist class.
//
//

#if !defined(AFX_LINKLIST_H__4E62685D_4A8F_4C66_BAE4_54E8354E8F01__INCLUDED_)
#define AFX_LINKLIST_H__4E62685D_4A8F_4C66_BAE4_54E8354E8F01__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Linklist  
{
private:
  typedef struct Node{
    Node *prev,*next;
    char data;
    Node(int t):data(t),prev(NULL),next(NULL){}
  }node;
  node* head;
  node* cur;
  int len;
public:
  Linklist();
  virtual ~Linklist();
  void checkEmpty();
  bool empty() const ;
  void pop_back();
  void pop_front();
  char back();
  char front();
  int size();
  void push_front(char x);
  void push_back(char x);
};

#endif // !defined(AFX_LINKLIST_H__4E62685D_4A8F_4C66_BAE4_54E8354E8F01__INCLUDED_)      
<pre name="code" class="cpp">// Linklist.cpp: implementation of the Linklist class.
//
//

#include "stdafx.h"
#include "G.h"
#include "Linklist.h"
#include "iostream"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

 Linklist::Linklist()
{
    cur=head=new node(-1);
    len=0;
}

Linklist::~Linklist()
{

}

char Linklist::back()
{
    checkEmpty();
    return cur->data;
}

char Linklist::front()
{
    checkEmpty();
    return head->next->data;
}


void Linklist::checkEmpty()
{
    if(empty())
    {
        std::cout << "error" ;
    }
}

void Linklist::pop_back(){
    checkEmpty();
    cur=cur->prev;
    delete cur->next;
    cur->next=NULL;
    --len;
}

void Linklist::pop_front()
{
    checkEmpty();
    node* tmp=head->next;
    head->next=tmp->next;
    tmp->prev=head;
    delete tmp;
    --len;
}

void Linklist::push_front(char x){
    node* newNode = new node(x);
    head->next=newNode;
    newNode->prev=head;
    ++len;
}

void Linklist::push_back(char x)
{
    node* newNode=new node(x);
    newNode->prev=cur;
    cur->next=newNode;
    cur=newNode;
    ++len;
}

bool Linklist::empty() const
{
    return len==0;
}

int Linklist::size(){
    return len;
}      
void CGView::OnDraw(CDC* pDC)
{
  CGDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  // TODO: add draw code for native data here
  Midpoint_Line(100,100,500,500,RGB(255,0,0),pDC);

  Linklist l;
  l.push_back('1');
  l.push_back('2');
  l.push_back('3');
  pDC->TextOut(50,40,_T(l.back()));
  pDC->TextOut(50,80,_T(l.front()));

  l.push_front('0');
  pDC->TextOut(50,120,_T(l.front()));

  l.pop_back();
  pDC->TextOut(50,160,_T(l.back()));
}      
// 畫線函數
void CGView::Midpoint_Line(int x0,int y0,int x1,int y1,COLORREF color,CDC * pDC){
  int a,b,d1,d2,d,x,y;
  a=y0-y1,b=x1-x0,d=2*a+b;
  d1=2*a,d2=2*(a+b);
  x=x0,y=y0;
  pDC->SetPixel(x,y,color);
  while(x<x1){
    if(d<0) { x++,y++,d+=d2; }
    else { x++,d+=d1; }
    pDC->SetPixel(x,y,color);
  }
}