c++ 实现线性表_数组_链表_顺序存储结构_链式存储结构

`在这里插入代码片`# c++ 实现线性表_数组_链表_顺序存储结构_链式存储结构

## 数组

~~~c++
#include <iostream>
#include "stdlib.h"
using namespace std;
#define MAXSIZE 20 //设置存储空间初始分量
#define OK 1
#define ERROR 0
typedef int ElemType;//ElemType 相当与int
typedef int Status;
class list1
{
public:
    list1();
    //~list1();
    Status GetElem(int i,ElemType &e); //获取
    Status ListInsert(int i,ElemType e);//插入
    Status show();
    Status ListDelete(int i,ElemType &e);//删除位置i的元素,并用e返回值
private:
    ElemType data[MAXSIZE];
    int length;
};
//构造函数用于初始化
list1::list1() {
    length = 0;
}
//获取指定位置元素的值
Status list1::GetElem(int i,ElemType &e) {
    if(length==0||i<1||i>length) return ERROR;
    e=data[i-1];
    return 0;
}
//在数组指定位置插入元素
Status list1::ListInsert(int i,ElemType e) {
    int k;
    if(length==MAXSIZE||i<1||i>length+1) //当数组满或者当要求插入位置小于1或者要求插入位置在数组最后以为后面时
        return ERROR;
    if(i<=length) {
        //将前面位置的元素均向后移动一位
        for(k=length-1;k>=i-1;k--) {
            data[k+1]=data[k];
        }
    }
    data[i-1]=e;
    length++;
    return OK;
}
//删除
Status list1::ListDelete(int i,ElemType &e) {
    int k;
    if(length==0||i<1||i>length) {
        cout<<"ERROR"<<endl;
        return ERROR;
    }
    e = data[i-1];
    if(i<length) for(k=i;k<length;k++) data[k-1]=data[k];
    length--;
    return OK;
}
Status list1::show() {
    if(length == 0) {
        cout<<"data is empty"<<endl;
        return ERROR;
    }
    else {
        cout<<"length is "<<length<<endl;
        cout<<"data is ";
        for(int i=0;i<length;i++) cout<<data[i]<<"  ";
        cout<<endl;
        return OK;
    }
}
int main()
{
    //构造函数用于初始化
    list1 q;
    q.show();
    //1.插入
    for(int i=0;i<=10;i++) q.ListInsert(i,i+1);
    q.show();
    /*q.ListInsert(1,22);
    q.show();*/
    //2.获取
    int e;
    q.GetElem(2,e);//使用了引用的方法
    cout<<"e is "<<e<<endl;
    //3.删除
    q.show();
    q.ListDelete(3,e);
    q.show();
    return 0;
}
~~~

## 静态链表

~~~c++
#include <iostream>
using namespace std;
#define MAXSIZE 1000 //假设链表的最大长度是1000
struct Node
{
    int data;
    int cur; 
};
class list3_oneself
{
public:
    list3_oneself();
    list3_oneself(int a[],int n);
    void show();//打印当前链表
    void show_real();//打印真实的顺序列表
    int length();//获当前链表长度
    void Insert(int n, int x);//在指定位置n前面插入元素x
    void Delete(int n);// 删除第i个节点
    int Get(int n);//获取指定位置的元素
    int Locate(int x);// 查找值为x的元素
private:
    Node StaticLinkList[MAXSIZE];
};
list3_oneself::list3_oneself() {
    for(int i=0;i<MAXSIZE-1;i++) StaticLinkList[i].cur = i+1; 
    StaticLinkList[MAXSIZE-1].cur = 0;
}
list3_oneself::list3_oneself(int a[],int n) {
    for(int i=0;i<MAXSIZE-1;i++) StaticLinkList[i].cur = i+1; 
    for(int i=1;i<n+1;i++) StaticLinkList[i].data = a[i-1];
    StaticLinkList[0].cur = n+1;//头节点存放备用链表第一个节点的下标
    StaticLinkList[MAXSIZE-1].cur = 1;//数组尾节点存放第一个插入元素的下标
    StaticLinkList[n].cur=0;//下一个数据为空则用0表示
}
void list3_oneself::show() {
    int len = length();
    cout<<"StaticLinkList's length is "<<len<<endl;
    cout<<"StaticLinkList is ";
    int k=StaticLinkList[MAXSIZE-1].cur;
    for(int i=1;i<=len;i++) {
        cout<<StaticLinkList[k].data<<"  ";
        k=StaticLinkList[k].cur;
    }
    //cout<<endl<<"cur is "<<StaticLinkList[len].cur<<endl;
    cout<<endl;
}
void list3_oneself::show_real() {
    int len = length();
    cout<<"StaticLinkList's length is "<<len<<endl;
    cout<<"real StaticLinkList is ";
    for(int i=1;i<=len;i++) {
        cout<<StaticLinkList[i].data<<"  ";
    }
    //cout<<endl<<"cur is "<<StaticLinkList[len].cur<<endl;
    cout<<endl;
}
int list3_oneself::length() {
    int k=StaticLinkList[MAXSIZE-1].cur;
    int num=0;
    while(k) {
        k=StaticLinkList[k].cur;
        num++;
    }
    return num;
}
void list3_oneself::Insert(int m,int x) {
    if(m>length()+1 || m<1 || m>MAXSIZE-2) {
        cout<<"overstep the boundary!!!"<<endl;
        exit(1);
    }
    int Cur=StaticLinkList[0].cur;//找到备用链表的第一个节点的下标
    ++StaticLinkList[0].cur;//备用链表的下标+1
    StaticLinkList[Cur].data = x;//插入新节点
    int k=MAXSIZE-1;
    for(int i=1;i<m;i++) { //找到第m个位置之前的位置
        k=StaticLinkList[k].cur;
    }
    StaticLinkList[Cur].cur=StaticLinkList[k].cur;//第m个位置
    StaticLinkList[k].cur=Cur;//指向插入节点的下标
}
void list3_oneself::Delete(int m) {
    if(m>length() || m<1) {
        cout<<"overstep the boundary!!!"<<endl;
        exit(1);
    }
    int k=MAXSIZE-1;
    for(int i=1;i<m;i++) {
        k=StaticLinkList[k].cur;//找到第m个元素的下标
    }
    int Cur=StaticLinkList[k].cur;//当前元素的下标
    StaticLinkList[k].cur = StaticLinkList[Cur].cur;//前一个元素的下标指向下一个元素
    StaticLinkList[Cur].cur = StaticLinkList[0].cur;//当前位置元素的下一个元素的下标指向原备用链表的第一个元素的位置
    StaticLinkList[0].cur = Cur;//将删除的元素的位置加入备用链表
}
int main()
{
    int arry[30];
    for(int i=0;i<30;i++) arry[i]=i;
    list3_oneself *StaticList = new list3_oneself(arry,30);
    StaticList->show();
    StaticList->Insert(1,100);
    StaticList->show();
    StaticList->Insert(32,101);
    StaticList->show();
    StaticList->show_real();
    StaticList->Delete(1);
    StaticList->show();
    StaticList->show_real();

    /*cout<<"StaticLinkList is ";
    for(int i=0;i<MAXSIZE;i++) {
        if(i%20==0) cout<<endl;
        cout<<StaticList.StaticLinkList[i].data<<"  ";
    }*/
    return 0;
}
~~~

## 链式存储结构

~~~c++
#include <iostream>
using namespace std;
// 单链表的节点
template<class T> //加入模板
struct Node
{
    T data;//数据域
    Node<T> *next;// 指针域,指向后继节点
};
// 单链表的类实现
template<class T>
class LinkList
{
public:
    LinkList();// 无参构造函数,建立只有头节点的空链表
    LinkList(T a[],int n);// 有参构造函数,建立有n个元素的单链表
    ~LinkList();// 析构函数
    int Length();// 求单链表的长度
    T Get(int i);// 查找第i个元素
    int Locate(T x);// 查找值为x的元素
    void Insert(int i, T x);// 在第i个元素处插入x
    T Delete(int i);// 删除第i个节点
    void PrintList();// 遍历各个元素
private:
    Node<T>* first;// 单链表的头节点
};

template<class T>
inline LinkList<T>::LinkList()
{
    first = new Node<T>;    // 生成头节点
    first->next = NULL;      // 头节点指针域为空
}

// 头插法建立单链表
template<class T>
LinkList<T>::LinkList(T a[], int n)
{
    first = new Node<T>;
    first->next = NULL;        // 初始化一个空链表
    for (int i = 0; i < n; i++)
    {
        Node<T>* S = new Node<T>;
        S->data = a[i];        // 为每个数据元素建立一个节点
        S->next = first->next;
        first->next = S;    // 将节点S插入头节点之后
    }
}
// 尾插法建立单链表
/*template<class T>
LinkList<T>::LinkList(T a[], int n)
{
    first = new Node<T>;// 建立头节点
    first->next = NULL;
    Node<T>* r = first;// 尾指针初始化
    for(int i = 0; i < n; i++)
    {
        Node<T>* S = new Node<T>;
        S->data = a[i];        // 为每个数据元素建立一个节点
        r->next = S;
        r = S;                // 插入节点S,并将尾指针指向S节点
    }
    r->next = NULL;            // 单链表建立完毕之后,将尾指针置空
}*/

template<class T>
LinkList<T>::~LinkList()
{
    while (first != NULL)
    {
        Node<T>* p = first;        // 暂存将被释放节点
        first = first->next;    // 指向下一个节点
        delete p;
    }
}

template<class T>
int LinkList<T>::Length()
{
    int count = 0;                // 计数
    Node<T>* p = first->next;    // 将工作指针指向第一个节点
    while (p != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}

template<class T>
T LinkList<T>::Get(int i)
{
    int count = 0;                // 计数
    Node<T>* p = first->next;    // 将工作指针指向第一个节点
    while (p != NULL)
    {
        count++;
        if (count == i)
            return p->data;
        p = p->next;
    }
    return -1;                    // 越界
}

template<class T>
int LinkList<T>::Locate(T x)
{
    int count = 0;                // 计数
    Node<T>* p = first->next;    // 将工作指针指向第一个节点
    while (p != NULL)
    {
        count++;
        if (p->data == x)
            return count;
        p = p->next;
    }
    return 0;                // 查找失败
}

template<class T>
void LinkList<T>::Insert(int i, T x)
{
    int count = 0;                // 计数
    Node<T>* p = first;            // 将工作指针指向头节点
    while (p != NULL)
    {
        if (count == i - 1)        // 找第i-1个节点
        {
            Node<T>* S = new Node<T>;
            S->data = x;
            S->next = p->next;
            p->next = S;
        }
        p = p->next;
        count++;
    }
    if (p == NULL)
        throw "位置越界";
}

template<class T>
T LinkList<T>::Delete(int i)
{
    int count = 0;                // 计数
    Node<T>* p = first;            // 将工作指针指向头节点
    while (p != NULL)
    {
        if (count == i - 1)
        {
            Node<T>* q = p->next;// 暂存被删节点
            T x = q->data;
            p->next = q->next;
            delete q;
            return x;
        }
        p = p->next;
        count++;
    }
    return -1;
}

template<class T>
void LinkList<T>::PrintList()
{
    Node<T>* p = first->next;    // 将工作指针指向第一个节点
    while (p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
}
int main()
{
    int arry[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    LinkList<int>* linklist = new LinkList<int>(arry, 10);
    cout << linklist->Length() << endl;
    cout << linklist->Get(5) << endl;
    cout << linklist->Locate(6) << endl;
    linklist->Insert(3, 11);
    linklist->Delete(10);
    linklist->PrintList();

    //system("pause");
    return 0;
}
~~~

### 链式存储结构

~~~c++
#include <iostream>
using namespace std;
struct Node
{
    int data;
    Node *next;
};
class list2_oneself
{
public:
    list2_oneself();
    list2_oneself(int a[],int n);
    //~list2_oneself();
    void PrintList();//打印当前链表
    int length();//获当前链表长度
    int Get(int n);//获取指定位置的元素
    int Locate(int x);//查找值为x的元素
    void Insert(int n, int x);//在指定位置插入元素x
    void Delete(int n);//删除第i个节点
private:
    Node *first;
};
list2_oneself::list2_oneself() {
    first = new Node;
    first->next = NULL;
}
//头插法
/*list2_oneself::list2_oneself(int a[],int n) {
    first = new Node;
    first->next = NULL;
    for(int i=0;i<n;i++) {
        Node *s=new Node;
        s->data = a[i];
        s->next = first->next;
        first->next = s;
    }
}*/
//尾插法
list2_oneself::list2_oneself(int a[],int n) {
    first = new Node;
    first->next = NULL;
    Node* r = first;     //尾指针初始化,尾指针指first
    for(int i=0;i<n;i++) 
    {
        Node* S = new Node;
        S->data = a[i];     //为每个数据元素建立一个节点
        r->next = S;        //指针r指向的节点(first)指向s
        r = S;              //将新的节点插入链表,此时r代表s
    }              
    r->next = NULL;         //单链表建立完毕之后,将尾指针置空
}
//打印数据
void list2_oneself::PrintList() {
    Node* p = first->next;    // 将工作指针指向第一个节点
    while(p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
    cout<<endl;
}
//求长度
int list2_oneself::length() {
    int count =0;
    Node* p=first->next;
    while(p!=NULL) {
        count++;
        p=p->next;
    }
    return count;
}
//获得指定位置的数据
int list2_oneself::Get(int n) {
    int count = 0;
    Node* p=first->next;
    while(p!=NULL) {
        count++;
        if(count == n) return p->data;
        p=p->next;
    }
    return -1;
}
//查找值为x的元素
int list2_oneself::Locate(int n) {
    int count =0;
    Node* p=first->next;
    while(p!=NULL) {
        count++;
        if(p->data == n) return count;
        p=p->next;
    }
    return 0;
}
//在指定位置插入元素
void list2_oneself::Insert(int n,int x) {
    int count=0;
    Node* p=first->next;
    while(p!=NULL) {
        count++;
        if(count == n) {
            Node* S = new Node;
            S->data = x;
            S->next = p->next;
            p->next = S;
        }
        p=p->next;
    }
}
//删除第i个节点
void list2_oneself::Delete(int n) {
    int count=0;
    Node* p=first->next;
    while(p!=NULL) {
        //count++;
        if(count == n-1) {
            Node* q=p->next;//要删除的节点
            p->next=q->next;
            delete(q);
        }
        count++;
        p=p->next;
    }
}
int main()
{
    int arry[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    list2_oneself *linklist = new list2_oneself(arry, 10);
    linklist->PrintList();
    cout<<"length is "<<linklist->length()<<endl;
    cout<<"data[5] is "<<linklist->Get(6)<<endl;
    cout<<"5 is on "<<linklist->Locate(5)<<endl;
    linklist->Insert(5,226);
    linklist->PrintList();
    cout<<"length is "<<linklist->length()<<endl;
    cout<<"delete 4"<<endl;
    linklist->Delete(4);
    linklist->PrintList();
    cout<<"length is "<<linklist->length()<<endl;
    return 0;
}
~~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LovG-Sco-Tec

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

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

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

打赏作者

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

抵扣说明:

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

余额充值