链式线性表

来源:互联网 发布:idea打印不出sql 编辑:程序博客网 时间:2024/06/01 22:37
#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;}

原创粉丝点击