从用户输入创建和打印链表
Creating and printing linked list from user input C++
#include <iostream>
using namespace std;
int main()
{
struct node
{
int data;
node * next;
};
node * head;
node * n;
node * temp;
node * q;
int number;
cout << "Enter numbers";
cin >> number;
n = new node;
n->data = number;
head = n;
temp = n;
while (cin >> number)
{
while (number != -500)
{
n = new node;
n->data = number;
temp->next = n;
temp = n;
}
}
while (head != NULL)
{
cout << head->data;
head = head->next;
}
}
我不明白为什么这不起作用。该程序创建一个新节点,然后设置用户输入的与该新节点的变量数据相等的任何内容,然后使head和temp指向新节点。然后它获取用户的第二个输入,并将其与-500进行比较,如果结果为真,则创建一个新节点,将第二个输入的数据放入变量data中,然后将第一个节点和第二个节点链接在一起,然后将temp点指向第二个节点。如果第二个while循环的条件为false,则转到第三个循环,这是应该打印列表的地方。
谁将最后一个节点的旁边设置为NULL?
At n =新节点;n->next不是NULL但未定义,在调试版本中通常是0xcccccccc或类似的值,以使其可见,它没有初始化。如果你试图解引用它,你会得到一个访问冲突
while (cin >> number) { // this loop is endless, because you can always read the user input data (unless some exception happen)
while (number != -500)
{
n = new node; // you already have data allocated. no need to allocate it once more
n->data = number;
temp->next = n;
temp = n;
// as was already mentioned: set n->next to NULL
} }
如果您想在检查数字不是-500之后中断,那么您可以执行以下操作:
while (cin >> number) {
if (number != -500) { ...; // do your stuff
break;
}
}
并且,顺便说一下,您有内存泄漏。如果您使用平面C指针,则考虑使用delete
运算符来清除内存。为此,您需要知道列表从哪里开始(基本上是头),并在调用delete node
的整个列表中迭代。
也请考虑,这样写是一种糟糕的代码风格:
while (cin>> number)
您也可以尝试这个-下面的代码根据用户输入创建一个链表,printlinkedlist(函数)打印创建的链表。
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* next;
};
void printlinkedlist(node* node)
{
int c=0; //taken just for good looking output
while(node!=NULL)
{
if(c>0)
{
cout<<"->"<<node->data;
node = node->next;
}
else
{
cout<<node->data;
node = node->next;
c++;
}
}
}
int main() {
int n;
cout<<"Enter no. of nodes=";
cin>>n; //User enters number of nodes he want.
int num,c=0; //initialized c for setting head with second node..
node* head = new node; //initialized head node
node * temp = new node; //initialized temp node
cin>>num;
head->data=num;
for(int i=2;i<=n;i++)
{
if(c==0)
{
cin>>num;
temp->data=num;
head->next=temp; //head point to second node i.e. temp
c++;
}
else
{
cin>>num;
node * temp1 = new node; //initialize other temp node for every value
temp1->data=num;
temp->next=temp1; //point to temp1 to temp
temp=temp1; //set temp as temp1
}
}
printlinkedlist(head);
}
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
};
// a function to create linked list passing pointer to pinter
void create_list(node**head)
{
int num;//num is the data
cin>>num ;
node*new_node=new node();//
new_node->data=num;
new_node->next=NULL;
node*temp;
if(*head==NULL)
{
*head=temp=new_node;
}
else{
temp->next=new_node;
temp=new_node;
}
}
void print_list(node* head)
{
if(head == NULL)
{
cout<<"empty list"<<endl;
}
node *ptr = NULL;
ptr=head;
cout<<"data in the list: "<<endl;
while(ptr!=0)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
int main()
{
int n,i; //
cout<<"enter the number of nodes:"<<endl;
cin>>n;
cout<<"enter data as num:"<<endl;
node* head=NULL;
for(i=0;i<n;i++)
{
create_list(&head);//passing the address of head
}
print_list(head);
return 0;
}
相关文章: