Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The task is:
"It is required to develop a program that provides:
processing of the created
single-linked cyclic list with the following functions:

a) inserting nodes with information part = 1 in all positions of the list preceding nodes with negative information part
b) deleting all nodes of the list containing negative numbers in the information part;
c) calculation of the number of occurrences of the number specified from the keyboard in the information fields of nodes;
d) recursive deletion of all nodes in the list."

The program for queue that I don't know how to redo to a program for a circular singly linked list is in "What I have tried".

Please help me, I am a beginner in this stuff.


What I have tried:

#include <iostream>
#include <fstream>
using namespace std;

struct Node 
    {
        int data;
        Node* Next; 
    };

void Addition(Node** Head, int x) 
    {  
        Node* temp = new Node; 
        temp->data = x; 
        temp->Next = *Head; 
        *Head = temp; 
    }

void Insertion(Node* Head) 
    { 
        Node* current = Head; 
        while (current) 
        {
            if (current->data < 0) 
            {
                Node* newNode = new Node; 
                newNode->data = 1; 
                newNode->Next = current->Next; 
                current->Next = newNode; 
            }
            current = current->Next; 
        }
    }

void RemovalNegative(Node** Head) 
    { 
        Node* front = *Head; 
        Node* prev = NULL; 
        while (front) 
        {
            if (front->data < 0) 
            { 
                if (prev) 
                {
                    prev->Next = front->Next; 
                }
                else 
                {
                    *Head = front->Next; 
                }
                Node *tmp = front->Next; 
                delete front; 
                front = tmp; 
            }
            else 
            {
                prev = front; 
                front = front->Next; 
            }
        }
    }

int RepeatingNum(Node* Head, int x) 
    { 
        int count = 0;
        Node* temp = Head;
        while (temp) 
        { 
            if (temp->data == x) count++; 
            temp = temp->Next; 
        }
        return count; 
    }

void QueueDel(Node* Head) 
    { 
        if (Head) 
        {
            Node* temp = Head->Next; 
            delete Head; 
            QueueDel(temp); 
        }
        else return;
    }

void Display(Node* Head) 
    {
        Node* Next;
        Node* prev = NULL; 
        Node* temp = Head; 
        while (temp) 
        {
            Next = temp->Next; 
            temp->Next = prev; 
            prev = temp; 
            temp = Next; 
        }
        while (prev) 
        {
            cout << prev->data << " "; 
            prev = prev->Next; 
        }
    }

int main() 
    {
        Node* Head = NULL;
        int x = 0;
        int ch = 0;
        int kount = 0;
        cout << "Element: ";
        while (cin >> x) 
        {
            if (x == 0) break;
            cout << "Element: ";
            Addition(&Head, x); 
        }
        cout << "Select an option: \n";
        cout << "1 - Insert '1' in front of a negative number.\n";
        cout << "2 - Delete all negative numbers.\n";
        cout << "3 - Quantity of a given number among the elements of queue.\n";
        cin >> ch;
        switch (ch) 
        {
            case 1:
                Insertion(Head); 
                Display(Head);
                cout << "\n";
                break;
            case 2:
                RemovalNegative(&Head); 
                Display(Head); 
                cout << "\n";
                break;
            case 3:
                cout << "Write a number to count its repetition: \n";
                cin >> kount;
                cout << "Quantity: " << RepeatingNum(Head, kount); 
                cout << "\n";
                break;
        }
        main();
        QueueDel(Head); 
        return 0;
    }
Posted
Updated 21-Mar-23 5:18am
Comments
Richard MacCutchan 20-Mar-23 11:55am    
You need to explain what is wrong with the above code.

Read your assignment again:
Quote:
It is required to develop a program that provides:
processing of the created
single-linked cyclic list with the following functions
That isn't the same as "find a program that does something vaguely similar on the internet and get other people to make it work as required".

What you have done is the second description - and the code you have found is nothing like what your assignment requires of you.
To "redo this program" into something you can hand in basically means throwing it all away and starting again from scratch - so that's what I'm suggesting you do. That way, you will get what you need, and you will start to develop skill in development - which you will not do if others do your homework for you!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Since you obviously didn't write the source code yourself and don't seem to understand it, it doesn't make much sense to continue working on it. You also wanted a C++ solution. The shown program framework is completely C except for cout and new and uses neither containers nor classes. If you search a bit you will find better examples. How about this one?

Implement a Circular Linked List Data Structure in C++
https://www.delftstack.com/howto/cpp/circular-linked-list-in-cpp/
 
Share this answer
 
This is really not C++ code. It's C with streaming input and output. There isn't a class or method to be seen.

Also - it usually is recommended to NOT recurse on the main function. In this code, you actually want to just loop - NOT recurse.

This code is not too far away from what is needed for a singly-linked list. You have a pointer to the next item and a piece of data. You just have to adapt it so the tail of the list points to the head so it is circular. The code that manipulates the list needs a lot of work but at least you have a place to start from :
C++
struct Node 
{
    int   Data;
    Node* pNext; 
};
Next you should implement a function to add a node and another one to delete a node from the list. The operations you have to do are specializations of those two actions.

I am not going write the code for you. You need write this code yourself. You might find this tutorial helpful : Circular Linked List[^]. Their data structure is almost identical to yours.
 
Share this answer
 
#include <iostream>
#include <fstream>
using namespace std;

struct Node 
    {
        int data;
        Node* Next; 
    };

void Addition(Node** Head, int x) 
    {
        Node* temp = new Node;
        if (*Head == NULL) 
        {
            temp->data = x;
            (*Head) = temp;
            (*Head)->Next = *Head;
        }
        else 
        {
            temp->data = x;
            temp->Next = (*Head)->Next;
            (*Head)->Next = temp;
            *Head = temp;
        }
    }

void Insertion(Node* Head) 
    { 
        Node* current = Head->Next;
        do 
        {
            if (current->Next->data < 0 && current->data != 1) 
            {
                Node* p = new Node;
                p->data = 1;
                p->Next = current->Next;
                current->Next = p;
            }
            current = current->Next;
        } 
        while (current != Head->Next);
}

void RemovalNegative(Node** Head) 
    { 
        if ((*Head)->Next != NULL) 
        {
            Node* c1 = (*Head)->Next;
            Node* c2 = (*Head)->Next->Next;
            do 
            {
                if (c2->data < 0) 
                {
                    if (c2 == *Head) 
                    {
                    *Head = c1;
                    c1->Next = (*Head)->Next;
                    delete c2;
                    }
                    else 
                    {
                        Node* tmp = c2;
                        c2 = tmp->Next;
                        c1->Next = c2;
                        delete tmp;
                        c1 = c2;
                        c2 = c1->Next;
                    }
                }
                else 
                {
                    c1 = c1->Next;
                    c2 = c2->Next;
                }
            } 
            while (c1 != *Head);
            if ((*Head)->Next->data < 0) 
            {
                Node* tmp = (*Head)->Next;
                (*Head)->Next = (*Head)->Next->Next;
                delete tmp;
            }
        }
        else return;
    }

int RepeatingNum(Node* Head, int x) 
    { 
        int count = 0;
        Node* temp = Head->Next;
        do 
        {
            if (temp->data == x) count++;
            temp = temp->Next;
        } 
        while (temp != Head->Next);
        return count;
    }

void CyclicListDel(Node* Head)
    {
        Node* newNode = Head->Next;
        if (newNode != Head->Next) 
        {
            Node* temp = newNode->Next;
            delete newNode;
            CyclicListDel(temp);
        }
        else return;
    }

void Display(Node* Head) 
    {
        Node* newNode = Head->Next;
        do 
        {
            cout << newNode->data << " ";
            newNode = newNode->Next;
        } 
        while (newNode != Head->Next);
    }

int main() 
    {
        Node* Head = NULL;
        int x = 0;
        int ch = 0;
        int kount = 0;
        cout << "Element: ";
        while (cin >> x) 
        {
            if (x == 0) break;
            cout << "Element: ";
            Addition(&Head, x); 
        }
        cout << "Select an option: \n";
        cout << "1 - Insert '1' in front of a negative number.\n";
        cout << "2 - Delete all negative numbers.\n";
        cout << "3 - Quantity of a given number among the elements of cyclic list.\n";
        cin >> ch;
        switch (ch) 
        {
            case 1:
                Insertion(Head); 
                Display(Head);
                cout << "\n";
                break;
            case 2:
                RemovalNegative(&Head); 
                Display(Head); 
                cout << "\n";
                break;
            case 3:
                cout << "Write a number to count its repetition: \n";
                cin >> kount;
                cout << "Quantity: " << RepeatingNum(Head, kount); 
                cout << "\n";
                break;
        }
        main();
        CyclicListDel(Head); 
        return 0;
    }
 
Share this answer
 
Comments
jeron1 21-Mar-23 11:27am    
If you have an update, then update the original post, and perhaps explain what the update does. You can use the green 'Improve Question' option.
Rick York 21-Mar-23 12:54pm    
You are still recursing on main which results in starting all over again with a new list. You also implemented your specializations but not the basic operations. This not how a linked list should be built.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900