3.DataStructure Cp2 T1 Singly-Linked Node
来源:互联网 发布:手机记日记什么软件好 编辑:程序博客网 时间:2024/06/06 02:54
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 oncetemplate <class type>class list{public://virtual function shall be put into 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>//<type>{private: //part I struct node//这个struct实际上是个class,注意构造函数 { type data; node*next; node() :next(NULL){}; node(const type& x, node*m) { data = x; next = m;/*修改完毕*/ } ~node() {};//我就说不用析构函数 }; //partII node*head, *tail; int currentlength; //partIII 不写成内置类编译怎么都不通过 node* move(int i)const { if (i<0 || i>currentlength) throw OutOfBound(); //at first,this hacebeen ignorant node*tmp = head; while (i>0)/*check*/ { tmp = tmp->next; i--; }//应该看看是不是移动到第i个节点了,有个问题,head是第0还是第一? 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-1void 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; }}
阅读全文
0 0
- 3.DataStructure Cp2 T1 Singly-Linked Node
- 2. DataStructure Cp2 Doubly-Linked Node
- LinkedList---Delete Node in the Middle of Singly Linked List
- #372 Delete Node in the Middle of Singly Linked List
- Delete Node in the Middle of Singly Linked List
- 单链表(Singly Linked List)
- 【DataStructure】Linked Data Structures
- Ch2-2: return the nth to the last node data of a singly linked list
- Ch2-3: remove the middle node in a singly linked list
- LintCode 372: Delete Node in the Middle of Singly Linked List
- Singly Linked List(链表)
- Linked Lists Part1-Singly Linked Lists
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- Linked List/Singly-Linked List/Doubly-Linked List
- Lesson 15: Singly linked lists (Printable Version)
- Libevent 分解之数据结构 Singly-linked List
- Queue学习之Singly-Linked List
- Reverse singly linked list using Java
- centos7 安装rocketmq
- 机器学习第十六课part2(前向算法,后向算法)
- 错误:Unable to add window -- token null is not for an application
- Android studio2.3 CMake新建工程的日志打印
- jquery 中添加元素
- 3.DataStructure Cp2 T1 Singly-Linked Node
- opencv之采集摄像头数据
- hibernate修改and删除
- Ionic 介绍
- netty+zk实现简单的rpc调用(基于传输层)
- 《剑指offer》刷题笔记系列综述
- 文章标题关于mysql安装出错问题
- python 深浅拷贝
- 如何使用U盘重装win7系统