3. DataStructure Cp2 T1 Singly-Linked Node
this one is quite similar with doubly-linked linknode
singlelinknode.h
#include"stdafx.h"
#include"targetver.h"
#ifndef singlelinknode_h
#define singlelinknode_h
#include<iostream>
using namespace std;
#pragma once
template <class type>
class list
{
public:
virtual void clear() = 0;
virtual int length()const = 0;
virtual void insert(int i, const type&x) = 0;
virtual void remove(int i) = 0;
virtual int search(const type&x)const = 0;
virtual type visit(int i)const = 0;
virtual void traverse()const = 0;
virtual ~list();
};
class OutOfBound {};
class IllegalSize {};
template <class type>
class singlelinknode :public list<type>
{
private:
struct node
{
type data;
node*next;
node() :next(NULL){};
node(const type& x, node*m)
{
data = x; next = m;
}
~node() {};
};
node*head, *tail;
int currentlength;
node* move(int i)const
{
if (i<0 || i>currentlength)
throw OutOfBound();
node*tmp = head;
while (i>0)
{
tmp = tmp->next;
i--;
}
return tmp;
}
public:
singlelinknode();
~singlelinknode();
void clear();
int length()const;
void insert(int i, const type&x);
void remove(int i);
int search(const type&x)const;
type visit(int i)const;
void traverse()const;
};
#endif
singlelinknode.cpp
#include "stdafx.h"
#include "singlelinknode.h"
template<class type>//不知道写什么?为head tail申请空间,随后再连一起
singlelinknode<type>::singlelinknode()
{
head = new node;
tail = new node;
head->next = tail;/*check*/
currentlength = 0;//忘写了
}
template<class type>
singlelinknode<type>::~singlelinknode()
{
clear();
delete head;
}
template<class type>
void singlelinknode<type>::clear()
{
node*tmp = head->next;
head->next = tail;
while (tmp != tail)//跟课本上不太一样
{
node*p = tmp;
tmp = tmp->next;/*check*/
delete p;
}
currentlength = 0;//注意清零,忘写了
}
template<class type>
int singlelinknode<type>::length() const
{
return currentlength;
}
template<class type>//插到i前面还是i后面? 是i和i-1
void singlelinknode<type>::insert(int i, const type & x)
{
node*p = move(i-1);
node*tmp = new node(x, p->next);
p->next = tmp;
currentlength++;//注意清零,忘写了
}
template<class type>
void singlelinknode<type>::remove(int i)
{
char*tmp = move(i-1);/*diffenence from doubly-linked is
we can only start from the very beginning*/
char*p = tmp->next;
tmp->next = tmp->next->next;/*check*/
delete p;//曾经在这一块有过疑问,
//事实上删除指针会自动启用对应的析构函数
currentlength
}
template<class type>
int singlelinknode<type>::search(const type & x) const
{
node*tmp = head;
int i = 0;
while (tmp != tail && tmp->data != x)
{
tmp = tmp->next;
i++;
}
if (tmp == tail)
{
cout << "none exit\t\n";
return -1;
}
else return i;//same question,should i start from 0?
return 0;
}
template<class type>
type singlelinknode<type>::visit(int i) const
{
if (i<0 || i>currentlength)throw OutOfBound();//at first,this hacebeen ignorant
node*tmp = head;
int n = 0;
while (n < i)
{
tmp = tmp->next;
n++;
}
return tmp->data;//我也不知道应该是前一个还是后一个
}
template<class type>
void singlelinknode<type>::traverse() const
{
node*tmp = head - next;
while (node != tail)
{
cout << tmp->data << endl;
tmp = tmp->next;
}
}