# Linked list application (local inversion / non descending combination / derivation of univariate polynomial / return the last k item) data structure C language version

6-3 local inversion of the single linked list of the leading node (9 points)

This problem requires writing a function to realize the local inverse operation function of the single chain linear table of the leading node. L is a single linked list of leading nodes, and the function listreverse_ L (LinkList & L) requires that the elements in the single linked list be inverted without opening a new node. If the elements in the original single linked list are 1,2,3,4 in turn, the elements after inversion are 4,3,2,1.

### Function interface definition:

`void ListReverse_L(LinkList &L);`

### Example of referee test procedure:

```//Library function header file contains
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

//Function status code definition
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int  Status;
typedef int  ElemType; //Assume that all elements in the linear table are integers

typedef struct LNode
{
ElemType data;
struct LNode *next;

{
LNode *rearPtr,*curPtr;   //A tail pointer, a pointer to the new node
L=(LNode*)malloc(sizeof (LNode));
if(!L)exit(OVERFLOW);
rearPtr=L;  //Initially, the head node is the tail node, and realptr points to the tail node
for (int i=1;i<=n;i++){  //Each cycle opens up a new node and collapses the new node to the tail node
curPtr=(LNode*)malloc(sizeof(LNode));//Generate new node
if(!curPtr)exit(OVERFLOW);
scanf("%d",&curPtr->data);//Enter element value
curPtr->next=NULL;  //The next of the last node is null
rearPtr->next=curPtr;
rearPtr=curPtr;
}
return OK;
}
LNode *p=L->next;  //p points to the first element node
while(p!=NULL)
{
if(p->next!=NULL)
printf("%d ",p->data);
else
printf("%d",p->data);
p=p->next;
}
}
int main()
{
int n;
scanf("%d",&n);
if(ListCreate_L(L,n)!= OK) {
printf("Table creation failed!!!\n");
return -1;
}
ListReverse_L(L);
ListPrint_L(L);
return 0;
}

Input format:

In the first row, enter an integer n to represent the number of elements in the single linked list. In the next row, there are n integers separated by spaces.

Output format:

Output the elements of the inverted sequence table. The two elements are separated by spaces, and there is no space after the last element.

```4
1 2 3 4```

### Output example:

`4 3 2 1`

### AC Code:

```void ListReverse_L(LinkList &L){
p = L->next;
L->next = NULL;
while(p){
q = p;
p = p->next;
q->next = L->next;
L->next = q;
}
}```

### Analysis report: digging

7-1 combination of two ordered linked list sequences (20 points)

Two non descending linked list sequences S1 and S2 are known, and the function is designed to construct a new non descending linked list S3 after the combination of S1 and S2.

### Input format:

The input is divided into two lines. Each line gives a non descending sequence composed of several positive integers, and − 1 is used to represent the end of the sequence (− 1 does not belong to this sequence). Numbers are separated by spaces.

### Output format:

Output the combined new non descending linked list in one line, separate the numbers with spaces, and there shall be no redundant spaces at the end; If the new linked list is empty, NULL is output.

### Input example:

```1 3 5 -1
2 4 6 8 10 -1```

### Output example:

`1 2 3 4 5 6 8 10`

### AC Code:

```#include <stdio.h>
#include <stdlib.h>
typedef struct Node *List;
typedef struct Node *Ptr;
struct Node{
int Data;
Ptr next;
};
int main(){
List L1, L2, L3;
List p1, p2, p3, p4;
List temp;
int flag=0;
int e;
L1 = (List)malloc(sizeof(struct Node));
L1->next = NULL;
p1 = L1;
scanf("%d", &e);
if(e==-1)
L1->next = NULL;
while (e != -1){
p2 = (List)malloc(sizeof(struct Node));
p2->Data = e;
p1->next = p2;
p2->next = NULL;
p1 = p2;
scanf("%d", &e);
}
L2 = (List)malloc(sizeof(struct Node));
L2->next = NULL;
p1 = L2;
scanf("%d", &e);
if(e==-1)
L2->next = NULL;
while (e != -1){
p2 = (List)malloc(sizeof(struct Node));
p2->Data = e;
p2->next = NULL;
p1->next = p2;
p1 = p2;
scanf("%d", &e);
}

p1 = L1->next;
p2 = L2->next;
L3 = L1;
p3 = L1;
while (1){
if (L2->next == NULL&&L1->next == NULL){
flag = 1;
break;
}
if (p1 == NULL || p2 == NULL)
break;
if (p1 != NULL&&p2 != NULL){
if (p1->Data > p2->Data){
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
else if (p1->Data < p2->Data){
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else{
p3->next = p1;
p3 = p1;
p1 = p1->next;
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
}
if (p1 != NULL)
p3->next = p1;
else if(p2 != NULL)
p3->next = p2;
if (flag)
printf("NULL\n");
else{
p3 = L3;
while (p3->next->next != NULL){
p3 = p3->next;
printf("%d ", p3->Data);
}
p3 = p3->next;
printf("%d\n", p3->Data);
}
return 0;
}```

### Analysis report: digging

7-2 derivation of univariate polynomial (20 points)

Design function to find the derivative of univariate polynomial.

### Input format:

Enter the non-zero term coefficients and exponents of polynomials in exponential descending mode (the absolute values are integers not exceeding 1000). Numbers are separated by spaces.

### Output format:

The coefficients and exponents of the nonzero terms of the derivative polynomial are output in the same format as the input. Numbers are separated by spaces, but there must be no extra spaces at the end.

### Input example:

`3 4 -5 2 6 1 -2 0`

### Output example:

`12 3 -10 1 6 0`

### AC Code:

```#include<stdio.h>
int main(){
int i=0,j,k=0,t1,t2;
int a1={0},b1={0};
while(scanf("%d %d",&t1,&t2)==2){
if(t2!=0){
a1[i]=t1*t2;
b1[i]=t2-1;
i++;k++;
}
if(getchar()=='\n') break;
}
if(k==0)
printf("0 0");
for(i=0;i<k;i++){
printf("%d %d",a1[i],b1[i]);
if(i<k-1)
printf(" ");
}
return 0;
}
```

### Analysis report: digging

7-3 find the penultimate item K of the linked linear table (20 points)

Given a series of positive integers, please design an algorithm as efficient as possible to find the number at the penultimate position.

### Input format:

The input first gives a positive integer K, followed by several non negative integers, and finally ends with a negative integer (the negative number is not included in the sequence and should not be processed).

### Output format:

Output the data at the penultimate position. If this location does not exist, the error message NULL is output.

### Input example:

`4 1 2 3 4 5 6 7 8 9 0 -1`

`7`

### AC Code:

```#include<bits/stdc++.h>
using namespace std;
typedef struct LNode{
int SXHNB, n;
struct LNode *next;
int a, sum = 0;
newL = new LNode;
newL->next = NULL;
while(scanf("%d", &a) == 1 && a >= 0){
sum++;
p = new LNode;
p->SXHNB = a;
p->next = newL->next;
newL->next = p;
}
newL->n = sum;
}
if(k > solveL->n) { printf("NULL\n"); return ;}
int sum = 0;
while(p){
sum++;
if(sum == k) {printf("%d\n", p->SXHNB); break;}
p = p->next;
}
return ;
}
int main(){
int K;
scanf("%d", &K);
CreaterList(L1);
Solve(L1, K);
return 0;
}

```

### Analysis report: digging

Day36 excavation to be filled

Posted on Tue, 05 Oct 2021 14:23:05 -0400 by s_shrum