链式线性表

#include<iostream>
using namespace std;
class Node
{
public://数据域加指针域,public下面方便赋值
    int date;
    Node *next;//指向下一个结点
    void printNode();
};
void Node::printNode()
{
    cout<<date<<endl;
}
class List
{
public:
    List();//创建
    ~List();//销毁
    void ClearList();//清空线性表
    bool ListEmpty();//判断是否为空表
    int ListLenght();//求线性表的长度
    bool GetElem(int i,Node *pNode);//
    int LocateElem(Node *pNode);//
    bool PriorElem(Node *pCurrentNode,Node *pPreNode);//访问指定元素前驱
    bool NextElem(Node *pCurrentNode,Node *pNextNode);//访问指定元素后继
    bool ListInsert(int i,Node *pNode);//在第i个位置插入元素
    bool ListDelete(int i,Node *pNode);//删除第i个位置的元素
    void ListTraverse();//遍历线性表
    bool ListInsertHead(Node *pNode);//从头插入
    bool ListInsertTail(Node *pNode);//从尾插入
private:
    Node *m_pList;
    //int m_iSize;//线性表大小 链表与顺序表的一个区别
    int m_iLength;//线性表长度
};
List::List()
{
    m_pList=new Node;
    m_pList->date=0;
    m_pList->next=NULL;
    m_iLength=0;//头结点不算在内
}
List::~List() //头结点也将销毁
{
    ClearList();
    delete m_pList;
    m_pList=NULL;
}
void List::ClearList() //保留了头结点
{
    Node *currentNode=m_pList->next;
    while(currentNode!=NULL)
    {
        Node *temp = currentNode->next;
        delete currentNode;
        currentNode = temp;
    }
    m_pList->next=NULL;
}
bool List::ListEmpty()  //同顺序表
{
    if(m_iLength==0)
    {
        return true;
    }
    else
    {
        return false;
    }
    //return m_iLength==0?true:false;
}
int List::ListLenght() //同顺序表
{
    return m_iLength;
}
bool List::ListInsertHead(Node *pNode)
{
    Node *temp = m_pList->next;
    Node *newNode=new Node;
    if(newNode==NULL)
    {
        return false;
    }
    newNode->date = pNode->date;
    m_pList->next = newNode;
    newNode->next = temp;
    m_iLength++;
    return true;
}
bool List::ListInsertTail(Node *pNode)
{
    Node *currentNode = m_pList;
    while(currentNode->next!=NULL)
    {
        currentNode=currentNode->next;
    }
    Node *newNode=new Node;
    if(newNode==NULL)
    {
        return false;
    }
    newNode->date = pNode->date;
    newNode->next = NULL;
    currentNode->next = newNode;
    m_iLength++;

    return true;
}
bool List::GetElem(int i,Node *pNode)
{
    if(i<0||i>=m_iLength)
    {
        return false;
    }
    Node *currentNode = m_pList;//找到头结点
    Node *currentNodeBefore=NULL;
    for(int k=0;k<=i;k++)//与插入不同k<=i;//通过for循环找到第i个结点
    {
        currentNodeBefore=currentNode;
        currentNode=currentNode->next;
    }
    pNode->date = currentNode->date;
    return true;
}
int List::LocateElem(Node *pNode)
{
    Node *currentNode=m_pList;
    int count=0;
    while(currentNode->next!=NULL)
    {
        currentNode=currentNode->next;
        if(currentNode->date==pNode->date)
        {
            return count;
        }
        count++;
    }
    return -1;
}
bool List::PriorElem(Node *pCurrentNode,Node *pPreNode)
{
    Node *currentNode=m_pList;
    Node *tempNode=NULL;
    while(currentNode->next!=NULL)
    {
        tempNode=currentNode;
        currentNode=currentNode->next;
        if(currentNode->date==pCurrentNode->date)
        {
            if(tempNode==m_pList)
            {
                return false;
            }
            pPreNode->date=tempNode->date;
            return true;
        }
    }
    return false;
}
bool List::NextElem(Node *pCurrentNode,Node *pNextNode)
{
    Node *currentNode=m_pList;
    while(currentNode->next!=NULL)
    {
        currentNode=currentNode->next;
        if(currentNode->date==pCurrentNode->date)
        {
            if(currentNode->next==NULL)//判断是否是最后一个结点
            {
                return false;
            }
            pNextNode->date=currentNode->next->date;
            return true;
        }
    }
    return false;

}
void List::ListTraverse()
{
    Node *currentNode=m_pList;
    while(currentNode->next!=NULL)
    {
        currentNode=currentNode->next;
        currentNode->printNode();
    }
}
bool List::ListInsert(int i,Node *pNode)
{
    if(i<0||i>m_iLength)
    {
        return false;
    }
    Node *currentNode = m_pList;
    for(int k=0;k<i;k++)
    {
        currentNode = currentNode->next;
    }
    Node *newNode=new Node;
    if(newNode==NULL)
    {
        return false;
    }
    newNode->date=pNode->date;
    pNode->next=currentNode->next;
    currentNode->next=newNode;

    return true;
}
bool List::ListDelete(int i,Node *pNode)
{
    if(i<0||i>=m_iLength)//判断i是否合法
    {
        return false;
    }
    Node *currentNode = m_pList;//找到头结点
    Node *currentNodeBefore=NULL;//找到当前结点的上一个结点。
    for(int k=0;k<=i;k++)//与插入不同k<=i;//找到第i个结点
    {
        currentNodeBefore = currentNode;
        currentNode = currentNode->next;
    }
    currentNodeBefore->next = currentNode->next;
    pNode->date = currentNode->date;//删除之前的取值
    delete currentNode;
    currentNode = NULL;
    m_iLength--;

    return true;
}
int main()
{
    Node node1;
    node1.date=2;
    Node node2;
    node2.date=4;
    Node node3;
    node3.date=6;
    Node node4;
    node4.date=8;
    Node node5;
    node5.date=10;

    List *pList=new List();
    pList->ListInsertTail(&node1);
    pList->ListInsertTail(&node2);
    pList->ListInsertTail(&node3);
    pList->ListInsertTail(&node4);

    pList->ListTraverse();

    delete pList;
    pList=NULL;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值