C++ c++;链表问题
//上面的节点hC++ c++;链表问题,c++,linked-list,nodes,C++,Linked List,Nodes,//上面的节点h 我需要创建一个链表,在链表前面插入一个节点作为程序,当然还需要打印出来。由于某种原因,我无法将节点插入列表的前面,并且在尝试打印时遇到infinte循环。它确实起了作用,但我不知道具体是什么。请帮忙 在插入方法中,当已有1个元素时tmp设置为head,然后tmp->setNext(head)将创建对自身的引用。这就是打印方法中出现无限循环的原因。请尝试以下插入代码 //node.h //#ifndef NODE_H #define NODE_H //node class cl
我需要创建一个链表,在链表前面插入一个节点作为程序,当然还需要打印出来。由于某种原因,我无法将节点插入列表的前面,并且在尝试打印时遇到infinte循环。它确实起了作用,但我不知道具体是什么。请帮忙 在插入方法中,当已有1个元素时
tmp
设置为head
,然后tmp->setNext(head)代码>将创建对自身的引用。这就是打印方法中出现无限循环的原因。请尝试以下插入代码
//node.h
//#ifndef NODE_H
#define NODE_H
//node class
class node {
int epnum;
float salary;
node* next;
public:
node()
{} //null constructor
//stores argument passed in func.
void SetData(float _salary, int _epnum){
salary = _salary;
epnum = _epnum;
}
//stores in next the address of the next node
void setNext (node* anext){
next = anext;
}
//returns epnum stored
int Epnum(){
return epnum;
}
float Salary(){
return salary;}
//returns addres of next node
node* Next(){
return next;
}
};
tmp->next = head;
我还要注意的是,在youprint方法中,对于包含1个元素的列表,没有角点。您的循环将完美地处理此情况。如果省略列表分支中的一个节点,将得到相同的结果
void List::Print()
{
//温度指针
节点*tmp=头部;
//无节点
if(tmp==NULL)
{
cout在列表::Insert
中,您有:
void List::Print()
{
// Temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
{
cout << "EMPTY" << endl;
return;
}
// Parse and print the list
do
{
cout << tmp->Epnum();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
接
node *tmp = head;
因此,对象中有循环链接。它的next
指向自身。这将导致打印函数中的无限循环
您需要的非常简单:
void List::Insert(float-sal,int-en)
{
//创建一个新节点
node*newNode=newNode();
新建节点->设置数据(sal,en);
新建节点->设置下一步(空);
//将“点”旁边的新节点设置为“头”
新建节点->设置下一步(头部);
//将新头设置为新节点
头=新节点;
//创建一个临时指针
//节点*tmp=头部;
/*如果(tmp!=NULL)
{
//列表中已存在节点
//解析到列表的末尾
/*while(tmp->Next()!=NULL)
{
tmp=tmp->Next();
}
//将最后一个节点指向新节点
tmp->setNext(头部);
}
其他的
{
//列表中的第一个节点
头=新节点;
}*/
}
/**
*从列表中删除节点
*/
作废列表::删除(浮动工资,整数数据)
{
//创建一个临时指针
节点*tmp=头部;
//无节点
if(tmp==NULL)
返回;
//列表的最后一个节点
如果(tmp->Next()==NULL)
{
删除tmp;
head=NULL;
}
其他的
{
//通过节点进行解析
节点*prev;
做
{
如果(tmp->Epnum()==数据和&tmp->Salary()==薪资)
打破
prev=tmp;
tmp=tmp->Next();
}while(tmp!=NULL);
//调整指针
prev->setNext(tmp->Next());
//删除当前节点
删除tmp;
}
}
/**
*打印列表的内容
*/
作废列表::打印()
{
//温度指针
节点*tmp=头部;
//无节点
if(tmp==NULL)
{
cout-Epnum();
很好,我的删除功能现在不起作用了。它只在最后一个节点起作用,但我认为第一个节点或中间的节点不起作用
void List::Insert(float sal, int en)
{
// Create a new node
node* newNode = new node();
newNode->SetData(sal, en);
newNode->setNext(head);
head = newNode;
}
void List::Print()
{
// Temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
{
cout << "EMPTY" << endl;
return;
}
// Parse and print the list
do
{
cout << tmp->Epnum();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
node *tmp = head;
tmp->next = head;
void List::Insert(float sal, int en)
{
// Create a new node
node* newNode = new node();
newNode->SetData(sal, en);
newNode->setNext(head);
head = newNode;
}
void List::Insert(float sal, int en)
{
// Create a new node
node* newNode = new node();
newNode->SetData(sal, en);
newNode->setNext(NULL);
//set the newNode next to point to head
newNode->setNext(head);
//set the new head as the newNode
head = newNode;
// Create a temp pointer
//node *tmp = head;
/*if ( tmp != NULL )
{
// Nodes already present in the list
// Parse to end of list
/*while ( tmp->Next() != NULL )
{
tmp = tmp->Next();
}
// Point the last node to the new node
tmp->setNext(head);
}
else
{
// First node in the list
head = newNode;
}*/
}
/**
* Delete a node from the list
*/
void List::Delete(float salary, int data)
{
// Create a temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL )
{
delete tmp;
head = NULL;
}
else
{
// Parse thru the nodes
node *prev;
do
{
if ( tmp->Epnum() == data && tmp->Salary()== salary )
break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->setNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
/**
* Print the contents of the list
*/
void List::Print()
{
// Temp pointer
node *tmp = head;
// No nodes
if ( tmp == NULL )
{
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL )
{
cout << tmp->Salary() + tmp->Epnum();
cout << " --> ";
cout << "NULL" << endl;
}
else
{
// Parse and print the list
do
{
cout << tmp->Epnum();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}