c++实现双链表的基本操作

#include <iostream>
using namespace std;

#define MAXSIZE 20
#define TRUE 1
#define FALSE 0
typedef bool Status;          /* Status是函数的类型,其值是函f数结果状态代码*/
typedef char ElemType;        /* ElemType类型根据实际情况而定,这里假设为int */

typedef struct Node {
    ElemType data;
    struct Node* prior;
    struct Node* next;
}DuLinkList;

void DisplayList(DuLinkList* List)
{
    Node* p = List;
    while (p->next)
    {
        p = p->next;
        cout << p->data << ' ';
    }
    cout << endl;
}

void InitList(DuLinkList*& List)
{
    List = new DuLinkList;
    List->next = NULL;
    List->prior = NULL;
}

void CreateListAtHead(DuLinkList*& List, ElemType arr[], int len)
{
    Node* temp;
    List = new DuLinkList;
    List->next = NULL; // 作为链表结束标志
    if (len <= 0)
        return;

    // 先创建第一个结点
    temp = new Node;
    temp->data = arr[0]; // 把数据放进去
    temp->prior = List;
    temp->next = List->next;
    List->next = temp;

    for (int i = 1; i < len; i++)
    {
        temp = new Node;
        temp->data = arr[i]; // 把数据放进去
        temp->prior = List;
        temp->next = List->next;
        List->next = temp;
    }
}

void CreateListAtTail(DuLinkList*& List, ElemType arr[], int len)
{
    Node* temp, * p; // temp用来存放临时的地址, p用来指向最后的那个节点
    List = new DuLinkList;
    List->next = NULL; // 作为链表结束标志
    List->prior = NULL;
    p = List;
    for (int i = 0; i < len; i++)
    {
        temp = new Node;
        temp->data = arr[i];
        p->next = temp;
        temp->prior = p;
        p = temp;
    }
    p->next = NULL;
}

Status ListInsert(DuLinkList*& List, int position, ElemType e)
{
    if (position <= 0)
        return FALSE;
    DuLinkList* p = List;
    DuLinkList* temp;
    int count = 0;
    while (p->next && count < position)
    {
        p = p->next;
        count++;
    }
    if (count != position)
        return FALSE;
    temp = new Node;
    temp->data = e;
    temp->next = p;
    temp->prior = p->prior;
    p->prior->next = temp;
    p->prior = temp;
    return TRUE;
}

Status ListDelete(DuLinkList*& List, int position, ElemType& e)
{
    if (position <= 0)
        return FALSE;
    DuLinkList* p = List;
    DuLinkList* temp;
    int count = 0;
    while (p->next && count < position)
    {
        p = p->next;
        count++;
    }
    if (count != position || p == NULL)
        return FALSE;
    e = p->data;
    temp = p;
    p->prior->next = p->next;
    p->next->prior = p->prior;
    delete temp;
    return TRUE;
}

void ClearList(DuLinkList*& List)
{
    DuLinkList* p = List;
    DuLinkList* temp;

    while (p = p->next)
    {
        temp = p;
        p = p->next;
        delete temp;
    }
}

Status ListEmpty(DuLinkList* List)
{
    if (List->next == NULL)
        return TRUE;
    else
        return FALSE;
}

int ListLength(DuLinkList* List)
{
    DuLinkList* p = List;
    int count = 0;
    while (p->next)
    {
        p = p->next;
        count++;
    }
    return count;
}

Status GetElem(DuLinkList* List, int position, ElemType& e)
{
    DuLinkList* p = List;
    int count = 0;
    if (position <= 0)
        return FALSE;
    while (p->next && count < position)
    {
        p = p->next;
        count++;
    }
    e = p->data;
    return TRUE;
}

int LocateElem(DuLinkList* List, ElemType e)
{
    Node* p = List;
    int count = 0;
    while (p->next)
    {
        p = p->next;
        count++;
        if (p->data == e)
            return count;
    }
    return 0;
}

void DestroyList(DuLinkList*& List)
{
    ClearList(List);
    delete List;
}

/*去两个集合的合集, LA和LB位要去合集的对象, LC为结果*/
void UnionList(DuLinkList* LA, DuLinkList* LB, DuLinkList*& LC)
{
    int lena, lenb, i;
    ElemType e;
    lena = ListLength(LA);
    lenb = ListLength(LB);
    for (i = 1; i <= lena; i++) // 把LA放到LC上
    {
        GetElem(LA, i, e);
        ListInsert(LC, i, e);
    }
    for (i = 1; i <= lenb; i++)
    {
        GetElem(LB, i, e);
        if (!LocateElem(LA, e))
            ListInsert(LC, ++lena, e);
    }
}

void CommonList(DuLinkList* LA, DuLinkList* LB, DuLinkList*& LC)
{
    ElemType e;
    int lena = ListLength(LA);
    for (int i = 1; i <= lena; i++)
    {
        GetElem(LA, i, e);
        if (LocateElem(LB, e))
            ListInsert(LC, ListLength(LC) + 1, e);
    }
}

void DifferenceSetList(DuLinkList* LA, DuLinkList* LB, DuLinkList*& LC)
{
    ElemType e;
    int lena = ListLength(LA);
    for (int i = 1; i <= lena; i++)
    {
        GetElem(LA, i, e);
        if (!LocateElem(LB, e))
            ListInsert(LC, ListLength(LC) + 1, e);
    }
}

int main()
{
    int temp, posi;
    ElemType temp1;
    DuLinkList* p;
    DuLinkList* list;
    ElemType arr[10] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
    CreateListAtTail(list, arr, 10);
    DisplayList(list);
    

    int i = 1;
    while (scanf("%d", &posi) != EOF && i<= 3)
    {
        ListDelete(list, posi, temp1);
        cout << temp1 << endl;
        DisplayList(list);
        i++;
    }

    i = 1;
    while (scanf("%d %c", &posi, &temp) != EOF && i<= 3)
    {
        ListDelete(list, posi, temp);
        cout << temp1 << endl;
        DisplayList(list);
        i++;
    }

    // 测试双向
    p = list->next;
    cout << p->data << endl;
    while (scanf("%d", &temp) != EOF)
    {
        if (temp == 1)// 往前
        {
            p = p->prior;
            cout << p->data << endl;
        }
        if (temp == 2)// 往后
        {
            p = p->next;
            cout << p->data << endl;
        }
    }



    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值