c++list(list)

本篇文章给大家谈谈c++list,以及list对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

VC 中list和Clist的区别,以及怎么用

list是在stl库中的皮返悄燃渣

Clist是在MFC库中的

都是用来实现链表结构的,具体的接口可能稍有不用,总世档体差不多,看msdn就可以了

c 如何将list放入数组

用List的 toArray(T[] t) 方法就行。

例棚谨子 整数和字符串,其它都一样。

ListInteger a=new LinkedListInteger(); for(int i=0;i50;i++) a.add(i); Integer[] b=new Integer[a.size()]; b=a.toArray(b); System.out.println(Arrays.toString(b)); ListString c=new LinkedList链慧基碧段String(); StringBuilder sb=null; for(int i=0;i50;i++){ sb=new StringBuilder(); for(int j=0;j10;j++) sb.append((char)((Math.random()*26)+65)); c.add(sb.toString()); } String[] d=new String[c.size()]; d=c.toArray(d); System.out.println(Arrays.toString(d));

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

C语言没有类的概念。C++有现成的List类, #includelist即可。

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

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

#include iostream

#include fstream

#include stdlib.h

#include string.h

using namespace std;

#includestdio.h

#includestring

 

#include "math.h"

 

templateclass 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.ageendl;

}

 

int main()

{

    ListStudent 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;

}

[img]

关于c++list和list的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

标签列表