【发布时间】:2018-05-24 21:25:50
【问题描述】:
我改进了我的简单应用程序,它允许您使用链接列表。每个节点都有一个 char 值作为数据和一个 int 计数,用于计算每个数据的出现次数。 我需要一个函数来复制现有列表并将其粘贴到其他地方,最后返回粘贴列表的第一个节点的地址。我怎样才能做到这一点? 这是我的全部代码:
#include <iostream>
using namespace std;
struct Snode //Snode class defines a node in a list
{
char data;
int count = 1;
Snode *next = NULL;
Snode(char a) : data(a) {}
};
class set//set class defines the list
{
private:
Snode *head;
public:
set() : head(NULL)//constructor method of the list
{
Snode *temp = head;
while (temp != NULL)
{
Snode *next = temp->next;
delete temp;
temp = next;
}
head = NULL;
}
~set()//destructor method of the list
{
Snode *temp = head;
while (temp != NULL)
{
head = head->next;
delete temp;
}
}
bool isAvailable(char value)//checks if the node is already in the list or not
{
Snode *temp = head;
while (temp != NULL)//untill the end of the list
{
if (temp->data == value)//if a similar node is found
return true;
else//if temp is not equal check the next node
temp = temp->next;
}
return false;//if no node is found return false
}
bool isFirst(char value)//checks if the node is the first node or not
{
return(head->data == value);//if it equals to head, it's the first node
}
bool isLast(char value)//checks if the node is the last node or not
{
Snode *last ;
return(last->next = NULL);//if its next pointer points the null pointer, it's the last node in the list
}
void display()//showing all the nodes
{
//creating a variable node to travers available nodes
Snode *temp = head;
while (temp != NULL)//travers nodes untill the end of the list
{
cout << temp->data << " " << temp->count << "\n"; //output format
temp = temp->next;//setting the next node as the variable
}
}
void insert(char value)//inserts a new node
{
if (head == NULL)//if the list is empty
{
//create a new node, set its count to 1 and set it as head of the list
Snode *temp = new Snode(value);
temp->count = 1;
head = temp;
}
else//is the list is'nt empty
{
if (isAvailable(value))//if the value is already available
{
//find the existing node and increase its count by 1
Snode *temp = head;
while (temp->data != value)//check the list to the end
temp = temp->next;
temp->count += 1;
}
else//if there's not an existing node with the given value
{
//create a new node with a count of 1 at the end of the list by setting *next equal to NULL
Snode *temp = new Snode(value);
temp->count = 1;
temp->next = NULL;
}
}
}
int count(char value)//counts the occurrence of a character value by reading the same node's count
{
Snode *temp = head;
while (temp != NULL)//travers nodes untill the end of the list
{
if(temp->data==value)
{
cout<<temp->count;
}
else
{
cout<<"This character is not in the list";
}
}
return temp->count;
}
void deleteFirst(char value)//delete the first node in the list
{
Snode *temp = head;//create a new node including head's data
head = head->next;//setting the second node as head
delete temp;//deleting the first node
}
void deleteLast(char value)//deleting the last node in the list
{
Snode *current= new head;//creating two new nodes and starting the travers from the first node
Snode *previous=new Snode();
while(current->next!=NULL)//continue traversing untill the end of the list
{
previous=current;//moving current and previous nodes to the right in order to check the next nodes
current=current->next;
if(current->next == NULL)//if the list is finished
{
previous->next = NULL;//pointing the previous node's next as NULL, instead of the last node
delete current;//deleting the last node
}
}
}
char remove(char value, struct Snode *temp)//removing a node from the list
{
if(!isAvailable(value))//if there's no node with the given data
{
cout<<"Not available";
return NULL;
}
else//if there is already a node with the same data
{
if(temp->count == 1)//if there's a node with one time occurrence
{
if(isFirst(value)//if it's equal to the first node
{
deleteFirst(value);
}
else if(isLast(value))//if it's equal to the last node
{
deleteLast(value);
}
else//if it's in the middle, neither first nor last
{
Snode *current = head;//traversing all nodes
Snode *previous=new Snode();
while(current->next=NULL)
{
if(current->data==value)
{
previous->next = current->next;
delete current;
}
previous=current;
current=current->next;
}
}
}
else if(temp->count > 1)
{
temp->count--;//decrease the count
}
}
}
};
int main()
{
//defining a mySet as a "set" type
set mySet;
//adding values to create nodes
mySet.insert('c');
mySet.insert('a');
mySet.insert('a');
mySet.insert('c');
mySet.insert('c');
//displaying nodes through "value count" format
mySet.display();
return 0;
}
【问题讨论】:
-
在你的构造函数中,你首先将
head初始化为NULL,然后然后,如果不是NULL,你遍历它并删除它之前包含的所有内容将其设置为NULL。看起来很绕。 -
@alterigel 所以我的析构函数会是这样的: ~set() { if(head != NULL) { Snode *temp = head; while (temp != NULL) { head = head->next;删除温度; } } } 正确的? @RemyLebeau 他们没有做同样的事情???
set分配值但~set遍历+删除节点。 -
@Nitwit 不,您的
~set()代码错误。您只分配一次temp,然后在每次迭代中分配delete相同的Snode。您根本没有通过列表推进temp。set()中的代码是~set()应该做的,例如:set() : head(NULL) {} ~set() { Snode *temp = head; while (temp) { Snode *next = temp->next; delete temp; temp = next; } }
标签: c++ data-structures linked-list singly-linked-list