Home > OS >  How to increase value at Nth position in Circularly linked lists?
How to increase value at Nth position in Circularly linked lists?

Time:03-26

I am fairly new to programming and I am working on a project that involves shifting nodes. I am currently stuck and do not know what to do. I am making a program to familiarize myself with circular linked lists, but I don't think my understanding is there yet. How can I get a node at a particular position denoted by user input and increase its value by one? To better explain:

The user input is supposed to look like this..with the numbers by it being an example:

nodes 8 place 3

The board originally would look like this: 1 1 1 1 1 1 1 1

BUT, Given that user input (above) it would become: 1 1 1 2 1 1 1 1

here is my code...or my attempt:

#include <iostream>

struct Node {
    int data;
    struct Node* next;
   // struct Node* back;
};
class LinkedList {
private:
    Node* head;
public:
    LinkedList() 
    {
      head = NULL;
    }

    bool isempty()
    { // will revisit
    }

    void add(int new_key) 
    {
      Node* newNode;
      newNode = new Node();
      newNode->data = new_key;
      if (head == NULL) 
      {
        head = newNode;
        newNode->next = head;
        return;
      }
      Node* current = head;
      while (current->next != head) 
      {
        current = current->next;
      }
      newNode->next = head;
      head = newNode;
      current->next = head;
    }

  
    int place_node() //confused..here
    {
      Node* current = head;
      int count, index = 0;
      while(current != nullptr)
      {
        if (count == index)
        {
          return(current->data);
        }
        count  ;
        current = current->next;
      }
    }

    void remove(int value)
    {
      Node* current = head;
      while(current != NULL && current->data != value)
        {
          current = current->next;
        }
      if (current == NULL)
      {
        return;
      }
      else
      {
        current->data = NULL;
      }
    }


    void print()   
    {
        Node* current = head;
       if (head != nullptr)
       {
        do 
         {
           std::cout << current->data << " ";
           current = current->next;
         }
        while (current != head);
       }
    }
};

int main() 
{
  LinkedList link_one;
  int nodes;
  std::cout << "nodes ";
  std::cin >> nodes;
  for (int index = 0; index < nodes; index  ){
    link_one.add(1);
  }
  link_one.print();
  std::cout << std::endl;
  // link_one.remove(1); 
 // link_one.remove(1);
 //link_one.print();
}

CodePudding user response:

try traverse

actually there is no need to write a answer, but...I am a newcomer :)

your function place_node should add a parameter:

int place_node(int idx)

then...traverse from your head node for "idx" times, modify the value of current node, that's it.

here's the complete code for function place_node to achieve your goal:

int place_node(int idx) //no need to return
{
  Node* current = head;
  int count, index = 0;
  for(int i=0;i<idx;  i) current = current->next;
  current->data =1;
  return 1; // no need to return
}
  • Related