删除线性表节点(线性表)
Home | Web Board | ProblemSet | Standing | Status | Statistics |
Problem E: 删除线性表节点(线性表)
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 921 Solved: 692
[ Submit][ Status][ Web Board]
Description
using namespace std;
struct node
{
int data;
node *next;
};
node *createlist(node *head,int n)
{
node *previous; //前驱结点
node *current; //当前结点
if(n<1)
{
return NULL; //建立空链表
}
previous=head=new node; //建立首结点
cin>>head->data; //输入数据
while(--n)
{
current=new node; //建立新结点
cin>>current->data; //输入数据
previous->next=current; //新节点挂在表尾
previous=current;
}
previous->next=NULL; //置表尾结束标志
return head; //返回首结点指针
}
node *listdel(node *head,int item)
{
node *temp;
/* 从头找到第一个不等于item的结点 */
while(head!=NULL&&head->data==item)
{
temp = head;
head=head->next;
delete temp; //删除该节点
}
if(head==NULL)
return NULL;
node *previous=head; //前驱结点
node *current = head->next; //当前结点
while(current!=NULL)
{
/* 删除连续相同的结点*/
while(current!=NULL&¤t->data==item)
{
/*
请在该部分填写缺少的代码
*/
}
previous->next=current; //重新连接结点
if(current==NULL)
break;
previous=current;
current=current->next; //当前结点后移
}
return head;
}
void output(node *head)
{
node *current=head;
while(current!=NULL)
{
cout<<current->data<<" "; //输出结点数据
current=current->next; //结点指针后移
}
}
int main()
{
node *head=NULL;
int n,item;
cin>>n;
head=createlist(head,n);
cin>>item;
head=listdel(head,item);
output(head);
return 0;
}
Input
输入 n:6
输入数据:1 2 3 4 5 6
输入 item:5
Output
输出:1 2 3 4 6
Sample Input
10 1 2 3 4 5 6 7 8 9 10 8
Sample Output
1 2 3 4 5 6 7 9 10
HINT
[ Submit][ Status][ Web Board]
Anything about the Problems, Please Contact Admin:admin
All Copyright Reserved 2010-2014 HUSTOJ TEAM
GPL2.0 2003-2014 HUSTOJ Project TEAM
Help Maunal
#include <iostream>
using namespace std;
struct node
{int data;node *next;
};
node *createlist(node *head,int n)
{node *previous; //前驱结点node *current; //当前结点if(n<1){return NULL; //建立空链表}previous=head=new node; //建立首结点cin>>head->data; //输入数据while(--n){current=new node; //建立新结点cin>>current->data; //输入数据previous->next=current; //新节点挂在表尾previous=current;}previous->next=NULL; //置表尾结束标志return head; //返回首结点指针
}
node *listdel(node *head,int item)
{node *temp;/* 从头找到第一个不等于item的结点 */while(head!=NULL&&head->data==item){temp = head;head=head->next;delete temp; //删除该节点}if(head==NULL)return NULL;node *previous=head; //前驱结点node *current = head->next; //当前结点while(current!=NULL){/* 删除连续相同的结点*/while(current!=NULL&¤t->data==item){previous->next =current->next ;delete current;current=current->next ;}previous->next=current; //重新连接结点if(current==NULL)break;previous=current;current=current->next; //当前结点后移}return head;
}
void output(node *head)
{node *current=head;while(current!=NULL){cout<<current->data<<" "; //输出结点数据current=current->next; //结点指针后移}
}
int main()
{node *head=NULL;int n,item;cin>>n;head=createlist(head,n);cin>>item;head=listdel(head,item);output(head);return 0;
}
删除线性表节点(线性表)相关推荐
- java中线性表删除元素和删除指定元素_线性表的插入和删除(Java版)
1.线性表的定义: (1).线性表是一种可以在任意位置插入和删除数据元素操作.由n(n≥0)个相同类型数据元素a0, a1,-, an-1组成的线性结构.除了第一个元素没有前驱元素和最后一个元素没有后 ...
- 1-2、数据结构线性表之顺序表的基本操作插入、删除、遍历、初始化
学习需要记录,更需要温习! 上一篇我们总结了什么是线性表,线性表的ADT类型,线性表的两种存储类型顺序存储结构和链式存储结构,今天正式进入线性表的顺序存储结构的总结和归纳,也就是数据结构的线性表的顺序 ...
- 【数据结构】 线性表的顺序表
线性表是一种最为常用的数据结构,包括了一个数据的集合以及集合中各个数据之间的顺序关系.线性表从数据结构的分类上来说是一种顺序结构.在Python中的tuple,list等类型都属于线性表的一种. 从抽 ...
- 四、【线性表】线性表的顺序表示和实现
线性表的顺序表示和实现 前文我们提到过线性表是逻辑结构,只说明了数据元素之间的相互关系,想要使用线性表,我们还需要在计算机上表示出这些数据元素以及元素之间的关系.而对于同一种逻辑结构,可以有多种存储结 ...
- python的线性链表_Python线性表——单链表-阿里云开发者社区
Python线性表--单链表 线性表简介 线性表是一种线性结构,它是由零个或多个数据元素构成的有限序列.线性表的特征是在一个序列中,除了头尾元素,每个元素都有且只有一个直接前驱,有且只有一个直接后继, ...
- 线性表之顺序表与单链表的区别及优缺点
这里比较的是基于C语言实现的顺序表与单链表,与其他语言的实现可能会有差异,但我相信语言是相通的,它们的实现机制应该也差不多. 1.What 什么是顺序表和单链表 ①顺序表: 顺序表是在计算机内存中以数 ...
- 线性表、顺序表和链表,你还分不清?
摘要:其实说实话,可能很多人依然分不清线性表,顺序表,和链表之间的区别和联系! 本文分享自华为云社区<程序员必会自己设计线性表(顺序表.链表)>,原文作者:bigsai. 前言 其实说实话 ...
- 【数据结构笔记04】线性结构:线性表及其实现
本次笔记内容: 2.1.1 引子:多项式表示 2.1.2 线性表及其存储顺序 2.1.3 顺序存储的插入和删除 2.1.4 链式存储及查找 2.1.5 链式存储的插入和删除 2.1.6 广义表与多重链 ...
- Java数据结构与算法_线性表_顺序表与链表
文章目录 线性表 顺序表 顺序表API设计 顺序表的代码实现 链表 单向链表 双向链表 总结 线性表 概述 线性表是最基本.最简单.也是最常用的一种数据结构. 一个线性表是n个具有相同特性的数据元素的 ...
最新文章
- Karush-Kuhn-Tucker 最优化条件 (KKT 条件)(转载)
- 还不知道 Redis 分布式锁的背后原理?还不赶快学习一下
- 华为云阳云计算外包给哪家公司的_长春作为东北中心,华为四大件已经配齐,绝了!...
- 入门顶点动画纹理的实例化绘制
- python找出只出现一次的数字_Python数组中只出现一次的数字
- OculusQuest2 直接连电脑调试
- python替换UTF-8编码文本中任意特殊字符,包括中文符号问题:大量文本,将其中的特殊字符用空
- AO3414相关技术资料
- 蛮牛第2季- Unity2d游戏开发经典教程
- 《高效能人士的7个习惯》PDF,复习笔记(上)
- android svg 线条动画教程,SVG技术入门:线条动画实现原理
- 【PS】如何把图片变成透明
- 小米路由器开启DDNS并支持二级路由
- vw/vh:移动适配之vw/vh(使用方法)
- 【顺序表】顺序表的就地逆置
- lcoal Storage 介绍
- Macbook充电和电池保养的标准答案
- Dx10和Dx9的一些区别
- 69个微信小程序常见问题
- 3dsmax怎么添加uv坐标_012:多层贴图UVWMAP(UV坐标)技术