在 C++ 中使用类的链表
linked list using class in c++
我正在使用 class 尝试这个链表C++。我以前使用相同的方法实现树。但在下面的代码中,似乎linked_list中的下一个指针无法作为示例工作。主函数中的行已被注释,是主要问题所在。
#include<iostream>
#include<cstdio>
using namespace std;
class node{
node* next;
char data;
public:
node(char x){
data=x;
next=NULL;
}
node(){
data='~';
next=NULL;
}
node* get_next_node(){
return next;
}
char get_data(){
return data;
}
void set_data(char x){
data=x;
}
};
class Linked_List{
node *Head;
public:
Linked_List(char v){
Head= new node(v);
}
Linked_List(){
Head= new node();
}
void append(char v){
node *Cur;
for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
;
}
Cur= new node(v);
cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
}
node* get_Head(){
return Head;
}
void clear(){
Head=NULL;
}
void show(){
node *Cur;
for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
cout<<Cur->get_data()<<" ";
}
}
};
class Graph{
int vertices;
public:
Linked_List *arr;
Graph(int v){
vertices=v;
arr=new Linked_List[v];
}
void addEdge(char x, char y){
int i;
bool flag;
bool flag2=false;
for(i=0;i<vertices;i++){
if(arr[i].get_Head()->get_data()==x){
arr[i].append(y);
flag=true;
break;
}
else if(arr[i].get_Head()->get_data()=='~'){
flag=false;
break;
}
}
if(flag==false){
arr[i].get_Head()->set_data(x);
arr[i].append(y);
}
/*int j;
for( j=0;j<vertices;j++){
if(arr[j].get_Head()->get_data()==y){
flag2= true;
break;
}
if(arr[j].get_Head()->get_data()=='~'){
break;
}
}
if(flag2==false){
arr[j].get_Head()->set_data(y);
}*/
}
void show(){
for(int i=0;i<vertices;i++){
arr[i].show();
cout<< endl;
}
}
};
int main(){
int v;
char x,y;
cin>>v;
Graph bfs(v);
int edge;
cin>>edge;
for(int i=0;i<edge;i++){
cin>>x>>y;
bfs.addEdge(x,y);
}
bfs.show();
/*Linked_List ll('4');
ll.append('5');
ll.append('6');
char a=ll.get_Head()->get_data();
cout<<a;
a=ll.get_Head()->get_next_node()->get_data();
cout<<a;*/
char a=bfs.arr[0].get_Head()->get_data();
cout<<a<<endl;
if(bfs.arr[0].get_Head()->get_next_node()){ //this condition should be true if there
//is other values. but it's not working.
a=bfs.arr[0].get_Head()->get_next_node()->get_data();
}
cout<<a;
return 0;
}
/*
4
5
0 1
0 2
1 3
1 2
2 1
*/
在类 Linked_List
中,修改append()
:
void append(char v){
node *Cur;
for(Cur=Head;Cur->get_next_node()!=NULL;Cur=Cur->get_next_node()){
;
}
Cur->set_next_node(new node(v));
cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
}
在类 node
中添加set_next_node()
方法:
void set_next_node(node *n)
{
this->next=n;
}
在链表中,node
的每个next
都应包含下一个节点。但是你所做的是循环直到Cur
NULL
。如果这样做,则无法将最后一个节点的next
设置为新节点。
若要在当前节点之后添加新节点,请使用 set_next_node()
方法。
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 链表,将列表复制到另一个列表
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 对单向链表进行排序时出现运行时错误