会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132485个问题

后边试着加了几个删除的方法,指点一波,其中有一块不太确定,代码中写了

class Node():
    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next

class Linklist():
    def __init__(self):
        self.root = Node()
        self.size = 0
        self.next = None

    def append(self,value):
        node = Node(value)
        if not self.next:
            self.root.next = node
        else:
            self.next.next = node
        self.size += 1
        self.next = node

    def append_first(self,value):
        '''
        最初
        root -> 1
        我理解的:
        2 -> root -> 1 (2为新增元素)
        如果是这样的话  可代码表示的就不一样了
        代码:
        root -> 2 -> 1
        很久没看书了,忘记了这一块的根节点用法了,是不是所谓的根节点root就是一个空,一个指针?
        '''
        node = Node(value)
        if not self.root.next:
            self.root.next = node
        else:
            tmp = self.root.next
            self.root.next = node
            node.next = tmp
        self.size += 1

    def remove_first(self):
        if not self.root.next:
            print('no node in link')
            pass
        else:
            '''
            最初:
            root -> 蔡文姬 -> 扁鹊 —> 阿珂
            删除头节点:
            root  -> 扁鹊 —> 阿珂
            '''
            self.root.next = self.root.next.next
            self.size -= 1


    def remove(self):
        '''
        从最后删除
        '''
        if not self.root.next:
            print('remove no node in link')
            pass
        else:
            current = self.root
            while current.next is not self.next:
                current = current.next
            self.next = current
            self.next.next = None
            self.size -= 1

    def remove_value(self,value):
        '''
        删除指定元素
        '''
        if not self.root.next:
            print('remove_value no node in link')
            pass
        else:
            if self.next.value == value:
                self.remove()
            elif self.root.next.value == value:
                self.remove_first()
            else:
                current = self.root.next
                while current.next.value != value:
                    current = current.next
                current.next = current.next.next
                self.size -= 1



    def remove_size(self,key):
        '''
        删除指定位置
        :param key: an int
        '''
        if key > self.size:
            print('size not enough')
        elif key == 1:
            self.remove_first()
        elif key == self.size:
            self.remove()
        else:
            '''
            感觉这一块不太对,但又不知道怎么搞
            '''
            current = self.root
            current_num = 1
            while current_num != key:
                current = current.next
                current_num += 1
            current.next = current.next.next
            self.size -= 1

    def __iter__(self):
        if not self.root.next:
            print('no node in link')
            pass
        else:
            current = self.root.next
            while current is not self.next:
                yield current.value
                current = current.next
            yield current.value


if __name__ == '__main__':
    link = Linklist()
    link.append('阿珂')
    link.append_first('扁鹊')
    link.append_first('蔡文姬')
    # link.remove_first()
    # link.remove_first()
    # link.remove()
    # link.remove_value('蔡文姬')
    link.remove_size(2)

    for v in link:
        print(v)


Python 全系列/第十六阶段:数据结构与算法/算法与数据结构(旧) 29821楼

屏幕截图 2023-03-11 162711.png

老师为啥,我这个打印结果跟不同

JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 29822楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 29823楼
Python 全系列/第一阶段:Python入门/编程基本概念 29825楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 29828楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 29829楼

image.png

JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 29833楼

如果在initialValue()方法内部加上Thread.sleep(500)的话,运行结果显示三个线程还是在共享着同一个连接,那不还是线程不安全吗?

运行效果图如下所示:

image.png

代码如下所示:

package com.bjsxt;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * <b style = "font-size:15px;">用于获得与数据库的连接以及关闭连接</b>
 * @author 郑锦宗
 * @version v1.0
 */
public class DBUtil {
	private static final String DRIVER="com.mysql.jdbc.Driver";
	private static final String	USER="root";
	private static final String PWD="root";
	private static final String URL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
	
	private static Connection conn=null;
	//定义一个数据库连接
	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>() {
		protected Connection initialValue() {
			try {
				Class.forName(DRIVER);
				
				if(conn==null){
					conn=DriverManager.getConnection(URL, USER, PWD);
				}
				
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return conn;
		}
	};
	
	//获取连接
	public static Connection getConnection(){
		return tl.get();
	}
	//关闭连接的方法
	public static void colseConnection(){
		if (conn!=null) {
			try {
				conn.close();
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	public static void main(String[] args) {
		System.out.println(getConnection());
	}
}


源码压缩包如下所示:

源码压缩包.zip


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 29835楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637