# Insert nodes in the bidirectional linked list and output

Posted by drfate on Sat, 06 Nov 2021 13:39:05 +0100

[problem description]

For the two-way linked list of the leading node, the insertion algorithm is realized.

[input form]

Enter an N in the first line (n is greater than or equal to 1 and less than 1000);

In the second line, enter n integers separated by spaces to create a two-way linked list with a length of N. for the convenience of two-way output, you need to keep the head and tail pointers;

On the third line, enter pos and e, separated by spaces, indicating the insertion position and the value of the inserted element, respectively.
[output form]

If the insertion position is legal and the insertion is successful, the following is output:

The first row outputs the elements in the bidirectional linked list from scratch, separated by spaces;

The second line reversely outputs the elements in the two-way linked list from the tail node, separated by spaces.

If the insertion position is illegal, for example, the insertion position is < 1 or out of the range of the linked list, error is output

[example input 1]

5

-4  5  2  7  0

2  100

[sample output 1]

-4 100 5 2 7 0

0 7 2 5 100 -4

[example input 2]

5

1 2 3 4 5

6 6

[sample output 2]

1 2 3 4 5 6

6 5 4 3 2 1

[example input 3]

5

1 2 3 4 5

8 8

[sample output 3]

error

To complete this problem, we first need to know how to represent a two-way linked list.

If a pointer field is added to the node of the linear linked list to point to the direct precursor of the node, starting from any node in the table, you can find both the successor of the node and the precursor of the node. The whole linked list contains two chains respectively pointing to the precursor and the successor, which is called a two-way linked list. The storage structure of bidirectional linked list is shown in the figure:

The storage representation of the bidirectional linked list is described as follows:

```typedef struct DuLNode
{
ElemType data;
struct DuLNode * prior;
struct DuLNode * next;

Initialization is as follows:

```DuLinkList initList()
{
DuLNode* rear;
rear=(struct DuLNode*)malloc(sizeof(struct DuLNode));
if(!rear)return NULL;
rear->next=NULL;
}```

The insertion and deletion of two-way linked list must be modified by two reverse pointers at the same time, and the operation process is more complex than that of linear list. Insert and delete operations are shown in the following figure:

Insert code to indicate:

```int insertList(DuLinkList head,int pos, ElemType e)
{
if(pos<1)
{
printf("error\n");
return 0;
}
int i;
for(i=1;i<pos;i++)
{
if(p->next==NULL)
{
printf("error\n");
return 0;
}
p=p->next;
}
DuLNode* pnew;
pnew->data=e;
p->next->prior=pnew;
pnew->next=p->next;
p->next=pnew;
pnew->prior=p;
return 1;
}```

Finally, the implementation and inspection of the complete code:

```#include<stdio.h>
#include<stdlib.h>
typedef  int ElemType;
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
//initialization
{
DuLNode* rear;
rear=(struct DuLNode*)malloc(sizeof(struct DuLNode));
if(!rear)return NULL;
rear->next=NULL;
}
//insert
{
if(pos<1)
{
printf("error\n");
return 0;
}
int i;
for(i=1;i<pos;i++)
{
if(p->next==NULL)
{
printf("error\n");
return 0;
}
p=p->next;
}
DuLNode* pnew;
pnew->data=e;
p->next->prior=pnew;
pnew->next=p->next;
p->next=pnew;
pnew->prior=p;
return 1;
}
{
ElemType e;
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&e);
}
return;
}
{
while(p->next!=rear)
{
p=p->next;
printf("%d ",p->data);
}
return;

}
{
DuLNode* p=rear;
{
p=p->prior;
printf("%d ",p->data);
}
return;
}
int main()
{
ElemType e;
int n,pos;
scanf("%d",&n);
scanf("%d %d",&pos,&e);
{