Linked list creation, print, Reverse print, insertion at end(ALL by RECURSION)

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
struct node *create(struct node *head)
{
    struct node *newnode;
    int x;
    cout << "Enter the data";
    cin >> x;
    if (x == -1)
        return head;
    else
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->data = x;
        newnode->next = 0;
        head = newnode;
        head->next = create(head->next);
        return head;
    }
}
void print(struct node *head)
{
    if (head == 0)
        return;
    else
    {
        cout << head->data << " ";
        print(head->next);
    }
}
void rev_print(struct node *head)
{
    if (head == 0)
        return;
    else
    {
        rev_print(head->next);
        cout << head->data << " ";
    }
}
struct node *insert_end(struct node *head)
{
    int x;
    struct node *newnode;
    if (head == 0)
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        cout << "Enter data for end :";
        cin >> x;
        if (x == -1)
            return head;
        newnode->data = x;
        newnode->next = 0;
        head = newnode;
        return head;
    }
    else
        head->next = insert_end(head->next);
    return head;
}
struct node *reverse_list(struct node *head)
{
    struct node *prev, *current, *next;
    prev = 0next = 0;
    current = head;
    while (current != 0)
    {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head = prev;
    return head;
}
struct node *insert_beg(struct node *head)
{
    int x;
    struct node *newnode;
    newnode = (struct node *)malloc(sizeof(struct node));
    cout << "Enter value of ins. at beg:";
    cin >> x;
    if (x == -1)
        return head;
    newnode->data = x;
    if (head == 0)
    {
        newnode->next = 0;
        head = newnode;
    }
    else
    {
        newnode->next = head;
        head = newnode;
    }
    return head;
}
int main()
{
    struct node *head = 0;

    cout << "Creata list: \n";
    head = create(head);
    cout << "\nlist print: \n";
    print(head);

    cout << "\nlist rev_print: \n";
    rev_print(head);

    cout << "\nInsert at end  ";
    head = insert_end(head);
    cout << "\nlist after insertion at end will be: \n";
    print(head);

    head = reverse_list(head);
    cout << "\nReversed list is:\n";
    print(head);

    head = insert_beg(head);
    cout << "\nNew list is: \n";
    print(head);
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree