PIPIOJ—1447: PIPI的线性表问题Ⅰ

题目描述 已知线性表中的元素以递增序列排列,并以单链表作存储结构。设计算法删除表中所有值相同的多余元素(使得操作后的线性表中所有的值均不相同),同时释放被删结点空间,并分析算法的时间复杂度。
输入 第一行输入一个正整数n,表示元素个数,n<=100。
第二行输入n个正整数元素,保证元素以递增排列,元素的值<=100。
输出 【PIPIOJ—1447: PIPI的线性表问题Ⅰ】输出删除相同元素后的链表。
样例输入 5
1 1 3 4 4
样例输出 1 3 4
C++代码实现

#define _CRT_SECURE_NO_WARNINGS #include #include typedef int ElemType; typedef struct LNode { ElemType data; struct LNode* next; }LNode,*LinkList; LinkList CreateList(LinkList& L, int n) { L = (LNode*)malloc(sizeof(LNode)); L->next = NULL; LNode* s, * r = L; int i = 0; int x; while (i < n) { scanf("%d", &x); s = (LNode*)malloc(sizeof(LNode)); s->data = https://www.it610.com/article/x; s->next = r->next; r->next = s; r = s; i++; } r->next = NULL; return L; }void PrintList(LinkList L) { LNode* p = L->next; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); }void DeleteRepeated(LinkList& L) { LNode* p = L->next; LNode* q, * r; while (p->next != NULL) { q = p->next; if (p->data =https://www.it610.com/article/= q->data) { r = q->next; p->next = r; free(q); } else { p = p->next; } } }int main() { LinkList L; int n; scanf("%d", &n); CreateList(L, n); DeleteRepeated(L); PrintList(L); return 0; }

    推荐阅读