[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.
Implementation of bidirectional 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; }DuLNde,* DuLinkList;
Initialization is as follows:
DuLinkList initList() { DuLNode* head; DuLNode* rear; head=(struct DuLNode*)malloc(sizeof(struct DuLNode)); if(!head)return NULL; rear=(struct DuLNode*)malloc(sizeof(struct DuLNode)); if(!rear)return NULL; head->prior=NULL; head->next=rear; rear->prior=head; rear->next=NULL; return head; }
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; } DuLNode* p=head; int i; for(i=1;i<pos;i++) { if(p->next==NULL) { printf("error\n"); return 0; } p=p->next; } DuLNode* pnew; pnew=(DuLinkList)malloc(sizeof(DuLNode)); 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; }DuLNode,*DuLinkList; //initialization DuLinkList initList() { DuLNode* head; DuLNode* rear; head=(struct DuLNode*)malloc(sizeof(struct DuLNode)); if(!head)return NULL; rear=(struct DuLNode*)malloc(sizeof(struct DuLNode)); if(!rear)return NULL; head->prior=NULL; head->next=rear; rear->prior=head; rear->next=NULL; return head; } //insert int insertList(DuLinkList head,int pos, ElemType e) { if(pos<1) { printf("error\n"); return 0; } DuLNode* p=head; int i; for(i=1;i<pos;i++) { if(p->next==NULL) { printf("error\n"); return 0; } p=p->next; } DuLNode* pnew; pnew=(DuLinkList)malloc(sizeof(DuLNode)); pnew->data=e; p->next->prior=pnew; pnew->next=p->next; p->next=pnew; pnew->prior=p; return 1; } void createList(DuLinkList head,int n) { ElemType e; int i; for(i=1;i<=n;i++) { scanf("%d",&e); insertList(head,i,e); } return; } void printList(DuLinkList head,DuLinkList rear) { DuLNode* p=head; while(p->next!=rear) { p=p->next; printf("%d ",p->data); } return; } void reprintList(DuLinkList head,DuLinkList rear) { DuLNode* p=rear; while(p->prior!=head) { p=p->prior; printf("%d ",p->data); } return; } int main() { DuLinkList head,rear; ElemType e; int n,pos; scanf("%d",&n); head=initList(); rear=head->next; createList(head,n); scanf("%d %d",&pos,&e); if(insertList(head,pos,e)) { printList(head,rear); printf("\n"); reprintList(head,rear); } return 0; }
The operation results are as follows: