如何用C语言或C++实现一个List类?

Python019

如何用C语言或C++实现一个List类?,第1张

C语言没有类的概念。C++有现成的List类, #include<list>即可。

如果要自己实现可以参考C++数据结构的书籍,是最基本的练习。

这里实现一个简单的例程,请参考:

#include <iostream>

#include <fstream>

#include <stdlib.h>

#include <string.h>

using namespace std

#include<stdio.h>

#include<string>

 

#include "math.h"

 

template<class T> class List{

public:

    List()  //构造函数

    {

        pFirst = NULL

    }

     

    void Add(T& t)  //在Link表头添加新结点

    {

        if(pFirst == NULL)

        {

            pFirst = new Node

            *(pFirst->pT) = t

        }

        else

        {

            Node* pNewNode = new Node

            *(pNewNode->pT) = t

            pNewNode->pNext = pFirst

            pFirst = pNewNode

        }

    }

 

  void Remove(T& t) //在Link中删除含有特定值的元素

    {

        Node* pNode = pFirst

        if(*(pNode->pT) == t)

        {

            pFirst = pFirst->pNext

            delete pNode

            return

        }

        while(pNode != NULL)

        {

            Node* pNextNode = pNode->pNext

            if(pNextNode!=NULL)

            {

                if(*(pNextNode->pT) == t)

                {

                    pNode->pNext = pNextNode->pNext

                    delete pNextNode

                    return

                }

            }

            else

                return//没有相同的

 

            pNode = pNode->pNext

        }

    }

  T* Find(T& t)  //查找含有特定值的结点

    {

        Node* pNode = pFirst

        while(pNode != NULL)

        {

            if(*(pNode->pT) == t)

            {

                return pNode->pT

            }

            pNode = pNode->pNext

        }

        return NULL

    }

  void PrintList()  // 打印输出整个链表

    {

        if(pFirst == NULL)

        {

            cout<<"列表为空列表!"<<endl

            return

        }

        Node* pNode = pFirst

        while(pNode != NULL)

        {

            cout<<*(pNode->pT)<<endl

            pNode = pNode->pNext

        }

    }

  ~List()

    {

        Node* pNode = pFirst

        while(pNode != NULL)

        {

            Node* pNextNode = pNode->pNext

            delete pNode

            pNode = pNextNode

        }

    }

protected:

  struct Node{

    Node* pNext

    T* pT

 

    Node()

    {

        pNext = NULL

        pT = new T

    }

    ~Node()

    {

        delete pT

    }

  }

  Node *pFirst        //链首结点指针

}

 

class Student

{

public:

    char id[20]    //学号

    char name[20]    //姓名

    int age    //年龄

    Student()

    {

    }

    ~Student()

    {

    }

    Student(const char* pid, const char* pname, int _age)

    {

        strcpy(id, pid)

        strcpy(name, pname)

        age = _age

    }

    bool operator==(const Student& stu)

    {

        return strcmp(id, stu.id) == 0 && strcmp(id, stu.id) == 0 && age==stu.age

    }

    Student& operator=(const Student& stu)

    {

        strcpy(id, stu.id)

        strcpy(name, stu.name)

        age = stu.age

    }

    friend ostream& operator<< (ostream &out,const Student& stu)

}

ostream & operator<< (ostream &out,const Student& stu)

{

    out<<"id:"<<stu.id<<"\tname:"<<stu.name<<"\tage:"<<stu.age<<endl

}

 

int main()

{

    List<Student> stuList

    cout<<"添加学生前:"<<endl

    stuList.PrintList()

     

    Student stu1("1", "张三", 18)

    Student stu2("2", "李四", 18)

    Student stu3("3", "王五", 18)

    Student stu4("4", "至尊宝", 18)

    Student stu5("5", "猪八戒", 18)

    Student stu6("6", "唐僧", 18)

    Student stu7("7", "沙和尚", 18)

    Student stu8("8", "观音", 18)

    stuList.Add(stu1)

    stuList.Add(stu2)

    stuList.Add(stu3)

    stuList.Add(stu4)

    stuList.Add(stu5)

    stuList.Add(stu6)

    stuList.Add(stu7)

    stuList.Add(stu8)

    cout<<"添加学生后:"<<endl

    stuList.PrintList()

 

 

    Student stu11("1", "张三", 18)

    Student* pStu = stuList.Find(stu11)

    cout<<"查找到的同学是:"<<*pStu

 

    stuList.Remove(stu11)

    cout<<"\n\n删除第一个后:"<<endl

    stuList.PrintList()

 

    return 0

}

对于结构体类型变量定义,struct list和list在C++中是相同的,都是正确的。

但在C语言中,如果没有经过重定义处理,则会编译报错。如:

typedef struct list

{

   int data

   struct list *next

} list 

这样之后,可以使用struct list 或 list 来定义变量了,如:

struct list *  link

list * link1