【发布时间】:2021-02-07 05:46:15
【问题描述】:
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
// Constructor
node(int d){
data = d;
next = NULL;
}
};
// Linked List from Vector
void createList(node*& head , vector<int> v){
cout<<v[0];
head->data = v[0];
head->next = NULL;
node* last = head;
for(int i = 1 ; i < v.size() ; i++){
cout<<"X"<<endl;
node* temp = new node(v[i]);
last->next = temp;
last = temp;
}
cout<<head->data;
}
// Print Linked list
void printList(node* head){
while(head != NULL){
cout<<head->data;
head = head->next;
}
}
int main(){
vector<int> v = {1 , 2 , 3 , 4 , 5};
node* head = NULL;
createList(head , v);
cout<<head->data;
printList(head);
}
上面这段代码是我使用的。首先,我创建了一个向量。然后我使用 tht 向量创建了一个链表 然后我正在尝试打印链接列表。但是输出什么也没显示。
输出什么也不显示。没有错误,没有输出。
【问题讨论】:
-
How to debug small programs。提示:
head在表达式head->data = v[0];中指向什么?
标签: c++ linked-list singly-linked-list