链接列表我有小问题 [英] link list little problem that i have
本文介绍了链接列表我有小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// reverse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
class node
{
friend class linklist;
int data;
node *next;
};
class linklist
{
public:
linklist() ;
void insert() ;
void reverse() ;
void show() ;
private:
node *first ;
node *help ;
node *p;
node *last;
};
linklist::linklist()
{
first = help = NULL ;
}
void linklist::insert()
{
node *p = new node();
p-> data = NULL ;
int n ;
cout << "plz enter the numbers of numbers :" << endl ;
cin >> n ;
cout << "plz enter the numbers : " << endl ;
for(int i = 0 ; i < n ; i++)
{
cin >> p -> data ;
}
if(first == NULL)
{
first = p;
}
else
{
p -> next = first -> next ;
first -> next = p ;
}
}
void linklist::reverse()
{
node *newlist = NULL ;
node *current = first ;
while(current )
{
node *next = current ->next;
current ->next = newlist;
newlist = current ;
current = next ;
}
}
void linklist::show()
{
int r = 2;
cout << "number " ;
node *curPtr = first;
while( curPtr ) {
cout << curPtr -> data << " ";
}
curPtr = curPtr -> next;
r ++;
}
int main()
{
linklist m;
m.insert();
m.reverse();
m.show();
return 0;
}
</iostream>
如果我想让我的程序在反向后写所有数字,该怎么办?
它没有做
what should i do if i want my program write all the numbers after reverse ?
it doesnt do
推荐答案
它问数字然后停止工作
看起来这就是您编写的代码.
此处:
it ask the numbers then stop working
Looks like that is the code what you have written.
here:
m.insert();
return 0;
您预计接下来会发生什么?看起来只是按照书面要求执行.
What do you expect to happen next? Looks like it''s just doing as per written.
您还只插入了一个节点.您需要为每个条目创建一个新节点.检查您的for循环范围.
You are also inserting only one node. You need to create a new node for each entry. Check the scope of your for-loop.
for(int i = 0 ; i < n ; i++)
{
node *p = new node();
cin >> p -> data ;
if(first == NULL) // Change to equality test instead of assignment
{
}
else
{
}
}
这篇关于链接列表我有小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文