链表 2.3

实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。

示例

输入:单向链表a->b->c->d->e中的结点c。

结果:不返回任何数据,但该链表变为:a->b->d->e。

分析:因为无法访问待删除结点的前继结点,所以只能通过复制将后续链表整体向前移动一个位置,并删除最后一个多余的结点。显然,当待删除结点为链表的尾结点时(即没有后继),该问题无解。

 1 #include <iostream>
 2 #include <fstream>
 3 #include <assert.h>
 4
 5 using namespace std;
 6
 7 struct Node {
 8     Node(): val(0), next(0) {}
 9     Node( int value ): val(value), next(0) {}
10     int val;
11     Node *next;
12 };
13
14 void removeNode( Node *node );
15
16 int main( int argc, char *argv[] ) {
17     string data_file = "./data.txt";
18     ifstream ifile( data_file.c_str(), ios::in );
19     if( !ifile.is_open() ) {
20         fprintf( stderr, "cannot open file: %s\n", data_file.c_str() );
21         return -1;
22     }
23     int n = 0, k = -1;
24     while( ifile >>n >>k ) {
25         assert( n > 0 && k > 0 && k < n );
26         Node guard, *node = &guard, *target = 0;
27         for( int i = 0; i < n; ++i ) {
28             node->next = new Node();
29             node = node->next;
30             ifile >>node->val;
31             cout <<node->val <<" ";
32             if( i == k-1 ) { target = node; }
33         }
34         cout <<endl;
35         if( target )
36             cout <<"target to remove: " <<target->val <<endl;
37         else
38             cout <<"target to remove: null" <<endl;
39         removeNode( target );
40         node = guard.next;
41         cout <<"result:" <<endl;
42         while( node ) {
43             Node *next = node->next;
44             cout <<node->val <<" ";
45             delete node;
46             node = next;
47         }
48         cout <<endl <<endl;
49     }
50     ifile.close();
51     return 0;
52 }
53
54 void removeNode( Node *node ) {
55     assert( node && node->next );
56     node->val = node->next->val;
57     while( node->next->next ) {
58         node = node->next;
59         node->val = node->next->val;
60     }
61     delete node->next;
62     node->next = 0;
63     return;
64 }

测试文件

2 1
1 3

3 1
1 2 3

3 2
1 2 3

7 1
14 54 96 23 45 12 45

7 2
14 54 96 23 45 12 45

7 6
14 54 96 23 45 12 45
时间: 2024-07-30 13:33:07

链表 2.3的相关文章

c语言动态链表的创建

创建动态连链表就是将一个个节点连接起来 (1)动态生成节点 (2)输入节点数据 (3)将节点链在一起 例: typedef struct Data { char num[20]; char name[10]; char sex; float english; float chinese; float math; }; typedef struct Node { struct Data data;//结构体类型//结构体嵌套 struct Node* next;//结构体指针型 }node,*Pn

算法学习——单链表快排

/**  * 以p为轴对start-end间的节点进行快排(包括start && 不包括end):  * 思路:  * 1.将头节点作为轴节点start,从start.next开始遍历,如果节点小于轴start的值,将该节点插入到轴节点后面:  * 2.将轴节点插入合适位置,即找到最后一个小于轴的节点,将该节点与轴节点值互换,此时就链表分为两部分,小于轴节点和大于轴节点:  * 3.递归的遍历2中两部分节点.  *   * @param p  * @param start  * @para

【数据结构】之散列链表(Java语言描述)

散列链表,在JDK中的API实现是 HashMap 类. 为什么HashMap被称为"散列链表"?这与HashMap的内部存储结构有关.下面将根据源码进行分析. 首先要说的是,HashMap中维护着的是一个数组: transient Node<K,V>[] table; ,数组中的每个元素都是一个 Node 对象.这里的Node是HashMap的一个内部类,代码如下: static class Node<K,V> implements Map.Entry<

单链表逆置

重写单链表逆置,熟能生巧- #include <iostream> #include <cstdlib> using namespace std; typedef struct List{ int num; struct List *next; }ListNode,*pListNode; void display(ListNode *pHead) { while(pHead) { cout<<pHead->num<<"--"; pH

java-------单链表

单链表: * 1.链表可以是一种有序或无序的列表 * 2.链表的内容通常存储在内存中分散的为止 * 3.链表由节点组成,每一个节点具有相同的结构 * 4.节点分为数据域和链域,数据域存放节点内容,链域存放下一个节点的指针 package myLinkList; public class MyLinkedList<T> { /** *Node:节点对象 * 包括数据域data和链域next(指向下一个节点对象) */ class Node { private T data; private No

数据结构之链表

---恢复内容开始--- 1:有头节点.单向.不循环链表 对于这种有头节点的单向不循环链表插入数据如下图: 1)头部插入 2)尾部插入 代码如下 : 1 #include "stdafx.h" 2 #include<iostream> 3 4 using namespace std; 5 6 typedef int DATA; 7 typedef struct node* LIST; 8 9 struct node 10 { 11 DATA data; 12 struct

拿java写了一个有点像指针的单链表

public class LinkList { private Node firstNode; private Integer position; public LinkList() {  super(); } public LinkList(Node firstNode) {  super();  this.firstNode = firstNode; } public Node getFirstNode() {  return firstNode; } public void setFirs

02 单链表

线性表之链式存储---单链表 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 // 数据结构 6 typedef struct node 7 { 8 int data; 9 struct node *next; 10 }linkList; 11 12 // 创建单链表,并初始化 13 linkList *linkList_init(void) 14 { 15 linkList *l

(单链表)单链表的整体逆序和局部逆序

题目一:将单链表翻转. 思路:有三种方式. 一:用数组存储单链表的值,然后重新逆序赋值,效率较低. 二:利用三个指针,在原来的基础上进行逆序.这种方法比较实用,效率也高. 三:从第2个节点到第N个节点,依次逐节点插入到第1个节点(head节点)之后,最后将第一个节点挪到新表的表尾.需要新建一个链表,这种方法和第二种差不多. 这里我就写出第二种方法,比较实用. 代码(方法二): struct ListNode { int val; ListNode *next; ListNode(int x) :

链表操作法则之逆向遍历与倒置算法

一.创建链表: 对链表进行操作的所有算法的前提,就是我们首先要创建一个链表,我们可以选择正向建链和逆向建链: (一).正向建链: 首先,我们得自定义节点类型: typedef struct Node { int data;//数据域 struct Node * pNext;//指针域 }NODE,*PNODE; 通过数组进行链表数据域的赋值: int main (void) { PNODE pHead;//头指针,接收创建链表时返回的头结点地址 int a[8] = {12,37,49,65,2