Skip to main content
Tweeted twitter.com/StackCodeReview/status/708225918330912768
added 33 characters in body
Source Link

PleaseHow can I improve this code
Please tell proper indentation and usage of camel-case.

Please tell proper indentation and usage of camel-case.

How can I improve this code
Please tell proper indentation and usage of camel-case.

Source Link

Singly Linked list implementation in C++

Please tell proper indentation and usage of camel-case.

#include <iostream>
using std::cout;
struct Node
{
    int data;
    Node* next;
};
Node* Create(int data){
    Node* root=new Node();
    root->data=data;
    root->next=NULL;
    Node* head=root;
    return head;    
}
void push_back(int data,Node* head){
    if(!head->next)
    {
        Node* newNode=new Node();
        newNode->data=data;
        newNode->next=NULL;
        head->next=newNode;
    }   
    else 
        push_back(data,head->next);
}
Node* push_front(int data,Node* head){
    Node* newNode=new Node();
    newNode->data=data;
    newNode->next=head;
    head=newNode;
    return head;
}
void pull_back(Node* head){
    Node* temp=head;
    if(!temp->next)
        delete temp;
    else
        pull_back(temp->next);
}
Node* pull_front(Node* head){
    Node* temp=head->next;
    delete head;
    Node* NewHead=temp;
    return NewHead;
}
void print(Node* head){
    while(head)
        cout<<head->data<<" ",
        head=head->next;
    cout<<"\n";
}
Node* Reverse(Node* head)
{
    Node* prev   = NULL;
    Node* current = head;
    Node* next;
    while (current != NULL)
    {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    head = prev;
    return head;
}
int Search(int data,Node* head,int count=1){
    if(head->data==data)
        return count;
    if(!head->next)
        return -1;
    return Search(data,head->next,count+1);
}
Node* MakeCircular(Node* head)
{
    Node* temp=head;
    while(!temp->next)
        temp=temp->next;
    temp->next=head;
    return head;
}
bool DetectCircle(Node* head){
    //using floyd's cycle
    Node* fast=head;
    Node* slow=head;
    if(!(slow && slow->next))return false;
    while(fast && fast->next){
        slow=slow->next;
        fast=fast->next->next;
        if(slow==fast)
            return true;
    }
    return false;
}
int main(int argc, char const *argv[])
{
    Node* head=Create(12);
    push_back(32,head);
    push_back(123,head);
    head=push_front(-21,head);
    print(head);
    head=Reverse(head);
    print(head);
    return 0;
}