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* next;
}LinkList;

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

void InitList(LinkList*& List)
{
    List = new LinkList;
    List->next = List;
}

void CreateListAtHead(LinkList*& List, ElemType arr[], int len)
{
    Node* temp;
    List = new LinkList;
    List->next = List;
    for (int i = 0; i < len; i++)
    {
        temp = new Node;
        temp->data = arr[i]; // 把数据放进去
        temp->next = List->next;
        List->next = temp;
    }
}

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

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

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

void ClearList(LinkList*& List)
{
    LinkList* p = List->next;
    LinkList* temp;

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

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

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

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

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

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

int main()
{
    ElemType e;
    int p;
    LinkList* list;
    ElemType arr[5] = { 'a', 'b', 'c', 'd', 'e'};
    
    cout << "1.初始化循环单链表:" << endl;
    InitList(list);

    cout << "2.依次采用尾插法插入a,b,c,d,e: " << endl;
    CreateListAtTail(list, arr, 5);

    cout << "3.输入循环单链表: " << endl;
    DisplayList(list);

    cout << "4.输出循环单链表的长度:";
    cout << ListLength(list) << endl;

    cout << "5.判断循环单链表是否未为空:" << endl;
    if (ListEmpty(list))
        cout << "链表为空" << endl;
    else
        cout << "链表不为空" << endl;

    cout << "6.查找元素: " << endl;
    cout << "输入你要查找的位置:";
    cin >> p;
    if (GetElem(list, p, e))
        cout << "查找成功\n" << "在" << p << "位置的值是:" << e << endl;
    else
        cout << "输入范围错误" << endl;

    cout << "7.输出元素的位置:";
    cin >> e;
    if (LocateElem(list, e) != -1)
        cout << e << "的第一个位置是:" << LocateElem(list, e) << endl;
    else
        cout << "顺序表中没有对应的值" << endl;

    cout << "8.在第p个位置插入e元素" << endl;
    cout << "输入你要插入的值: ";  cin >> e;
    cout << "输入你要插入的位置: ";    cin >> p;
    if (ListInsert(list, p, e))
        cout << "插入成功" << endl;
    else
        cout << "插入失败" << endl;

    cout << "9.输出循环单链表:" << endl;
    DisplayList(list);

    cout << "10. 删除顺序表的第P个元素" << endl;
    cout << "输入你要删除的元素的位置:";
    cin >> p;
    if (ListDelete(list, p, e))
    {
        cout << "删除成功" << endl;
        cout << "删除的位置的值为:" << e << endl;
    }
    else
    {
        cout << "删除失败" << endl;
    }

    cout << "11.输出循环单链表:" << endl;
    DisplayList(list);

    cout << "12.销毁单链表:";
    DestroyList(list);

    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值