VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 567|回复: 2

[已解决]一道链表的题,谢谢大佬

[复制链接]
41_avatar_middle
最佳答案
0 
在线会员 发表于 2020-3-17 16:01:39 | 显示全部楼层 |阅读模式
10驿站币
#include  <iostream>
using  namespace  std;

class  ListNode  {
        friend  class  LinkList;    //声明LinkList为友元类
        public:
                ListNode(){next  =  NULL;};
                ListNode(int  x){
                        val  =  x;
                        next  =  NULL;
                }
        private:
            int  val;  //数据域
            ListNode  *next;  //指针域
    };

class  LinkList{
        public:
                LinkList(int*  arr,  int  len);  //构造函数
                ~LinkList();  //析构函数
                void  Output();
        private:
                ListNode*  head;//头指针,指向第一个结点
};

LinkList::LinkList(int*  arr,  int  len)
怎么去写这个linklist呢?如何去实现这个函数呢?
最佳答案
92_avatar_small
2020-3-17 16:01:40
  1. class ListNode {
  2.         friend  class  LinkList;    //声明LinkList为友元类
  3. public:
  4.         ListNode(){ next = NULL; };
  5.         ListNode(int  x){
  6.                 val = x;
  7.                 next = NULL;
  8.         }
  9. private:
  10.         int  val;  //数据域
  11.         ListNode  *next;  //指针域
  12. };

  13. class LinkList {
  14. public:
  15.         LinkList(int*  arr, int  len);  //构造函数
  16.         ~LinkList();  //析构函数
  17.         void  Output();
  18. private:
  19.         ListNode*  head;//头指针,指向第一个结点
  20. };

  21. LinkList::LinkList(int*  arr, int  len)
  22. {
  23.         head = NULL;
  24.         ListNode* p_last = NULL;

  25.         for (int idx = 0; idx < len; ++idx)
  26.         {
  27.                 ListNode* p_curr = new ListNode();
  28.                 p_curr->val = arr[idx];

  29.                 if (!p_last)
  30.                 {
  31.                         head = p_curr;
  32.                         p_last = p_curr;
  33.                 }
  34.                 else
  35.                 {
  36.                         p_last->next = p_curr;
  37.                         p_last = p_curr;
  38.                 }
  39.         }
  40. }

  41. LinkList::~LinkList()
  42. {
  43.         ListNode* p = head;
  44.         while (p)
  45.         {
  46.                 ListNode* pT = p;
  47.                 p = p->next;
  48.                 delete pT;
  49.         }
  50. }

  51. void LinkList::Output()
  52. {
  53.         ListNode* p = head;
  54.         while (p)
  55.         {
  56.                 cout << p->val << endl;
  57.                 p = p->next;
  58.         }
  59. }
复制代码


就爱做这样的小题目练手

最佳答案

查看完整内容

就爱做这样的小题目练手




上一篇:如何去用类实现字符串的输入输出?
下一篇:哪位大哥帮看一下这代码的具体作用?
92_avatar_middle
最佳答案
9 
在线会员 发表于 2020-3-17 16:01:40 | 显示全部楼层    本楼为最佳答案   
bestAnswer
  1. class ListNode {
  2.         friend  class  LinkList;    //声明LinkList为友元类
  3. public:
  4.         ListNode(){ next = NULL; };
  5.         ListNode(int  x){
  6.                 val = x;
  7.                 next = NULL;
  8.         }
  9. private:
  10.         int  val;  //数据域
  11.         ListNode  *next;  //指针域
  12. };

  13. class LinkList {
  14. public:
  15.         LinkList(int*  arr, int  len);  //构造函数
  16.         ~LinkList();  //析构函数
  17.         void  Output();
  18. private:
  19.         ListNode*  head;//头指针,指向第一个结点
  20. };

  21. LinkList::LinkList(int*  arr, int  len)
  22. {
  23.         head = NULL;
  24.         ListNode* p_last = NULL;

  25.         for (int idx = 0; idx < len; ++idx)
  26.         {
  27.                 ListNode* p_curr = new ListNode();
  28.                 p_curr->val = arr[idx];

  29.                 if (!p_last)
  30.                 {
  31.                         head = p_curr;
  32.                         p_last = p_curr;
  33.                 }
  34.                 else
  35.                 {
  36.                         p_last->next = p_curr;
  37.                         p_last = p_curr;
  38.                 }
  39.         }
  40. }

  41. LinkList::~LinkList()
  42. {
  43.         ListNode* p = head;
  44.         while (p)
  45.         {
  46.                 ListNode* pT = p;
  47.                 p = p->next;
  48.                 delete pT;
  49.         }
  50. }

  51. void LinkList::Output()
  52. {
  53.         ListNode* p = head;
  54.         while (p)
  55.         {
  56.                 cout << p->val << endl;
  57.                 p = p->next;
  58.         }
  59. }
复制代码


就爱做这样的小题目练手
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-10 10:22:52 | 显示全部楼层

#include  <iostream>
using  namespace  std;

class  ListNode {
        friend  class  LinkList;    //声明LinkList为友元类
public:
        ListNode() { next = NULL; };
        ListNode(int  x) {
                val = x;
                next = NULL;
        }
private:
        int  val;  //数据域
        ListNode  *next;  //指针域
};

class  LinkList {
public:
        LinkList(int*  arr, int  len);  //构造函数
        ~LinkList();  //析构函数
        void  Output();
private:
        ListNode*  head;//头指针,指向第一个结点
};

LinkList::LinkList(int * arr, int len)
        :head(NULL)
{
        if (arr == NULL || len <= 0)
        {
                return;
        }

        //逆序遍历数组,链表头插。保持数组的顺序
        for (int  nIndex = len - 1; nIndex >= 0; --nIndex)
        {
                ListNode* pNode = new ListNode();
                pNode->val = arr[nIndex];
                pNode->next = head;

                head = pNode;
        }
}

LinkList::~LinkList()
{
        while (head != NULL)
        {
                ListNode* pNode = head->next;
                delete head;
                head = pNode;
        }
}

void LinkList::Output()
{
        ListNode* pNode = head;
        while (pNode != NULL)
        {
                cout << pNode->val << endl;
                pNode = pNode->next;
        }
}


int main()
{
        int arr[9] = { 1,2,3,4,5,6,7,8,9 };
        LinkList linklist(arr, 9);
        linklist.Output();

        return 0;
}
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-5-29 23:32

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表