Question

Write a function to implement linked list consisting of five nodes. Store the data in the...

  1. Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming

George, Paul, Ross, Joe, Elaine, Robert1

  1. Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have to write a function for insertion operation, in C++ programming).

                               Davis1, Davis2, Davis3

  1. Delete a node (data and the node together) from the linked list that you implemented in problem 2. (You have to write a function for delete operation, in C++ Programming).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

#include<iostream>

using namespace std;

// Node Declaration

struct node
{

    string data;

    struct node *next;

}*start;

//class declaration

class single_llist
{
    public:

        node* create_node(string);
        void insertFirst(string);
        void insert_pos(string);
        void delete_pos();
        void display();
        single_llist()
        {
            start = NULL;
        }
};

int main()

{
    single_llist s1;

    int choice;

    single_llist sl;

    start = NULL;

    cout<<"Inserting nodes initially";

    sl.insertFirst("Robert1");
    sl.insertFirst("Joe");
    sl.insertFirst("Ross");
    sl.insertFirst("Paul");
    sl.insertFirst("George");
    sl.display();

    s1.insert_pos("Davis3");
    s1.display();
    s1.insert_pos("Davis2");
    s1.display();
    s1.insert_pos("Davis1");
    sl.display();

    sl.delete_pos();
    sl.display();

   return 0;

}

/*

* Creating Node

*/

node *single_llist::create_node(string value)

{

    struct node *temp, *s;

    temp = new(struct node);

    if (temp == NULL)

    {

        cout<<"Memory not allocated "<<endl;

        return 0;

    }

    else

    {

        temp->data = value;

        temp->next = NULL;

        return temp;

    }

}

void single_llist::insertFirst(string 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<<endl<<"Element "<<value<<" Inserted "<<endl;

}

/*

* Insertion of node at a given position

*/

void single_llist::insert_pos(string value)

{

    int pos, counter = 0;

    struct node *temp, *s, *ptr;

    temp = create_node(value);

    cout<<"\nEnter the position at which node to be inserted: ";

    cin>>pos;

    int i;

    s = start;

    while (s != NULL)

    {

        s = s->next;

        counter++;

    }

    if (pos == 1)

    {

        if (start == NULL)

        {

            start = temp;

            start->next = NULL;

        }

        else

        {

            ptr = start;

            start = temp;

            start->next = ptr;

        }

    }

    else if (pos > 1 && pos <= counter)

    {

        s = start;

        for (i = 1; i < pos; i++)

        {

            ptr = s;

            s = s->next;

        }

        ptr->next = temp;

        temp->next = s;

    }

    else

    {

        cout<<"Position out of range"<<endl;

    }

}

/*

* Delete element at a given position

*/

void single_llist::delete_pos()

{

    int pos, i, counter = 0;

    if (start == NULL)

    {

        cout<<"List is empty"<<endl;

        return;

    }

    cout<<"\nEnter the position of value to be deleted: ";

    cin>>pos;

    struct node *s, *ptr;

    s = start;

    if (pos == 1)

    {

        start = s->next;

    }

    else

    {

        while (s != NULL)

        {

            s = s->next;

            counter++;

        }

        if (pos > 0 && pos <= counter)

        {

            s = start;

            for (i = 1;i < pos;i++)

            {

                ptr = s;

                s = s->next;

            }

            ptr->next = s->next;

        }

        else

        {

            cout<<"Position out of range"<<endl;

        }

        free(s);

        cout<<"\nElement Deleted"<<endl;

    }

}

/*

* Display Elements of a link list

*/

void single_llist::display()

{

    struct node *temp;

    if (start == NULL)

    {

        cout<<"\nThe List is Empty"<<endl;

        return;

    }

    temp = start;

    cout<<"\nElements of list are: "<<endl;

    while (temp != NULL)

    {

        cout<<temp->data<<"->";

        temp = temp->next;

    }

    cout<<"NULL"<<endl;

}


Output:

Add a comment
Know the answer?
Add Answer to:
Write a function to implement linked list consisting of five nodes. Store the data in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function to implement linked list consisting of six nodes. Store the data in the...

    Write a function to implement linked list consisting of six nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming use these as names as nodes George, Paul, Ross, Joe, Elaine, Robert1. Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes...

  • using C++, NOT C language 1. Write a function called insert() to insert a node to...

    using C++, NOT C language 1. Write a function called insert() to insert a node to the beginning of a linked list. The data is passed into the function. For example insert(8) will insert a node at the beginning of the list with the number 8 in the data field. Each node in the linked list is a ListNode struct as discussed in class. 2. Write a function called print() that will traverse the entire linked list and print out...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

  • #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a...

    #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a complete linked list program which supports the following commands from the user: insert, display, delete, average, find, insertBefore, insertAfter. insert allows them to insert a number into the current list, display will display the numbers in the current list etc. The program is complete except that I have removed the body of the method for find. Given the following class for the nodes in...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • A linked list is constructed of nodes described by the following structure: struct node{ char data;...

    A linked list is constructed of nodes described by the following structure: struct node{ char data; struct node *next; }; Assume a linked list containing a sentinel node is constructed from the above nodes. Write a function named "count"-prototyped as int count(struct node*sent)- that accepts a pointer to the sentinel node; counts the number of data (non-sentinel) nodes containing the character 'A'; and returns that count as the function value.

  • Assume we have a linked list with 12 nodes pointed to by FIRST. Write a function...

    Assume we have a linked list with 12 nodes pointed to by FIRST. Write a function that deletes the first node. Write a function that deletes the 7th node. Write a function that deletes the last node.

  • Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted...

    Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT