实验名称 |
实验二 单链表 |
实验目的 |
理解单链表的定义掌握单链表的检索、插入、删除等算法的实现; |
实验任务 |
实现单链表完成线性表的基本操作: 初始化线性表、清空线性表、求线性表长度、检查线性表是否为空、遍历线性表、从线性表中查找元素、从线性表中查找与给定元素值相同的元素在线性表中的位置、插入元素、删除元素。 |
实验内容 LIST.h #ifndef _LIST_H_ #define _LIST_H_ #include <bits/stdc++.h> template<class T> class List{ public: virtual void clear()=0; virtual bool empty()const=0; virtual int size()const=0; virtual void insert(int i,const T &value)=0; virtual void remove(int i)=0; virtual int search(const T&value)const=0; virtual T visit(int i)const=0; virtual void traverse()const=0; virtual void inverse()=0; virtual ~List(){}; }; class outOfRange{}; class badSize{}; /* class outOfRange : public exception { public: const char* what()const throw() { return "ERROR OUT OF RANGE. \n";} }; class badSize : public exception { public: const char* what()const throw() { return "ERROR BAD SIZE. \n";} };*/ #endif LinkList.h #ifndef _LINKLIST_H_ #define _LINKLIST_H_ #include<stack> #include "List.h" using namespace std; template <class T> class linkList:public List<T>{ private: struct Node{ public: T data; Node* next; Node(const T value , Node* p = NULL){ data = value; next = p; } Node(Node* p = NULL) { next = p; }
}; Node* head; Node* tail; int curLength; Node* getPosition(int i)const;
void traverseRecursive(Node * p); void traverseNonRecursive1(); void traverseNonRecursive2(); void traverseNonRecursive3(); public:
linkList(); ~linkList(); void clear(); bool empty()const{return head->next==NULL;} int size()const; void insert(int i,const T &value); void remove(int i); int search(const T &value)const; T visit(int i)const; void traverse()const; void headCreate(); void tailCreate(); void inverse(); int prior(const T &value)const; linkList* Union(linkList<T> * lb); void outPut(); }; template<class T> linkList<T>::linkList(){ head = tail = new Node(); curLength = 0; } template <class T> linkList<T>::~linkList(){ clear(); delete head; } template<class T> void linkList<T>::clear(){ Node *p,*tmp; p = head->next; while(p != NULL) |
数据结构与算法_实验二单链表
最新推荐文章于 2024-05-20 12:46:49 发布