Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C LANGUAGE You just need to fill up the blanks, the structure of code is below. Please try to answer within 2 hours. Write a

C LANGUAGE

You just need to fill up the blanks, the structure of code is below.

Please try to answer within 2 hours.

Write a C program that takes a linked list and a number n as input and deletes the nthlast node (nthnode from the end) of the list. For this problem, n=1 means "delete the last node," n=2 means "delete the second last node," and so on. If n is greater than the length of the list (denoted by len), then modulus operation is performed on the absolute value of n by len, i.e., (n-1)\%len + 1this used.

Each node of a linked list must have the following structure.

struct node {

int data;

struct node* next;

};

NOTE

  • Use of ARRAYS IS NOT PERMITTED. You will not get marks if you use linked lists to solve the problem.
  • The template contains initial code for list manipulation. You can modify it or use it as it is. DO NOT change the print function.
  • No marks if youdo not really deletethe node. The print function provided must work as-it-is to print the modified list.
  • INPUT
  • The input will consist of two lines containing the numbers. The first line will contain a stream of numbers. Keep accepting the input until you get a -1. The second line will contain a number nn, n will fit in an int.

OUTPUT

Display the final linked list, with nthnth last node deleted from the linked list. You must use the provided print routine. It prints an X at the end to mark the end of the list.

Examples

INPUT

1 2 3 4 5 -1 1

OUTPUT

1 2 3 4 X

#include #include

// to store the link list struct node { int data; struct node* next; };

// insertion in the list "head", at the END struct node* insert(struct node* head, int data) { struct node* n=(struct node*)malloc(sizeof(struct node)); n->next=NULL; n->data=data; if(head==NULL) return n; struct node* tmp=head; while(tmp->next!=NULL) tmp=tmp->next; tmp->next=n; return head; }

// print the list "head" void print(struct node* head) { while(head!=NULL) { printf("%d ",head->data); head=head->next; } printf("X "); return; }

int main() { // Fill this area with your code. return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions

Question

Identify the major participants in the U.S. monetary system.

Answered: 1 week ago

Question

Find the general solution of the differential equation. xy' = y

Answered: 1 week ago