Question

In: Computer Science

I want the linked list to be inserted by the user and donot use getline to...

I want the linked list to be inserted by the user and donot use getline to insert it

#include <bits/stdc++.h>

using namespace std;

  

// A linked list Node

struct Node {

    int data;

    struct Node* next;

};

  

// Size of linked list

int size = 0;

  

// function to create and return a Node

Node* getNode(int data)

{

    // allocating space

    Node* newNode = new Node();

  

    // inserting the required data

    newNode->data = data;

    newNode->next = NULL;

    return newNode;

}

  

// function to insert a Node at required position

void insertPos(Node** current, int pos, int data)

{

    // This condition to check whether the

    // position given is valid or not.

    if (pos < 1 || pos > size + 1)

        cout << "Invalid position!" << endl;

    else {

  

        // Keep looping until the pos is zero

        while (pos--) {

  

            if (pos == 0) {

  

                // adding Node at required position

                Node* temp = getNode(data);

  

                // Making the new Node to point to

                // the old Node at the same position

                temp->next = *current;

  

                // Changing the pointer of the Node previous

                // to the old Node to point to the new Node

                *current = temp;

            }

            else

              // Assign double pointer variable to point to the

              // pointer pointing to the address of next Node

              current = &(*current)->next;

        }

        size++;

    }

}

  

// This function prints contents

// of the linked list

void printList(struct Node* head)

{

    while (head != NULL) {

        cout << " " << head->data;

        head = head->next;

    }

    cout << endl;

}

  

// Driver Code

int main()

{

    // Creating the list 3->5->8->10

    Node* head = NULL;

    head = getNode(3);

    head->next = getNode(5);

    head->next->next = getNode(8);

    head->next->next->next = getNode(10);

  

    size = 4;

  

    cout << "Linked list before insertion: ";

    printList(head);

  

    int data = 12, pos = 3;

    insertPos(&head, pos, data);

    cout << "Linked list after insertion of 12 at position 3: ";

    printList(head);

  

    // front of the linked list

    data = 1, pos = 1;

    insertPos(&head, pos, data);

    cout << "Linked list after insertion of 1 at position 1: ";

    printList(head);

  

    // insetion at end of the linked list

    data = 15, pos = 7;

    insertPos(&head, pos, data);

    cout << "Linked list after insertion of 15 at position 7: ";

    printList(head);

  

    return 0;

}

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

// A linked list Node

struct Node {

    int data;

    struct Node* next;

};

// Size of linked list

int size = 0;

// function to create and return a Node

Node* getNode(int data)

{

    // allocating space

    Node* newNode = new Node();

    // inserting the required data

    newNode->data = data;

    newNode->next = NULL;

    return newNode;

}

// function to insert a Node at required position

void insertPos(Node** current, int pos, int data)

{

    // This condition to check whether the

    // position given is valid or not.

    if (pos < 1 || pos > size + 1)

        cout << "Invalid position!" << endl;

    else {

        // Keep looping until the pos is zero

        while (pos--) {

            if (pos == 0) {

                // adding Node at required position

                Node* temp = getNode(data);

                // Making the new Node to point to

                // the old Node at the same position

                temp->next = *current;

                // Changing the pointer of the Node previous

                // to the old Node to point to the new Node

                *current = temp;

            }

            else

              // Assign double pointer variable to point to the

              // pointer pointing to the address of next Node

              current = &(*current)->next;

        }

        size++;

    }

}

// This function prints contents

// of the linked list

void printList(struct Node* head)

{

    while (head != NULL) {

        cout << " " << head->data;

        head = head->next;

    }

    cout << endl;

}

// Driver Code

int main()

{

    // Creating the list 3->5->8->10

    Node* head = NULL;
    Node* current = head;
    int n;
    cout<<"enter no of values you want to enter to initialize the linked list:\t";
    cin>>n;
    for(int i = 0; i < n;i++)
    {
   cout<<"enter value:\t";
   int ele;
   cin>>ele;
   Node* newNode = getNode(ele);
   if(head == NULL)
   {
       head = newNode;
   }
   else
   {
       current = head;
       while(current -> next != NULL)
       {
           current = current -> next;
       }
       current -> next = newNode;
   }
   size++;
    }
    printList(head);
    int data, pos;
    int ch;
    cout<<"want to enter element with position (1 - yes/ 0 - no) :\t";
    cin>>ch;
    while(ch != 0)
    {
   cout<<"enter value:\t";
        cin>>data;
        cout<<"enter position:\t";
        cin>>pos;
        insertPos(&head, pos, data);
        printList(head);
        cout<<"want to insert one more (1 - yes/ 0 - no) :\t";
   cin>>ch;
    }
    return 0;
}

If you have any doubts please comment and please don't dislike.


Related Solutions

In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
Write a program of linked list in which in which element can be inserted only at...
Write a program of linked list in which in which element can be inserted only at the end of the linked list and deletion can also take place at the end. Code needed in java.
C++ Text message decoder Use getline() to get a line of user input into a string:...
C++ Text message decoder Use getline() to get a line of user input into a string: Enter text: IDK if I'll go. It's my BFF's birthday. Search the string using find() for common abbreviations and replace() them with their long form. In addition, use a for loop to iterate through each character of the string and replace any occurences of '.' with '!': Enter text: IDK if I'll go. It's my BFF's birthday. I don't know if I'll go! It's...
I want c++ /c program to build a double linked list and find with a function...
I want c++ /c program to build a double linked list and find with a function the middle element and its position and the linked list must be inserted by the user aand note You should handle the both cases of whether the size of the list even or odd
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
I am having trouble with printing the linked list from user input. Can someone tell me...
I am having trouble with printing the linked list from user input. Can someone tell me what I am doing wrong. #include <iostream> using namespace std; struct node {    int info;    node*link;    node*current;    node*prev;    node * head;    node * last; } ; void Insert(node*&head,node*&last,int n); int SplitList(node*&head); void print(node*&head); int main() {    int num;    node*h;    node*l;    cout << "Enter numbers ending with -999" << endl;    cin >> num;   ...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT