WHCSRL 技术网

c++ 单链表

学了数据结构c++有一年多了,来来去去看了好几回的书,(中间跑去学java基础了)顺手就发个自己完善的小单链表测试。
代码:

#include<iostream>
#include<string>
using namespace std;
#define MAXNUM 100
/*单链表*/
struct List
{
	int data;
	struct List * next;//链表定义
};//每次新建立的对象其实都是代表*next
//struc List*L意思是创建一个指向了List列表的L指针
class LinkListOperaotr
{
public:
    int mark;
    void ShowMenu();//菜单
    void Init(struct List* L);//创建列表
    void Show(struct List* L);//遍历链表
    int  LengthList(struct List* L);//链表长度,因为要获得长度 所以要有返回值
    void InsertList(struct List* L, int k, int data);//链表插入--按照位置
    void DeletetListPoint(struct List* L, int k);//链表删除--按照位置
    bool IsEmptyList(struct List* L);//链表是否为空
    void CleanList(struct List* L);//清空链表
    void SearchPoint(struct List* L, int point);//查找数据--按照位置
};
void LinkListOperaotr::ShowMenu()
{
    cout <<"***0遍历链表**********" << endl;
    cout <<"***1删除元素-按位置***" << endl;
    cout <<"***2插入链表-按位置***" << endl;
    cout <<"***3链表长度**********" << endl;
    cout <<"***4链表是否为空******" << endl;
    cout <<"***5搜索数据-按位置***" << endl;
    cout <<"***6******************" << endl;
    cout <<"***7释放线性表内存****" << endl;
    cout <<"***9退出程序**********" << endl;
}
bool  LinkListOperaotr::IsEmptyList(struct List* L)//----------------链表是否为空
{
    if (L->next == 0)
        return true;
    else
        return false;
}
void LinkListOperaotr::Init(struct List * L)//---------------创建链表
{
    int cur;
    cin >> cur;//初始输入一个结点
    while (cur != -1) //输入-1的时候停止链表的输入,也就是中断链表的初始输入
    {
        struct List* ptr = (struct List*)malloc(sizeof(struct List));//将List的地址赋值给了ptr,ptr所带来的变化影响的是List,没有创建新的内存
        ptr->data = cur;
        ptr->next = NULL;
        L->next = ptr;
        L = L->next;
        cin >> cur;//输入cur每次输入都会创建一个新的结点
    }
}
void LinkListOperaotr::Show(struct List* L)//-------------展示链表
{ //遍历链表值
    cout << "链表遍历:";

    while (L->next) 
    {
        cout << L->next->data << " ";
        L = L->next;
    }
    cout << endl;
}
// 在第K个位置前插入data元素, 最后链表 的第K个位置就是data
void LinkListOperaotr::InsertList(struct List* L, int k, int date) //k在之后的主函数输入,data为要插入的元素
{
    struct List* pre = NULL; //存储第K-1个元素的值
    struct List* ptr = (struct List*)malloc(sizeof(struct List)); //申请空间
    ptr->data = date;
    ptr->next = NULL;

    while (k && L->next)
    {  //查找到放置data元素的位置
        pre = L;
        L = L->next;
        k--;
    }

    if (k > 0)
    {    //如果K > 0 直接插到链表的表尾
        L->next = ptr;
        L = L->next;
    }
    else 
    {
        pre->next = ptr;    //链接链表
        ptr->next = L;
    }
}
int LinkListOperaotr::LengthList(struct List* L)//--此函数也方便其它函数的操作--链表长度
{
  
    int len=0;//记录长度
    while(L->next!=NULL)//如果L的下一个位置有元素就成立循环
    {
        len++;
;        L = L->next;
    }
    return len;//返回长度
}
void LinkListOperaotr::CleanList(struct List* L)//--------------清空链表
{
    struct List* ptr = L;
    if (LengthList(L) > 0)//如果链表不为空可以清楚
    {
        while (ptr->next)
        {
            struct List* temp = ptr->next;//存储上一级指针所在位置
            ptr->next = ptr->next->next;//指针前移动
            free(temp);//释放掉上一个指针的空间
            //这种做法避免指针前移以后不能释放空间,当然也能ptr->next=ptr->next->next直接让后面的数据无法显示,但是达不到内存释放的效果
        }
    }

}
void LinkListOperaotr::DeletetListPoint(struct List* L, int k)//----------删除链表元素---按照位置
{
    //cout << "123456";

    if (LengthList(L) <= 0)
    {
        cout << "列表为空,无法删除元素" << endl;
        return;
    }
    struct List* ptr = L->next;//前一个元素
    struct List* pre = L;//记录后一个元素
    k = k - 1;
    while (ptr&&k--)
    {
        //此过程两个指针向前位移
        pre = ptr;
        ptr = ptr->next;
    }
    if (ptr == NULL || k > 0)
    {
        cout << "找不到可以删除元素的位置";
    }
    else
    {
        pre->next = ptr->next;//指针前移
        free(ptr);
    }
}
void LinkListOperaotr::SearchPoint(struct List* L, int point)//寻找数据----------按位查找
{
    int mark = point;
    struct List* ptr = L;
    while (point > 0 && ptr->next)
    {
        ptr = ptr->next;//指针前移动
        point--;
    }
    if (point == 0 && ptr != NULL)
    {
        cout << "第" << mark << "数据是:" << ptr->data << endl;;
    }
    else {
        cout << "第" << mark << "个元素不存在哦!" << endl;
    }
}
int main()
{
    LinkListOperaotr LLO;//实例化成员对象
    struct List* head = (struct List*)malloc(sizeof(struct List));//申请动态空间 大小为结构体内存*数据类型内存
    head->next = NULL;//指针置空
    cout << "请先创建链表以实现接下来的功能" << endl;
    cout << "输入链表的数据" << "      " << "输入-1结束" << endl;
    LLO.Init(head);//创建链表
    while (true)
    {
        LLO.ShowMenu();
        int choice;
        cout << "输入你的选择" << endl;
        cin >> choice;
        switch (choice)
        {
        case 0:
            LLO.Show(head);
            break;
        case 1:
            cout << "恁选择了删除元素" << endl;
            int point;
            cout << "输入删除元素的位置:";
            cin >> point;
            LLO.DeletetListPoint(head,point);
            cout << "您成功删除了元素";
            break;
        case 2:
        {cout << "输入要插入的链表数据" << endl;
        cout << "位置:";
        int k;
        cin >> k;
        cout << "数据:";
        int data;
        cin >> data;
        LLO.InsertList(head, k, data);
        break;
        }
        case 3:
            cout << "链表长度为:" << LLO.LengthList(head);
            break;
        case 4:
       
            if (LLO.IsEmptyList(head))
                cout << "链表为空" << endl;
            else
                cout << "链表不为空" << endl;
            break;
        case 5:
            cout << "输入你要搜索的数据的位置";
                int poi;
            cin >> poi;
            LLO.SearchPoint(head, poi);
            break;
        case 6:
            break;
        case 7:
            cout << "恁选择了释放链表";
            LLO.CleanList(head);
            break;
        case 9:
            system("pause");
            exit(0);
;            break;
        default:
            system("cls");
            break;
        }

    }
    return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239

  • 1
推荐阅读