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
  • 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

  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • [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; };

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

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
ADVERTISEMENT