0

I've implemented code for the circular linked list but no output is being shown on output window except the following:

Time to print the List

And this is the message my compiler is giving:

-------------- Build: Debug in Circular_Linked_List (compiler: GNU GCC Compiler)---------------

Target is up to date. Nothing to be done (all items are up-to-date).

-------------- Run: Debug in Circular_Linked_List (compiler: GNU GCC Compiler)---------------

Checking for existence: C:\Users\hp\Desktop\CPP Programming\Circular_Linked_List\bin\Debug\Circular_Linked_List.exe Executing: "C:\Program Files (x86)\CodeBlocks/cb_console_runner.exe" "C:\Users\hp\Desktop\CPP Programming\Circular_Linked_List\bin\Debug\Circular_Linked_List.exe" (in C:\Users\hp\Desktop\CPP Programming\Circular_Linked_List.)

This is the following code which I'm trying to run and build on code::blocks17.12

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
    int data;
    Node *next;
};
void push(Node* head,int data)
{
    Node *new_node = new Node();
    Node *temp = head;
    new_node->data = data;
    new_node->next = head;
    if(head!=NULL)
    {
        while(temp->next!=head)
        {
            temp = temp->next;
        }
        temp->next = new_node;
    }
    else{
        new_node->next = new_node;
    }
    head = new_node;
}
void printList(Node* head)
{
    Node *temp = head;
    if(head!=NULL){
        while(temp->next!=head)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }
}
else{
    return;
}
}
int main()
{
    Node *head = NULL;

    push(head,12);
    push(head,14);
    push(head,15);
    push(head,16);

    cout<<"Time to print the List\n";

    printList(head);
    return 0;
}

  • 3
    In `push` , `head` needs to be passed by reference, if you want to modify it. – rafix07 Jan 13 '20 at 12:19
  • 4
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Lightness Races in Orbit Jan 13 '20 at 12:20
  • 3
    Unrelated to your problem, but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) as well as [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Jan 13 '20 at 12:20

1 Answers1

1

You need to change your push function and printlist function as follows:

void push (Node * &head, int data)
{
  Node *new_node = new Node ();
  Node *temp = head;
  new_node->data = data;
  new_node->next = head;
  if (head != NULL)
    {
      while (temp->next != head)
    {
      temp = temp->next;
    }
      temp->next = new_node;
    }
  else
    new_node->next = new_node;
  head = new_node;
}

void printList (Node * head)
{
  Node *temp = head;
  if (head != NULL)
    {
      while (temp->next != head)
    {
      cout << temp->data << " " << std::endl;
      temp = temp->next;
    }
    cout << temp->data << " " << std::endl;
    }
  else
    return;
}

Because in push function you are modifying the pointer itself, and pointer is passed by value, it will be unchanged, once the function returns. To change pointer inside function, You need to pass it by reference. You can find useful information about passing a pointer by reference here and here

Also in printlist function the node for which temp->next!=head is true, will not be printed. So you need to print it separately.

Roya Ghasemzadeh
  • 663
  • 5
  • 15