GO作业:单向链表增删插入遍历

该博客介绍了如何使用Go语言创建一个单向链表,并详细讲解了如何进行在链表前、中、后位置的插入操作,以及如何删除指定节点和遍历整个链表的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用随机数生成结构体链表,然后实现在前面插入,在后面插入,在中间插入,指定节点删除并遍历等。

package main

import (
	"fmt"
	"math/rand"
)

type Student struct {
	Name  string
	Age   int
	score float32
	next  *Student
}

func trans(head *Student) {
	p := head
	for p != nil {
		fmt.Println(*p)
		p = p.next
	}
}

func listLen(head *Student) int {
	var p *Student = head
	lenth := 0
	for p != nil {
		lenth++
		p = p.next
	}
	return lenth
}

func insertHead(p *Student) *Student {
	for i := 0; i < 10; i++ {
		stu := Student{
			Name:  fmt.Sprintf("stu%d", i),
			Age:   rand.Intn(100),
			score: rand.Float32() * 100,
		}
		stu.next = p
		p = &stu
	}
	return p
}

func insertTail(p *Student) *Student {
	var head *Student = p
	for i := 0; i < 10; i++ {
		stu := Student{
			Name:  fmt.Sprintf("stu%d", i),
			Age:   rand.Intn(100),
			score: rand.Float32() * 100,
		}
		if head != nil {
			p.next = &stu
			p = p.next
		} else {
			head = &stu
			p = head
		}
	}
	return head
}

func delNode(head *Student, toDel int) *Student {
	var p *Student = head
	var pre, tmpNext *Student
	lenth := listLen(head)
	if toDel > lenth || toDel < 1 {
		fmt.Println("No item was deleted.")
		return head
	} else if toDel == 1 {
		head = p.next
	} else {
		for i := 1; i < toDel; i++ {
			pre = p
			tmpNext = p.next
			p = p.next
		}
		pre.next = tmpNext.next
	}
	return head
}

func insertMid(head, toInsItem *Student, toIns int) *Student {
	var p *Student = head
	var pre *Student
	lenth := listLen(head)
	if toIns > lenth+1 || toIns < 0 {
		fmt.Println("No item was inserted.")
		return head
	} else {
		switch toIns {
		case lenth + 1:
			for {
				if p != nil {
					pre = p
					p = p.next
				} else {
					pre.next = toInsItem
					break
				}
			}
		case 0, 1:
			toInsItem.next = head
			head = toInsItem
		default:
			for i := 1; i < toIns; i++ {
				pre = p
				p = p.next
			}
			pre.next = toInsItem
			toInsItem.next = p
		}
	}
	return head
}

func main() {
	var head1, head2 *Student
	fmt.Println("在链表前面插入========")
	head1 = insertHead(head1)
	trans(head1)
	fmt.Println("Current insertHead list1 lenth is: ", listLen(head1))
	fmt.Println("在链表后面插入========")
	head2 = insertTail(head2)
	trans(head2)
	fmt.Println("Current insertTail list2 lenth is: ", listLen(head2))

	fmt.Println("删除链表list2第3个元素========")
	head2 = delNode(head2, 3)
	trans(head2)
	fmt.Println("After delete, the list2 lenth is: ", listLen(head2))
	fmt.Println("删除链表list2第5个元素========")
	head2 = delNode(head2, 5)
	trans(head2)
	fmt.Println("After delete, the list2 lenth is: ", listLen(head2))
	var stuadd1 = Student{
		Name:  "zgx",
		Age:   46,
		score: 90.3,
	}
	var stuadd2 = Student{
		Name:  "hy",
		Age:   45,
		score: 80.1,
	}
	var stuadd3 = Student{
		Name:  "zxy",
		Age:   17,
		score: 100.0,
	}
	fmt.Println("在链表list2的第一个位置增加一个元素========")
	head2 = insertMid(head2, &stuadd1, 0)
	trans(head2)
	fmt.Println("After insert, the list2 lenth is: ", listLen(head2))
	fmt.Println("在链表list2的第十的位置增加一个元素========")
	head2 = insertMid(head2, &stuadd2, 10)
	trans(head2)
	fmt.Println("After insert, the list2 lenth is: ", listLen(head2))
	fmt.Println("在链表list2的第三个位置增加一个元素========")
	head2 = insertMid(head2, &stuadd3, 3)
	trans(head2)
	fmt.Println("After insert, the list2 lenth is: ", listLen(head2))
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值