SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除
Problem Description
按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。
Input
第一行输入元素个数 n (1 <= n <= 15);
第二行输入 n 个整数,保证在 int 范围内。
Output
第一行输出初始链表元素个数;
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。
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;
};
int main()
{
struct node *head, *p, *q, *r;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
int n,i;
scanf("%d",&n);
for(i=0; i<n; i++){
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = head->next;
head->next = p;
}
printf("%d\n",n);
p = head->next;
while(p->next)
{
printf("%d ",p->data);
p = p->next;
} printf("%d\n",p->data);
p = head->next;//要删除的节点是q,它的前一个用r表示;
while(p){
r = p;
q = r->next;
while(q){
if(p->data==q->data){
r->next = q->next;
free(q);
n--;
q = r->next;
}
else{
r = r->next;
q = r->next;
}
//printf("*\n");
}
p = p->next;
//printf("#\n");
}
printf("%d\n",n);
p = head->next;
while(p->next){
printf("%d ",p->data);
p = p->next;
} printf("%d\n",p->data);
return 0;
}
SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除的更多相关文章
- SDUT OJ 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT-2122_数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 按照数据输入的相反顺序(逆 ...
- SDUT 3404 数据结构实验之排序七:选课名单.!?
数据结构实验之排序七:选课名单 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 随着学校规模 ...
- SDUT 3346 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...
- SDUT 3379 数据结构实验之查找七:线性之哈希表
数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...
- SDUT 3363 数据结构实验之图论七:驴友计划
数据结构实验之图论七:驴友计划 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 做为一个资深 ...
- SDUT OJ 数据结构实验之链表五:单链表的拆分
数据结构实验之链表五:单链表的拆分 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之链表九:双向链表
数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
随机推荐
- Mybatis工具Generator
转自:http://www.cuiyongzhi.com/post/36.html MyBatis Generator(以下简称为MBG),可以逆向生成持久层的基本代码,而且mybatis的实现方案比 ...
- 微信公众平台PHP示例一
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015-12-18 * Time: 21:51 */ define ...
- MapReduce和YARN框架
MapReduce组件如图
- PC 微信页面倒计时代码 safari不兼容date的问题
PC: 1.html页面: <div class="aTime"> <em id="t_d"></em> <em id ...
- libevent源码分析
这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...
- opennebule 创建cdrom数据发送
{","csrftoken":"b9b5026f1a92180b789971ed8e21d28b"}
- Python 安装selenium
一.报错信息 No module named 'selenium' 二.系统环境 操作系统:Win10 64位 Python版本:Python 3.7.0 三.安装参考 1.使用pip安装seleni ...
- 10.LIKE 操作符
LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. LIKE 操作符 LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. SQL LIKE 操作符语法 SELECT colum ...
- Django框架 之 Auth用户认证
Django框架 之 Auth用户认证 浏览目录 auth模块 user对象 一.auth模块 1 from django.contrib import auth django.contrib.aut ...
- Web Pages version 2兼容 Web Pages version 1的设置
If you want to run a site using Web Pages version 1 (instead of the default, as in the previous poin ...