1------双指针

指针问题

在这里插入图片描述

1.用指针合并两个数组

# 用指针合并两个数组
arr_1 = [1, 4, 3, 6, 10]
arr_2 = [2, 5, 8, 11]
arr_1.sort()  # 对两个数组进行排序
arr_2.sort()
ind = 0  # 指针初始为0
ans = arr_1.copy()
for i in range(0, len(arr_2)):
    while ind < len(arr_1):
        if arr_2[i] <= arr_1[ind]:
            ans.insert(i + ind, arr_2[i])
            # 插入的时候因为插入位置是前面插入数插入后的位置所以是 i + ind 例如把8插入i=2;ind=4前面插入了2,5所以在ans中应该插在6的位置=i+ind
            break
        else:
            ind += 1
    else:
        ans = ans + arr_2[i:]  # 遍历完了arr_1还没有说明从i以后都比arr_1大所以都接在arr_1后面
        break
print(ans)

2.用指针实现数组的二分查找

# 用指针实现数组的二分查找
numbers = [3, 1, 5, 7, 6, 8, 13, 15, 14, 17, 18, 24, 30, 43, 56]
numbers.sort()
head, tail = 0, len(numbers)  # 数组的长度刚好是最大下标值+1
search = int(input("Enter a number to search"))

while tail - head > 1:
    mid = (head + tail) // 2
    if numbers[mid] > search:
        tail = mid
    if numbers[mid] < search:
        head = mid + 1  # mid指向的元素小于search所以不要把它包含在范围内
    if numbers[mid] == search:
        ans = mid
        break
else:
    if search == numbers[head]:
        ans = head
    else:
        ans = -1  # 数组中没有这个元素输出-1

print("你要找的数的位置", ans)
print("你要找的数是", numbers[ans])

3.输出单链表
在这里插入图片描述

3.1输出一个由两个列表组成的单链表

# 输出一个由两个列表组成的单链表
listvalue = [1, 5, 6, 2, 4, 3]
listpointer = [3, 2, -1, 5, 1, 4]
head = 0
print(listvalue[head])
next = listpointer[head]
while next != -1:
    print(listvalue[next])
    next = listpointer[next]

3.2输出一个列表套列表组成的单链表

# 输出一个列表套列表组成的单链表
linkedlist = [[1, 3], [5, 2], [6, -1], [2, 5], [4, 1], [3, 4]]
value = 0  # linkedlist[m][0]表示第m+1个小数组中储存的值
pointer = 1  # linkedlist[m][1]表示第m+1个小数组中储存的指针
head = 0
print(linkedlist[head][value])  # 输出第一个元素的值
next = linkedlist[head][pointer]  # 给next赋初始值
while next != -1:
    print(linkedlist[next][value])  # 输出下一个元素中储存的值
    next = linkedlist[next][pointer]  # 指针变成下一个元素中储存的指针

4.输出双链表

4.1输出一个由三个列表组成的双链表

# 输出一个由三个列表组成的双链表
listvalue = [1, 5, 6, 2, 7, 3]
listright = [3, 2, 4, 5, -1, 1]
listleft = [-1, 5, 1, 0, 2, 3]

head = listleft.index(-1)
print(listvalue[head])
next = listright[head]

while next > -1:
    print(listvalue[next])
    next = listright[next]
 

4.2输出一个列表套列表组成的双链表

# 输出一个列表套列表组成的双链表
linkedlist = [[1, 3, -1], [5, 2, 5], [6, 4, 1], [2, 5, 0], [7, -1, 2], [3, 1, 3]]
value = 0
right = 1
left = 2
head = 0
print(linkedlist[head][value])
next = linkedlist[head][right]
while next > -1:
    print(linkedlist[next][value])
    next = linkedlist[next][right]

4.3双向输出双链表

# 双向输出双链表
listvalue = [1, 5, 6, 2, 7, 3]
listright = [3, 2, 4, 5, -1, 1]
listleft = [-1, 5, 1, 0, 2, 3]

head = listleft.index(-1)
print(listvalue[head])
next = listright[head]

while next > -1:
    print(listvalue[next])
    next = listright[next]

head = listright.index(-1)
print(listvalue[head])
next = listleft[head]

while next > -1:
    print(listvalue[next])
    next = listleft[next]

5.链表中插入元素

先让插入元素的指针指向下一个元素再使前一个元素的指针指向下一个元素

否则:
在这里插入图片描述

5.1在单链表中插入元素

# 在单链表中插入元素
def output(listvalue, listright, head):
    print(listvalue[head])
    next = listright[head]

    while next > -1:
        print(listvalue[next])
        next = listright[next]


listvalue = [1, 5, 6, 2, 7, 3]
listright = [3, 2, 4, 5, -1, 1]
head = 0
prepos = 5

output(listvalue, listright, head)
print()

listvalue.append(4)
listright.append(listright[prepos]) # 先指向后一个元素
listright[prepos] = len(listvalue) - 1 # 再将前一个元素的指针指向插入的元素

output(listvalue, listright, head)

5.2在双链表中插元素

# 在双链表中插元素
def output1(listvalue, listright, head1):
    print(listvalue[head1])
    next = listright[head1]

    while next > -1:
        print(listvalue[next])
        next = listright[next]

def output2(listvalue, listleft, head2):
    print(listvalue[head2])
    next = listleft[head2]

    while next > -1:
        print(listvalue[next])
        next = listleft[next]


listvalue = [1, 5, 6, 2, 7, 3]
listright = [3, 2, 4, 5, -1, 1]
listleft = [-1, 5, 1, 0, 2, 3]
head1 = 0
head2 = 4
prepos = 5

output1(listvalue, listright, head1)
output2(listvalue, listleft, head2)
print()

listvalue.append(4)
listright.append(listright[prepos])  # 给新元素的两个指针赋值
listleft.append(prepos)
listleft[listright[prepos]] = len(listvalue) - 1  # 将前后两个元素的指针指向新元素
listright[prepos] = len(listvalue) - 1
# 必须先left再right     

output1(listvalue, listright, head1)
output2(listvalue, listleft, head2)

6.删除链表中的元素
在这里插入图片描述

6.1删除单链表中的元素

# 删除单链表中的元素(删除值为5的元素)
def output(listvalue, listright, head):
    print(listvalue[head])
    next = listright[head]

    while next > -1:
        print(listvalue[next])
        next = listright[next]


listvalue = [1, 5, 6, 2, 7, 3]
listright = [3, 2, 4, 5, -1, 1]
head = 0
prepos = 5  # 确定要删除的元素的前一个元素的位置

output(listvalue, listright, head)
print()

listright[prepos] = listright[listright[prepos]]

output(listvalue, listright, head)

6.2删除双链表的元素
在这里插入图片描述

# 删除双链表的元素(删除值为5的元素)
listvalue = [1, 5, 6, 2, 7, 3]
listright = [3, 2, 4, 5, -1, 1]
listleft = [-1, 5, 1, 0, 2, 3]
head = 0
prepos = 5  # 确定前一个元素的位置
listright[prepos] = listright[listright[prepos]]  # 前一个元素的right指针指向后一个元素
listleft[listright[listright[prepos]]] = prepos  # 把后一个元素的left指针指向前一个元素

pycharm中:
快捷键为‘Ctrl + Alt + L’。
码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再次按下Ctrl+/,注释被取消

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值