take the following code and give details of what each line o

take the following code and give details of what each line of code is doing

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
    int info;
    struct node *next;
}*start;


/*
* Class Declaration
*/
class single_llist
{
    public:
        node* create_node(int);
        void insert_begin();
        void insert_pos();
        void insert_last();
        void delete_pos();
        void sort();
        void search();
        void update();
        void reverse();
        void display();
        single_llist()
        {
            start = NULL;
        }
};


/*
* Main :contains menu
*/
main()
{
    int choice, nodes, element, position, i;
    single_llist sl;
    start = NULL;
    while (1)
    {
        cout<<endl<<\"---------------------------------\"<<endl;
        cout<<endl<<\"Operations on singly linked list\"<<endl;
        cout<<endl<<\"---------------------------------\"<<endl;
        cout<<\"1.Insert Node at beginning\"<<endl;
        cout<<\"2.Insert node at last\"<<endl;
        cout<<\"3.Display Linked List\"<<endl;
        cout<<\"4.Exit \"<<endl;
        cout<<\"Enter your choice : \";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<\"Inserting Node at Beginning: \"<<endl;
            sl.insert_begin();
            cout<<endl;
            break;
        case 2:
            cout<<\"Inserting Node at Last: \"<<endl;
            sl.insert_last();
            cout<<endl;
            break;
        case 3:
            cout<<\"Display elements of link list\"<<endl;
            sl.display();
            cout<<endl;
            break;
        case 4:
            cout<<\"Exiting...\"<<endl;
            exit(1);
            break;
        default:
            cout<<\"Wrong choice\"<<endl;
        }
    }
}


/*
* Creating Node
*/
node *single_llist::create_node(int value)
{
    struct node *temp, *s;
    temp = new(struct node);
    if (temp == N ULL)
    {
        cout<<\"Memory not allocated \"<<endl;
        return 0;
    }
    else
    {
        temp->info = value;
        temp->next = NULL;
        return temp;
    }
}


/*
* Inserting element in beginning
*/
void single_llist::insert_begin()
{
    int value;
    cout<<\"Enter the value to be inserted: \";
    cin>>value;
    struct node *temp, *p;
    temp = create_node(value);
    if (start == NULL)
    {
        start = temp;
        start->next = NULL;
    }
    else
    {
        p = start;
        start = temp;
        start->next = p;
    }
    cout<<\"Element Inserted at beginning\"<<endl;
}


/*
* Inserting Node at last
*/
void single_llist::insert_last()
{
    int value;
    cout<<\"Enter the value to be inserted: \";
    cin>>value;
    struct node *temp, *s;
    temp = create_node(value);
    s = start;
    while (s->next != NULL)
    {
        s = s->next;
    }
    temp->next = NULL;
    s->next = temp;
    cout<<\"Element Inserted at last\"<<endl;
}

/*
* Display Elements of a link list
*/
void single_llist::display()
{
    struct node *temp;
    if (start == NULL)
    {
        cout<<\"The List is Empty\"<<endl;
        return;
    }
    temp = start;
    cout<<\"Elements of list are: \"<<endl;
    while (temp != NULL)
    {
        cout<<temp->info<<\"->\";
        temp = temp->next;
    }
    cout<<\"NULL\"<<endl;
}

Solution

ANSWER ::

ASSMUING THE TOTAL LINE IN CODE ARE 158*(INCLUDING COMMENTS)

SO...

PROGRAM STARTS WITH SPECIFING HEADER FILES AND GLOBAL VARIABLE DECLARATION

THEN Node Declaration USING STRUCTURES,

THEN WE WANT TO DECLARE THE CLASS USING SINGLE LINKED LIST AND THE PROTOTYPE IS PUBLIC

IN THE PUBLIC WE WANT TO CREATE THE NODE AS INTEGER DATATYPE

THE NODE CONSIST OF insert_begin();
insert_pos();-> INDICATES THE CURRENT POSSITION
insert_last();->INSERT AT LAST
delete_pos();->TO DELETE THE NUMBER AT CURRENT POSITION
sort();->START THE SORTING
search();->USED TO SEARCH
update();->UPDATE THE NODE
reverse();->REVERSE NUMBERS
display();->TO DISPLAY THE NODE
        single_llist()->FIFO

WE ARE STARTING TAKING NULL VALUE

AND THEN TAKE USER DEFIND FUNCTION MAIN()

CREATE THE DATATYPES choice, nodes, element, position, i

OBJECT CREATION OF SLL IS SL

USING WHILE LOOP THE NODE INSERT AT THE BEGINING OR ENDING STAGE

WE ARE USING SWITCH CASES TO PERFORM SLL ON NODE

NODE CREATION AS

node *single_llist::create_node(int value)

ALSO DECLARE THE TEMP IS TAKEN AS NULL VALUE

INSERT THE ELEMENT AT THE BEGINING

USING IF CONDITION WE CAN INSERT THE NODE AT THE BEGINING

struct node *temp, *p;
    temp = create_node(value);
    if (start == NULL)
    {
        start = temp;
        start->next = NULL;
    }
    else
    {
        p = start;
        start = temp;
        start->next = p;

AND THEN INSERT THE NODE AT THE LAST

struct node *temp, *s;
    temp = create_node(value);
    s = start;
    while (s->next != NULL)
    {
        s = s->next;
    }
    temp->next = NULL;
    s->next = temp;

USING WHILE LOOP WE ARE INSERTING THE NODE AT LAST

USING THIS BELLOW CODE WE CAN SEARCH THE ELEMENTS IN THE NODE

struct node *temp;
    if (start == NULL)
    {
        cout<<\"The List is Empty\"<<endl;
        return;
    }
    temp = start;
    cout<<\"Elements of list are: \"<<endl;
    while (temp != NULL)
    {
        cout<<temp->info<<\"->\";
        temp = temp->next;
    }
    cout<<\"NULL\"<<endl;
}

take the following code and give details of what each line of code is doing #include<iostream> #include<cstdio> #include<cstdlib> using namesp
take the following code and give details of what each line of code is doing #include<iostream> #include<cstdio> #include<cstdlib> using namesp
take the following code and give details of what each line of code is doing #include<iostream> #include<cstdio> #include<cstdlib> using namesp
take the following code and give details of what each line of code is doing #include<iostream> #include<cstdio> #include<cstdlib> using namesp
take the following code and give details of what each line of code is doing #include<iostream> #include<cstdio> #include<cstdlib> using namesp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site