线性表链式结构的基本实现

以下代码是严蔚敏版数据结构的线性表链式结构的基本实现,同时结合王道复习书中的定义方法。函数名是严格按照严蔚敏版的数据结构来的,欢迎大家指正学习。目前各个函数基本测试完毕,能够正确运行,希望大家学习测试,如有错误请联系我。

1.操作集:link_list.h

#ifndef link_list_h
#define link_list_h
#include <cstdlib>

typedef struct LNode{
    int data;//数据存储部分
    struct LNode *next;//指向下一个结点的指针
}LNode,*LinkList;

void InitList(LinkList &L){
    L = (LNode*)malloc(sizeof(LNode));
    L->data = 0;//L将作为头节点,我希望data里可以存放链表的长度
    L->next = NULL;
    return;
}//初始化函数

void DestoryList(LinkList &L){
    LNode *p;
    if(L == NULL){
        return;
    }
    while(L){
        p = L->next;
        free(L);
        L = p;
    }//在这里我们循环地把这个表释放
    return;
}//链表的销毁就是直接把整个脸表空间都释放,我们需要检查它的每一个结点然后释放

void ClearList(LinkList &L){
    LNode *p,*q;
    p = L->next;
    while(p){
        q = p->next;
        free(p);
        p = q;
    }
    L->next = NULL;
    L->data = 0;
    return;
}

bool ListEmpty(LinkList &L){
    if(L->data == 0)
        return true;
    else
        return false;
}

int ListLength(LinkList L){
    return L->data;
}

void GetElem(LinkList L, int i, int &e){
    int n;
    if(i<=0||i>L->data)//下标是从1开始到长度,所以不在此范围都标定失败
        e = -1;//暂且表示查找失败
    LNode *p = L->next;
    for(n = 1; n <= i; n++){
        p = p->next;
    }
    e = p->data;
    return;
}

int LocateElem(LinkList L, int e){
    int i = 1;
    LNode *p = L->next;
    while(p){
        if(e == p->data)
            return i;
        i++;
        p = p->next;
    }
    return -1;//如果没找到最后返回一个-1
}

void PriorElem(LinkList L, int cur_e, int &pre_e){
    LNode *p,*q;
    q = L;
    p = L->next;
    while(p){
        if(p->data == cur_e&&q!=L){//q不能等于L
            pre_e = q->data;
            return;
        }
        p = p->next;
        q = q->next;
    }//这个循环,q是从L开始,p是从L->next开始,第一个结点是没有前驱的,如果第一次就等于,则必须报错
    pre_e = -1;//暂且将失败情况定为-1
    return;
}

void NextElem(LinkList L, int cur_e, int &next_e){
    LNode *p;
    p = L->next;
    while(p->next){
        if(p->data == cur_e){
            next_e = p->next->data;
            return;
        }
        p = p->next;
    }
    next_e = -1;
}

void ListInsert(LinkList &L, int i ,int e){
    int n;
    LNode *p = L;
    if(i<1||i>L->data+1)
        return;
    for(n = 0; n < i-1; n++){
        p = p->next;
    }
    LNode *s = (LNode*)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    L->data++;
    return;
}//这里我理解错了一个东西

void ListDelete(LinkList &L, int i){
    int n;
    LNode *p = L,*q;
    for(n = 0; n < i - 1; n++){
        p = p->next;
    }
    q = p->next;
    p->next = q->next;
    free(q);
    return;
}

void TraverseList(LinkList L){
    LNode *p = L->next;
    while(p){
        std::cout << p->data << " " << std::endl;
        p = p->next;
    }
    return;
}

bool Head_Insert(LinkList &L, int e){
    LNode *s = (LNode *)malloc(sizeof(LNode));
    s->data = e;
    s->next = L->next;
    L->next = s;
    L->data += 1;
    return true;
}

bool Tail_Insert(LinkList &L){
    LNode *tail = L;
    while(1){
        LNode *s = (LNode *)malloc(sizeof(LNode));
        int e;
        std::cin >> e;
        if(e < 0)
            break;
        s->data = e;
        tail->next = s;
        tail = s;
        L->data += 1;
    }
    tail->next = NULL;
    return true;
}

void create_a_linklist(LinkList &L){
    std::cout << "头插法(1)/尾插法(2)" << std::endl;
    int flag;
    std::cin >> flag;
    if(flag == 1){
        while(1){
            int e;
            std::cin >> e;
            Head_Insert(L, e);
            if(e < 0)
                break;
        }//输入小于0的则停止
    }else if(flag == 2){
        Tail_Insert(L);
    }else{
        std::cout << "错误的flag" << std::endl;
    }
}


int menu(){
    std::cout << "1.初始化 "
                 "2.销毁 "
                 "3.清空 "
                 "4.判空 "
                 "5.求长 "
                 "6.取值 "
                 "7.定位 "
                 "8.前驱 "
                 "9.后继 "
                 "10.插入 "
                 "11.删除 "
                 "12.输出 "
                 "13.建表"
                 "0.退出" << std::endl;
    int flag;
    std::cout << "请你输入你想要的操作:" << std::endl;
    std::cin >> flag;
    return flag;
}

#endif /* link_list_h */

2.测试代码:main.cpp(与之前顺序表的基本一致,只是加了一个头插法和尾插法的操作)

//
//  main.cpp
//  线性表的链式实现
//
//  Created by 李若白 on 2020/12/5.
//

#include <iostream>
#include "link_list.h"
 
using namespace std;
int main() {
    LinkList L;
    while(1){
        int flag = menu();
        if(flag==0){
            break;
        }else if(flag == 1){
            InitList(L);
        }else if(flag == 2){
            DestoryList(L);
        }else if(flag == 3){
            ClearList(L);
        }else if(flag == 4){
            if(ListEmpty(L))
                cout << "empty" << endl;
            else
                cout << "unempty" << endl;
        }else if(flag == 5){
            cout << ListLength(L) << endl;
        }else if(flag == 6){
            cout << "Please input the value you want to search:" << endl;
            int e,i;
            cin >> i;
            GetElem(L,i,e);
            cout << e << endl;
        }else if(flag == 7){
            cout << "Please input the location you want to identity:" << endl;
            int e,v;
            cin >> e;
            v = LocateElem(L,e);
            if(v!=-1)
                cout << "The location of you value is:" << v << endl;
            else
                cout << "There no value you input Location!" << endl;
        }else if(flag == 8){
            cout << "Please input a value you want to find the prior value:" << endl;
            int e,pe;
            cin >> e;
            PriorElem(L,e,pe);
            if(pe != -1){
                cout << "Prior value is:" << pe << endl;
            }else{
                cout << "There no a prior of you input value!" << endl;
            }
        }else if(flag == 9){
            cout << "Please input a value you want to find the next value:" << endl;
            int e,ne;
            cin >> e;
            NextElem(L,e,ne);
            if(ne != -1){
                cout << "Next value is:" << ne << endl;
            }else{
                cout << "There no a next of you input value!" << endl;
            }
        }else if(flag == 10){
            cout << "Please input the value you want to insert:" << endl;
            int e;
            cin >> e;
            cout << "Please input the location you want to insert:" << endl;
            int loc;
            cin >> loc;
            ListInsert(L, loc, e);
        }else if(flag == 11){
            cout << "Please input the location you want to delete" << endl;
            int loc;
            cin >> loc;
            ListDelete(L,loc);
        }else if(flag == 12){
            TraverseList(L);
        }else if(flag == 13){
            create_a_linklist(L);
        }
        else{
            cout << "Error input!" << endl;
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值