输出格式:
第一行输出初始链表元素个数;
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
输入样例:
10 21 30 14 55 32 63 11 30 55 30
输出样例:
10 30 55 30 11 63 32 55 14 30 21 7 30 55 11 63 32 14 21
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
#include <stdio.h> #include <stdlib.h> struct node { int a; struct node *next; }; struct node *creat(int n); //链表建立; void putout(struct node *head); //链表输出; void delete_(struct node *head,int n); //重复节点删除; int main() { int n; struct node *head; head = (struct node *)malloc(sizeof(struct node)); head->next = NULL;//头结点的建立; scanf("%d",&n); head = creat(n); //建立链表; printf("%d\n",n); putout(head); //输出链表; delete_(head,n); //删除节点; putout(head); //输出链表; return 0; } struct node *creat(int n) //链表建立; { struct node *head,*p; head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; while (n--) { p = (struct node *)malloc(sizeof(struct node)); p->next = head->next; scanf("%d",&p->a); head->next = p;//逆序建立; } return head; }; void putout(struct node *head) //链表输出函数; { struct node *p = head->next; while(p) { if (p->next) { printf("%d ",p->a); } else { printf("%d\n",p->a); } p = p->next;//注意不要遗落; } } void delete_(struct node *head,int n) //重复节点删除; { struct node *p,*pi,*q; q = head->next; while(q) { p = q; pi = q->next; while(pi) { if (pi->a == q->a) { struct node *r = pi; p->next = pi->next; pi = pi->next; free(r);//释放删除的节点; n--; } else { pi = pi->next; p = p->next; } } q = q->next; } printf("%d\n",n); }