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;
}
推荐阅读
- 技术雷达|从技术雷达看DevOps的十年——容器技术和微服务
- 数据连接一切,开启融合数据云新时代——星环科技春季新品发布周盛大开启
- Android之——经常使用手机号码功能
- 区块链的应用领域——公共服务(三)
- Android自己定义控件实战——仿多看阅读平移翻页
- Android中的动画具体解释系列——飞舞的蝴蝶
- Android——使用 Intent传递类
- Android 架构设计实现——MVP模式
- App开放接口api安全性—Token签名sign的设计与实现
- Be Happy.——我的ACM退役贴