C++ 链表创建、插入、删除、遍历

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


using namespace std;


struct Node {
    int data;
    Node *next;
};

class Linklist
{
public :
    Linklist();
    ~Linklist();
    void CreatList1(int n);        //头插入法创建链表
    void CreatList2(int n);        //尾插入法创建链表
    void Insert(int i, int e);     //插入函数
    int Delete(int i);             //删除表中元素
    int GetData(int i);            //取得表中元素值
    int Search(int obj);           //在表中寻找匹配项
    int ListLength();              //获得表的长度
    void Display();                //遍历整个链表
private :
    Node *Head;  //指向一块内存,存储线性表
    //int m_size;    //线性表的大小
    int m_length;    //当前存储大小
};
Linklist::Linklist()
{
//    m_size = size;
    //m_pList = new int(m_size);
    Head = new Node;
    Head->next = NULL;
    m_length = 0;
}
Linklist::~Linklist()
{
    delete [] Head;
    Head = NULL;
}
void Linklist::CreatList1(int n)
{
    Node *p;
    Node *temp;
    p = Head;
    for (int i = 0; i < n; i++)
    {
        temp = new Node;
        cin >> temp->data;
        temp->next = p->next;
        p->next = temp;
    }
}
void Linklist::CreatList2(int n)
{
    Node *p;
    Node *temp;
    p = Head;
    for (int i = 0; i < n; i++)
    {
        temp = new Node;
        cin >> temp->data;
        p->next=temp;
        p = temp;
    }
}

void Linklist::Insert(int i, int e) {       //在i处插入e
    Node *temp;
    temp = Head;
    int j = 0;
    while (temp&&j < i - 1)
    {
        temp=temp->next;
        j++;
    }
    if (!temp || j > i - 1)
    {
        cout << "error" << endl;
    }
    else
    {
        Node *s = new Node;
        s->data = e;
        s->next = temp->next;
        temp->next = s;
    }
}
int Linklist::Delete(int i) {          //删除i处的数据
    Node *temp;
    temp = Head;
    int j = 0;
    while (temp&&j < i - 1)
    {
        temp = temp->next;
        j++;
    }
    if (!temp || j>i - 1)
    {
        cout << "error" << endl;
        return -1;
    }
    else
    {
        Node *s;
        s = temp->next;
        temp->next = s->next;
        delete s;
    }
}
int Linklist::GetData(int i) {         //得到i处的元素
    Node *temp;
    temp = Head;
    int j = 0;
    while (temp&&j < i - 1) {
        temp = temp->next;
        j++;
    }
    if (!temp || j > i - 1) {
        cout << "寻找位置错误\n";
        return -1;
    }
    else {
        cout << i << "处的数据为:" << temp->next->data << "\n";
        return temp->data;
    }
}
int Linklist::Search(int obj) {       //寻找链表中有无与obj匹配的元素
    int j = 1;
    Node *temp;
    temp = Head->next;
    while (temp && temp->data != obj) {
        temp = temp->next;
        j++;
    }
    if (temp == NULL) {
        cout << "该链表中无此元素" << "\n";
        return 0;
    }
    else {
        cout << "在该链表中的第" << j << "个元素等于" << obj << "\n";
        return j;
    }
}
int Linklist::ListLength() {                //计算链表长度
    Node *temp;
    temp = Head->next;
    int j = 0;
    while (temp)
    {
        temp = temp->next;
        j++;
    }
    return j;
}
void Linklist::Display()
{
    Node *temp;
    temp = Head->next;
    while (temp)
    {
        int e = temp->data;
        cout << e << " ";
        temp = temp->next;
    }
    cout << endl;
}

int main()
{
    int user_num;
    cin >> user_num;

    Linklist list1;  
    Linklist list2;
    list1.CreatList1(user_num);
    list1.Display();
    cout << list1.ListLength() << endl;
    list1.Insert(2, 99);
    cout << list1.ListLength() << endl;
    list1.Display();
    list1.Delete(2);
    cout << list1.ListLength() << endl;
    list1.Display();
    system("pause");
    return 0;
//}

猜你喜欢

转载自blog.csdn.net/xiemanliu9029/article/details/81171290
今日推荐