
请教以下关于C++中单链表的输出问题1
要求:调用对象l中的display()方法来实现单链表的数据域输出编译环境VC++6.0问题:应该如何修改现在的程序?程序如下:#include<iostream.h>#...
要求:调用对象l中的display()方法来实现单链表的数据域输出
编译环境VC++6.0
问题:
应该如何修改现在的程序?
程序如下:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int num;
struct Node *next;
};
class LinkList
{
public:
LinkList();
void Createlist();
void Insertvalue(struct Node *);
void Deletevalue(struct Node *);
void Display();
//private:
int Nodenum;
};
LinkList::LinkList()
{
Nodenum=0;
}
void LinkList::Createlist()
{
int i;
int n; //设置节点数
cout<<"please input the number of the node:"<<endl;
cin>>n;
struct Node *List,*Temp,*New;
List=(struct Node *)malloc(sizeof(struct Node)); //创建头节点并赋值
List->num=0;
List->next=NULL;
Nodenum++;
Temp=List;
for(i=0;i<n;i++) //for循环创建n个新节点并赋值
{
New=(struct Node *)malloc(sizeof(struct Node));
New->next=NULL;
New->num=i;
Temp->next=New; //连接节点
Temp=Temp->next;
Nodenum++;
cout<<"the "<<i+1<<"th Node has been created"<<endl;
}
}
void LinkList::Insertvalue(struct Node * l)
{
struct Node *p,*New;
p=l;
int i,j,e;
cout<<"please enter the position where you want to insert the number:"<<endl;
cin>>i;
cout<<"please enter the value of number you want to insert:"<<endl;
cin>>e;
for(j=0;j<i-1;j++)
p=p->next;
New=(struct Node *)malloc(sizeof(struct Node));
New->num=e;
New->next=p->next;
p->next=New;
Nodenum++;
cout<<"Insert completed"<<endl;
}
void LinkList::Deletevalue(struct Node *l)
{
struct Node *p1,*p2;
p1=l;
int i,j;
cout<<"please enter the position where you want to delete the number:"<<endl;
cin>>i;
for(j=1;j<i-1;j++)
p1=p1->next;
p2=p1->next;
p1->next=p2->next;
free(p2);
Nodenum--;
cout<<"Delete completed!"<<endl;
}
void LinkList::Display()
{
struct Node *p;
int i;
p=List;
for(i=0;i<Nodenum;i++)
{
cout<<p->num;
p=p->next;
}
}
void main()
{
LinkList l;
l.Createlist();
l.Display();
getchar();
}
提示:
--------------------Configuration: cpp1 - Win32 Debug--------------------
Compiling...
cpp1.cpp
I:\programme\数据结构\homework\2\1.0\cpp1.cpp(106) : error C2065: 'List' : undeclared identifier
I:\programme\数据结构\homework\2\1.0\cpp1.cpp(106) : error C2440: '=' : cannot convert from 'int *' to 'struct Node *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
cpp1.exe - 2 error(s), 0 warning(s) 展开
编译环境VC++6.0
问题:
应该如何修改现在的程序?
程序如下:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int num;
struct Node *next;
};
class LinkList
{
public:
LinkList();
void Createlist();
void Insertvalue(struct Node *);
void Deletevalue(struct Node *);
void Display();
//private:
int Nodenum;
};
LinkList::LinkList()
{
Nodenum=0;
}
void LinkList::Createlist()
{
int i;
int n; //设置节点数
cout<<"please input the number of the node:"<<endl;
cin>>n;
struct Node *List,*Temp,*New;
List=(struct Node *)malloc(sizeof(struct Node)); //创建头节点并赋值
List->num=0;
List->next=NULL;
Nodenum++;
Temp=List;
for(i=0;i<n;i++) //for循环创建n个新节点并赋值
{
New=(struct Node *)malloc(sizeof(struct Node));
New->next=NULL;
New->num=i;
Temp->next=New; //连接节点
Temp=Temp->next;
Nodenum++;
cout<<"the "<<i+1<<"th Node has been created"<<endl;
}
}
void LinkList::Insertvalue(struct Node * l)
{
struct Node *p,*New;
p=l;
int i,j,e;
cout<<"please enter the position where you want to insert the number:"<<endl;
cin>>i;
cout<<"please enter the value of number you want to insert:"<<endl;
cin>>e;
for(j=0;j<i-1;j++)
p=p->next;
New=(struct Node *)malloc(sizeof(struct Node));
New->num=e;
New->next=p->next;
p->next=New;
Nodenum++;
cout<<"Insert completed"<<endl;
}
void LinkList::Deletevalue(struct Node *l)
{
struct Node *p1,*p2;
p1=l;
int i,j;
cout<<"please enter the position where you want to delete the number:"<<endl;
cin>>i;
for(j=1;j<i-1;j++)
p1=p1->next;
p2=p1->next;
p1->next=p2->next;
free(p2);
Nodenum--;
cout<<"Delete completed!"<<endl;
}
void LinkList::Display()
{
struct Node *p;
int i;
p=List;
for(i=0;i<Nodenum;i++)
{
cout<<p->num;
p=p->next;
}
}
void main()
{
LinkList l;
l.Createlist();
l.Display();
getchar();
}
提示:
--------------------Configuration: cpp1 - Win32 Debug--------------------
Compiling...
cpp1.cpp
I:\programme\数据结构\homework\2\1.0\cpp1.cpp(106) : error C2065: 'List' : undeclared identifier
I:\programme\数据结构\homework\2\1.0\cpp1.cpp(106) : error C2440: '=' : cannot convert from 'int *' to 'struct Node *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
cpp1.exe - 2 error(s), 0 warning(s) 展开
2个回答
展开全部
display函数中List没有声明,更不能把值赋值给p。
应该在display中传一个参数吧,比如传一个结构体指针。
应该在display中传一个参数吧,比如传一个结构体指针。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class LinkList
{
public:
LinkList();
void Createlist();
void Insertvalue(struct Node *);
void Deletevalue(struct Node *);
void Display();
//private:
Node * List;//加上这句试试
int Nodenum;
};
{
public:
LinkList();
void Createlist();
void Insertvalue(struct Node *);
void Deletevalue(struct Node *);
void Display();
//private:
Node * List;//加上这句试试
int Nodenum;
};
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询