双向链表结构实现线性表

结合之前的代码和学习,我又写了双向链表实现线性表。双向链表在找前驱后继有比较好的优势。操作命名是严格按照严蔚敏版数据结构写的,大家复习时可以参考一下。

1.测试代码(main.cpp)

#include <iostream>
#include "dulink_list.h"

using namespace std;
int main() {
    dulink_list 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;
}

2.操作集(dulink_list.h)

#include <iostream>
#include <cstdlib>

typedef struct duNode{
    int data;
    duNode *next;
    duNode *prior;
}duNode, *dulink_list;

void InitList(dulink_list &L){
    L = (duNode*)malloc(sizeof(duNode));
    L->data = 0;
    L->next = NULL;
    L->prior = NULL;
}

void DestoryList(dulink_list &L){
    duNode *p = L->next, *q;
    while(p){
        q = p;
        p = p->next;
        free(q);
    }
    free(L);
    return;
}

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

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

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

void GetElem(dulink_list L, int i, int &e){//注意这里我仍然会使用屏蔽操作,对外的下标起始点为1
    int j = 1;
    duNode *p = L->next;
    if(i<1||i>L->data)
        e = -1;
    while(p){
        if(j == i)
            e = p->data;
        p = p->next;
        j++;
    }
    e = -1;//使用-1表示没有
}

int LocateElem(dulink_list L, int e){
    int j = 1;
    duNode *p = L->next;
    while(p){
        if(e == p->data)
            return j;
        p = p->next;
        j++;
    }
    return -1;
}

void PriorElem(dulink_list L, int cur_e, int &pre_e){
    duNode *p = L->next;
    while(p){
        if(p->data == cur_e){
            if(p == L->next){
                pre_e = -1;//第一个元素没有前驱
                return;
            }
            pre_e = p->prior->data;
            return;
        }
        p = p->next;
    }
    pre_e = -2;//没有找到此元素
}

void NextElem(dulink_list L, int cur_e, int &next_e){
    duNode *p = L->next;
    while(p){
        if(p->data == cur_e){
            if(p->next == NULL){
                next_e = -1;//最后一个元素没有后继
                return;
            }
            next_e = p->next->data;
            return;
        }
        p = p->next;
    }
    next_e = -2;//没有找到此元素
}

void ListInsert(dulink_list &L, int i, int e){
    duNode *p = L->next, *q;
    int j = 1;
    if(i<1||i>L->data)
        return;
    while(p->next){
        if(i == j){
            q = p->prior;
            duNode *s = (duNode*)malloc(sizeof(duNode));
            s->data = e;
            s->next = p;
            s->prior = q;
            p->prior = s;
            q->next = s;
            L->data += 1;
            return;
        }
        j++;
        p = p->next;
    }
    if(j == i){
        duNode *s = (duNode*)malloc(sizeof(duNode));
        s->data = e;
        p->next = s;
        s->prior = p;
        s->next = NULL;
        L->data += 1;
    }
}

void ListDelete(dulink_list &L, int i){
    duNode *p = L->next, *q, *n;
    int j = 1;
    if(i<1||i>L->data)
        return;
    while(p->next){
        if(i == j){
            q = p->prior;
            n = p->next;
            q->next = n;
            n->prior = q;
            free(p);
            L->data -= 1;
            return;
        }
        j++;
        p = p->next;
    }
    if(i == j){
        q = p->prior;
        q->next = NULL;
        free(p);
        L->data-=1;
    }
}

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

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;
}

void create_a_linklist(dulink_list &L){//在此简单写一个头插法
    int e;
    duNode *s;
    std::cout << "请输入数字,负数结尾" << std::endl;
    while(1){
        std::cin >> e;
        if(e<0)
            break;
        s = (duNode*)malloc(sizeof(duNode));
        s->data = e;
        if(L->next!=NULL){
            s->prior = L;
            s->next = L->next;
            L->next->prior = s;
            L->next = s;
            L->data++;
        }else{
            s->next = NULL;
            s->prior = L;
            L->next = s;
            L->data++;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值