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?
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 =...
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...
Assume that you want to implement binary search with a linked list. What would be the...
Assume that you want to implement binary search with a linked list. What would be the performance of this algorithm? Compare and contrast this algorithm with the implementation of binary search on traditional sorted array and give a discussion. Your discussion and analysis must explain what the possibilities, issues and consequences of such design are, and then explain whether these issues would exist in the traditional array approach. Your answer can be around 1-2 paragraph of writing backed-up with algorithmic...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT