鱼C论坛

 找回密码
 立即注册
查看: 152|回复: 2

为什么这个代码运行后什么都显示不出来

[复制链接]
发表于 2024-3-17 20:30:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
using namespace std;

typedef struct LIST1{
        int num;
        LIST1* next;
        LIST1(int x) : num(x), next(NULL){}
}list1;
class Linked{
        private:
               
        public:
                list1* head;
               
                Linked(){
                        head = NULL;
                }
               
                void creat(int n,list1* head){
        //        system("pause");
                        int i=0;
                        list1* p;
                        //list1* p1;
                       
                        //p->num = n;
                        //cout<<p->num<<endl;
                        p = head;
//                        p = new list1(1);
                        cout<<87<<endl;
                        for(i=0;i<n;i++){//注意:为尾插法,head无值
                                p = new list1(i+1);               
                                head->next = p;
                                p->next = NULL;
                                head = head->next;
                        }
                        //return head;
                }       
               
                //void delete(list1* h){//删除h结点,但不到下一个
                       
                //}
                void bianli(list1* h){
                        int i = 0;
                        while(h!=NULL){
                                cout<<h->num<<endl;
                                h = h->next;
                        }
                }
};

int main(){
        int i=0,n=0,m=0;
        cin>>n;
        cin>>m;
        Linked* l1;
        list1* p;
        l1->creat(n,l1->head);
        cout<<"78"<<endl;
        l1->bianli(p);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-17 20:30:30 | 显示全部楼层
这段代码没有主函数,所以没有执行任何操作。在C++中,每一个程序都需要一个入口点,也就是主函数main()。请在代码中添加如下主函数:

  1. int main(){
  2.     Linked linked;
  3.     linked.creat(10, NULL);
  4.     return 0;
  5. }
复制代码


现在运行你的代码会输出一个链表节点的创建。

另外,你的`creat()`函数有一个问题,你传入的头节点指针没有被正确地赋值。解决办法是将`list1* head`的参数去掉,通过`this->head`来访问类的成员属性。所以`creat()`函数应该改成:

  1. void creat(int n){
  2.     int i=0;
  3.     list1* p = new list1(n);
  4.     this->head = p;
  5.     cout << "Node created with value " << n << endl;
  6. }
复制代码


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 21:46:20 | 显示全部楼层
  1. #include <iostream>

  2. using std::cin, std::cout, std::endl;
  3. using std::ostream;

  4. class list_t {
  5.     struct node_t {
  6.         int num;
  7.         node_t *next;
  8.         node_t(int x): num(x), next(nullptr) {}
  9.     };
  10.     node_t *head = nullptr;
  11.     static void list_free(node_t *node) {
  12.         if(!node) return;
  13.         list_free(node->next);
  14.         delete node;
  15.     }
  16. public:
  17.     list_t(size_t size) {
  18.         node_t **next = &this->head;
  19.         for(size_t i = 0; i < size; ++i) {
  20.             *next = new node_t(i + 1);
  21.             next = &(*next)->next;
  22.         }
  23.     }
  24.     ~list_t() {this->list_free(this->head);}
  25.     friend ostream &operator<<(ostream &os, const list_t &list) {
  26.         for(node_t *node = list.head; node; node = node->next)
  27.             os << node->num << " ";
  28.         return os;
  29.     }
  30. };

  31. int main() {
  32.     int size; cin >> size;
  33.     cout << list_t(size) << endl;
  34.     return 0;
  35. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 04:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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