Knowee
Questions
Features
Study Tools

You are given a sequence of integers terminated with a -1. The -1 isnot part of the input sequence.  Next, you are given a positive number N. You have to create a linked list with the input sequence of integersas entries. You can use the following structure.  struct node{  int data;struct node *next; };  Now, you have to delete all but the last N elements from the linkedlist, and print the resulting list. (i.e. The resulting list willconsist of only the last N elements from the list.)  If N is longer than the length of the linked list, you must print -1. While printing, the entries of the list must be separated by a singlespace.  Sample Input3 4 5 6 -11 Sample Output6

Question

You are given a sequence of integers terminated with a -1. The -1 isnot part of the input sequence.  Next, you are given a positive number N. You have to create a linked list with the input sequence of integersas entries. You can use the following structure.  struct node{  int data;struct node *next; };  Now, you have to delete all but the last N elements from the linkedlist, and print the resulting list. (i.e. The resulting list willconsist of only the last N elements from the list.)  If N is longer than the length of the linked list, you must print -1. While printing, the entries of the list must be separated by a singlespace.  Sample Input3 4 5 6 -11 Sample Output6

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The problem is asking to implement a linked list using the given sequence of integers. After creating the linked list, we need to delete all but the last N elements from the list and print the resulting list. If N is longer than the length of the linked list, we should print -1.

Here is a step-by-step solution in C++:

#include<bits/stdc++.h>
using namespace std;

struct node{
    int data;
    struct node *next;
};

node* createNode(int data){
    node* newNode = new node();
    if (!newNode) {
        cout << "Memory error\n";
        return NULL;
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

node* insertNode(node* head, int data){
    node* newNode = createNode(data);
    if (head == NULL) {
        head = newNode;
    } else {
        node* temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
    return head;
}

void deleteNodes(node* &head, int N){
    if (head == NULL)
        return;
    node* temp = head;
    int count = 0;
    while (temp != NULL) {
        count++;
        temp = temp->next;
    }
    if (count < N) {
        cout << "-1";
        return;
    }
    temp = head;
    for (int i = 1; i < count-N+1; i++) {
        node* next = temp->next;
        free(temp);
        temp = next;
    }
    head = temp;
}

void printList(node* head){
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}

int main(){
    node* head = NULL;
    int data;
    while (cin >> data && data != -1) {
        head = insertNode(head, data);
    }
    int N;
    cin >> N;
    deleteNodes(head, N);
    printList(head);
    return 0;
}

This program first creates a linked list from the input sequence. Then it counts the number of nodes in the list. If the count is less than N, it prints -1. Otherwise, it deletes the first count-N nodes from the list. Finally, it prints the remaining nodes in the list.

This problem has been solved

Similar Questions

Write a function that searches a linked list for the integers that occur more than once, and removes all their occurrences except for the first occurrences. The function does not remove the integers that occur only once. Removed nodes must be freed. Each node is defined asstruct node{     int data;     struct node * next;};The function is declared as follows. Its input is the head pointer of the linked list. It returns the head pointer of the linked list after the removal.struct node *remove_repeated_integer(struct node *list);

Write a C program to reverse a single linked list recursively. #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // Write a function to create a new node with the given data // Write a function to print the linked list // Write a function to reverse the linked list recursively // Write your main function here

reverse a linked list- DATA STRUCTURESWrite a program to reverse a linked list.Constraints:Integers onlyExample:Input FormatTo get the number of elements followed by the elements separated by a single space.Output FormatDisplays the reversed list.Note: There is a space at the end of the outputExplanation:linked listPublic Test Cases:# INPUT EXPECTED OUTPUT1 3 67 45 1212 45 672 5 -1 3 2 6 44 6 2 3 -1

Given a linked list of size N. The task is to reverse every k nodes (where k is an input to the function) in the linked list.Input FormatFirst line of input contains n- the size of first linked list. Next line of input contains n integers the n elements of the first linked list.Constraints1 <= N <= 10^41 <= k <= NOutput FormatPrint the modified linked list.Sample input81 2 2 4 5 6 7 84Sample output4 2 2 1 8 7 6 5

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.After doing so, return the head of the final linked list.  You may return any such answer.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.