单链表删除整表_单链表删除

单链表删除整表

Deletion can be at various positions like:

删除可以在各个位置进行,例如:

  1. Deleting the first node

    删除第一个节点

  2. Deleting the last node

    删除最后一个节点

  3. Deleting the intermediate node

    删除中间节点

删除单个链表中的第一个节点 (Deleting the first node in single linked list)

In such case, the head node is to be removed and the next node needs to be assigned as updated head node.

在这种情况下,将删除头节点,并且需要将下一个节点分配为更新的头节点。

  1. Create a temporary node, say temp which points to the head node (first node) of the list.

    创建一个临时节点,例如temp ,它指向列表的头节点(第一个节点)。

  2. Move head node pointer to the immediate next node and delete (dispose) the temp node.

    将头节点指针移动到紧邻的下一个节点,然后删除(布置)临时节点。

删除单个链接列表中的最后一个节点 (Deleting the last node in the single linked list)

In such case the last node is to be removed from list. The steps are following:

在这种情况下,最后一个节点将从列表中删除。 步骤如下:

  1. We need to keep track of the previous node of last node. That's why while traversing to the last node we need to set a prev node also which will point to the previous node of the tail node( last node) after traversal.

    我们需要跟踪最后一个节点的前一个节点。 这就是为什么在遍历到最后一个节点时,我们还需要设置一个prev节点,该节点将在遍历后指向尾节点(最后一个节点)的前一个节点。

  2. So, we have two pointer, one tail node pointing to the last node and another is prev node pointing to the previous node of the last node.

    因此,我们有两个指针,一个尾节点指向最后一个节点,另一个是上一个节点指向最后一个节点的前一个节点。

  3. Set the next pointer of the prev node to NULL and delete the tail node. (last node)

    将上一个节点为NULL的下一个指针,并删除尾节点。 (最后一个节点)

删除单个链接列表中的中间节点 (Deleting an intermediate node in the single linked list)

In such case an intermediate node is to be deleted. The approach is quite similar to the previous.

在这种情况下,中间节点将被删除。 该方法与以前的方法非常相似。

  1. Similar to the previous case, a curr node and a prev node is to be maintained. Curr node will point to the node to be deleted and prev node will point to the previous node.

    与前一种情况类似,将保留curr节点和prev节点。 Curr节点将指向要删除的节点,而prev节点将指向上一个节点。

  2. Set the next pointer of the prev node to the next pointer of curr node.

    在上一个节点的next指针设置为CURR节点的下一个指针。

  3. Delete the curr node.

    删除curr节点。

链表中删除的C实现 (C implementation of deletion in a linked list)

#include <stdio.h>
#include <stdlib.h>
struct node{
int data; // data field
struct node *next;
};
void traverse(struct node* head){
struct node* current=head; // current node set to head
int count=0; // to count total no of nodes
printf("\n traversing the list\n");
while(current!=NULL){ //traverse until current node isn't NULL
count++; //increase node count
printf("%d ",current->data);
current=current->next; // go to next node
}
printf("\ntotal no of nodes : %d\n",count);
}
struct node* creatnode(int d){
struct node* temp=malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
return temp;
}
int main(){
printf("creating the linked list by inserting new nodes at the begining\n");
printf("enter 0 to stop building the list, else enter any integer\n");
int k,count=1,x;
struct node* curr,*temp,*prev;
scanf("%d",&k);
struct node* head=creatnode(k); //buliding list, first node
scanf("%d",&k);
///inserting at begining//
while(k){
curr=creatnode(k);
curr->next=head;                     //inserting each new node at the begining
head=curr;
scanf("%d",&k);
}
traverse(head); // displaying the list
deleting the first node
printf("\ndeleting the first node.............\n");
temp=head;   // first node assigned to temp
head=head->next; // head node is updated
free(temp); // deleting the first node
traverse(head);  // displaying the list
printf("\nfirst node deleted...............\n");
/deleting the last node
printf("\ndeleting the last node.............\n");
temp=head->next;
prev=head;
while(temp->next!=NULL){
temp=temp->next;
prev=prev->next;
} 
// after traversal temp points to the last 
//node and prev to the previous node of the last node
prev->next=NULL;
free(temp);
traverse(head);
printf("\last node deleted................\n");
///deleting any intermediate node in the list
printf("\n enter the position of the node you want to delete\n");
scanf("%d",&x);
temp=head->next;
prev=head;
while(count!=x-1){
temp=temp->next;
prev=prev->next;
count++;
} // temp pointing to the node to be deleted and prev to the previous node 
prev->next=temp->next;
free(temp);
traverse(head);
return 0;
}

