### List 7 of data structure experiment: deletion of duplicate elements in single list

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

Establish a single chain table according to the reverse order (reverse bit order) of data input, and delete the repeated elements in the single chain table (only the last one with the same value is reserved).

Input

Number of input elements in the first line n (1 < = n < = 15);

Enter n integers in the second line to ensure that they are in the range of int.

Output

The first line outputs the number of initial linked list elements;

The second line outputs the initial list in reverse order;

The third line outputs the number of single chain table elements after deleting duplicate elements;

The fourth line outputs the single chain table after the duplicate elements are deleted.

Sample Input

10 21 30 14 55 32 63 11 30 55 30

Sample Output

10 30 55 30 11 63 32 55 14 30 21 7 30 55 11 63 32 14 21

#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; };//Linked list is to connect nodes. First, establish the node of linked list, which stores data field and pointer field struct node *creat(int n) { int i; struct node *head,*p; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL;//Request a header node to be empty for(i=1; i<=n; i++) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=head->next;//To insert the chain list in reverse order, we need to insert the p-nodes that are input in order into the chain list. At the end of the list, we assume that there are already head nodes and P. if we insert a new node, the new node will be connected with P. if we want to connect, let the new node point to the address of P. the address of P is recorded by head - > next, the new p - > next = head - > next, save the pointer field of head and give it a new reference To connect the head node and the new P at the same time, let head - > next = new p head->next=p;//After establishing p } return head; }; int retu(struct node *head,int n) { struct node *x,*q,*z; x=head->next;//head is a built linked list, let x point to the first data, set up q, z to go down and compare with the data in X while(x)//As long as x is not empty { q=x;//q go down from the data where x is and compare all the data behind it one by one z=q->next; while(z) { if(z->data==x->data)//Is compared with the first data { q->next=z->next;//If the data is equal, delete node z, first save an address below z, let q point to the back of z, and link the link list after q and z z=q->next;//z go back and compare the data n--;//Count the number of nodes after deletion } else { z=z->next; q=q->next; } } x=x->next; } return n; } void print(struct node *head) { int n=0; struct node *p; p=head->next; while(p)//As long as p is not empty { n++; if(n==1) printf("%d",p->data); else printf(" %d",p->data);//Output the data saved by p, and point to the next one after output p=p->next; } printf("\n");//n ensure that the number of spaces is exactly the same } int main() { struct node *head; int n,m; scanf("%d",&n); head=creat(n); printf("%d\n",n); print(head); m=retu(head,n); printf("%d\n",m); print(head); return 0; }