I toned up my simple app which allows you to work with linked lists. each node has a char value as data and an int count which counts the occurrence of each data. I need a function to copy the existing list and paste it somewhere else and finally return the address of the first node of the pasted list. how can I do that? here's my whole code:
#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;
}