Output

输出量

creating the linked list by inserting new nodes at the begining      
enter 0 to stop building the list, else enter any integer 
1  
2  
3  
4  
5  
6  
7  
8  
9  
0  
traversing the list     
9 8 7 6 5 4 3 2 1        
total no of nodes : 9    
deleting the first node.............
traversing the list     
8 7 6 5 4 3 2 1          
total no of nodes : 8    
first node deleted...............   
deleting the last node............. 
traversing the list     
8 7 6 5 4 3 2 
total no of nodes : 7    
last node deleted................   
enter the position of the node you want to delete        
5  
traversing the list     
8 7 6 5 3 2   
total no of nodes : 6  

翻译自: https://www.includehelp.com/data-structure-tutorial/single-linked-list-deletion.aspx

单链表删除整表

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541282.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WordPress数据表wp-options数据字段存JSON数据

2019独角兽企业重金招聘Python工程师标准>>> wp_options表是WordPress中最重要的表&#xff0c;一切程序设置、主题设置和绝大多数插件的设置大都保存在此表。 WordPress里面数据表wp-options数据字段存JSON数据 &#xff1a; a:90: {s:11:"^wp-json/?$"…

css scroll属性_CSS中的scroll-behavior属性

css scroll属性CSS | 滚动行为属性 (CSS | scroll-behavior property) Who does not want their links to function smoothly and attractively? This type of functionality is very easy to implement. All you need is a bit of awareness about the property that would h…

c 语言五子棋游戏代码,C语言案例:控制台版本的五子棋游戏【代码】

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼return true;}x;}return false;}bool isLeftInclinedWin(bool isBlack,int x,int y){char c isBlack ? :O;int count;while(x>0 && y>0 && state[x][y] c){y--;x--;}count 0;if(state[x][y] c) count 1…

基于HTML5 Canvas 实现弹出框

用户鼠标移入时&#xff0c;有弹出框出现&#xff0c;这样的需求很常见。这在处理HTML元素实现时简单&#xff0c;但是如果是对 HTML5 Canvas 构成的图形进行处理&#xff0c;这种方法不再适用&#xff0c;因为 Canvas 使用的是另外一套机制&#xff0c;无论在 Canvas 上绘制多…

半素数c语言,非常简单的c题目 不懂 紧急求助

1 半素数素数是指大于1且只有1和他本身两个因子的正整数&#xff0c;例如2、3、5、89都是素数&#xff0c;2、4、10都不是素数。在这里我给半素数下一个定义&#xff1a;一个大于1的正整数能分解为两个素数的乘积&#xff0c;那么这个正整数就是半素数&#xff0c;比如62*3&am…

JavaScript | 嵌套if的示例

Example: 例&#xff1a; In this example, we are reading salary of an employee and finding the discount and net pay based on given salary and discount rate. 在此示例中&#xff0c;我们正在读取员工的薪水&#xff0c;并根据给定的薪水和折扣率找到折扣和净工资。 …

POJ 1014 Dividing 背包

二进制优化&#xff0c;事实上是物体的分解问题。 就是比方一个物体有数量限制&#xff0c;比方是13&#xff0c;那么就须要把这个物体分解为1。 2&#xff0c; 4&#xff0c; 6 假设这个物体有数量为25&#xff0c;那么就分解为1&#xff0c; 2&#xff0c; 4。 8。 10 看出规…

node.js 中间件_Node.js中的Passport中间件(模块)

node.js 中间件Hi! Welcome to Node.js Authentication Series, where well study and program the passport module or middleware. 嗨&#xff01; 欢迎使用Node.js身份验证系列 &#xff0c;我们将在其中研究和编程通行证模块或中间件 。 Nowadays, an important tool in m…

android开发自动提示框,Android 多种简单的弹出框样式设置代码

简介这是一个基于AlertDialog和Dialog这两个类封装的多种弹出框样式&#xff0c;其中提供各种简单样式的弹出框使用说明。同时也可自定义弹出框。项目地址&#xff1a;http://www.easck.com/jjdxmashl/jjdxm_dialogui特性1.使用链式开发代码简洁明了2.所有的弹出框样式都在Dial…

小程序中利用Moment.js格式时间

2019独角兽企业重金招聘Python工程师标准>>> LeanCloud给的日期是ISO格式&#xff0c;比如2017-06-05T14:08:20.589Z&#xff0c;直接显示在页面上体验不好。 凡是有关日期的&#xff0c;格式化、计算&#xff0c;用moment就够了。 1.下载 http://momentjs.com/ 选m…

php 检查数组为空_检查数组是否为空在PHP中

php 检查数组为空Given an array and we have to check if array is an empty or not using PHP. 给定一个数组&#xff0c;我们必须检查数组是否为空或不使用PHP。 To check whether an array is empty or not, we can use a built-in function empty(), in other cases wher…

JEESZ分布式架构3--CentOs下安装MySQL(环境准备)

声明&#xff1a;因为运行环境是基于Linux系统的&#xff0c;在做此框架之前需要做一些前期的环境准备工作CentOs下安装MySQL网上很多实例&#xff0c;因为博客后期作为框架的原生教程&#xff0c;故这边做详细的安装记录&#xff0c;我这边已经下载好了MySQL&#xff0c;通过s…

一个函数里两个setjmp_C语言中setjmp.h的longjmp()函数

一个函数里两个setjmpWe can call this function as an advance version of goto statement but with more dynamic range. The longjump() function allows us to pass parameters to know that the control has been jumped or not. 我们可以将此函数称为goto语句的高级版本&…

linux sublime nodejs,Ubuntu环境下sublime3 nodejs安装与插件配置

1.sudo add-apt-repository ppa:webupd8team/sublime-text-3回车&#xff0c;出现很多信息。但是我们看看图片最后字知道&#xff0c;这地方在等待我们确认是否添加这个仓库&#xff0c;按enter键继续&#xff0c;按crtlc取消。此时&#xff0c;按ENTER继续&#xff0c;建立信任…

李洪强iOS开发之FMDB线程安全的用法

// // ViewController.m // 04 - FMDB线程安全的用法 // // Created by 李洪强 on 2017/6/6. // Copyright © 2017年 李洪强. All rights reserved. // #import "ViewController.h" //导入头文件 #import "FMDB.h" interface ViewController () p…

SCHAR_MIN常数,C ++中的示例

C SCHAR_MIN宏常量 (C SCHAR_MIN macro constant) SCHAR_MIN constant is a macro constant which is defied in climits header, it is used to get the minimum value of a signed char object, it returns the minimum value that a signed char object can store, which i…

android分开两个线程做事,android开发教程之handle实现多线程和异步处理

这次浅谈一下Handler,为什么会出现Handler这个功能特性呢&#xff1f;首先&#xff0c;在之前的基本控件&#xff0c;基本都是在Activity的onCreate(Bundle savedInstanceState)方法中调用和处理的&#xff0c;但是&#xff0c;在有些情况&#xff0c;比如在网络上下载软件等一…

夏夜

儿时的夏夜毕竟是最有夏夜的味道。屋堂的煤油灯啪嗒的跳动&#xff0c;忽明忽暗&#xff0c;真怕它脆弱的明亮突然变黑暗。屋外弯月星稀&#xff0c;月光优雅的撒在平静的湖面上&#xff0c;清纯而又温和。水鸟在湖岸边慵懒的伸了伸脖子。正享受着夏夜的宁静和清凉。调皮的小孩…

Python operator.lt()函数与示例

operator.lt()函数 (operator.lt() Function) operator.lt() function is a library function of operator module, it is used to perform "less than operation" on two values and returns True if the first value is less than the second value, False, otherw…

android实现滑动切换图,Android:使用ViewPager实现左右滑动切换图片加点点

图片发自简书App1、引入android-support-v4.jar包&#xff0c;在主布局里加入< ?xml version"1.0" encoding"utf-8"?>< RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.…