数据结构与算法_实验二单链表

实验名称

实验二 单链表

实验目的

理解单链表的定义掌握单链表的检索、插入、删除等算法的实现;

实验任务

实现单链表完成线性表的基本操作:

初始化线性表、清空线性表、求线性表长度、检查线性表是否为空、遍历线性表、从线性表中查找元素、从线性表中查找与给定元素值相同的元素在线性表中的位置、插入元素、删除元素。

实验内容

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)

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

激稳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值