数据结构学习日记—数组实现线性表
前言
大二下我们专业就要学习数据结构了,刚好报名了蓝桥杯,就想着接着这个寒假学习一下数据结构和算法,应该算是从零开始吧。之前也没有接触过这方面的内容,/(ㄒoㄒ)/~~。祝我一切顺利。这部分的内容我选择看书学习,数据结构看的是《大话数据结构》。
1.线性表的定义
线性表:零个或多个数据元素的有限序列
这里有几个关键的地方:
1.序列。也就是说线性表是有顺序的。第一个元素无前驱,最后一个元素无后继,中间的每个元素都有一个前驱,一个后继。
2.有限。线性表中的元素数目是一定的,就算是十分大也是一个确定的数。
生活中的线性表的例子就比较多了:排队,花名册,都可以看作线性表。这次我们先通过数组来实现线性表的一些基本操作
2.线性表的操作
1.InitList 初始化操作,建立一个空的线性表
2.ListEmpty 若线性表为空,返回true 否则返回 false (这里我更喜欢叫isEmpty)
3.ClearList 将线性表清空
4.GetElem(L,i) 将线性表中的第i个元素返回
5.LocateElem(L,e) 在线性表中查找与e信息相等的元素,查找成功返回小标,查找失败返回-1
6.ListInsert(L,i,e) 在线性表下标为i的位置插入元素e
7.ListDelete(L,i) 将线性表中下标为i的元素删除 并返回背删除的元素
8.ListLength(L) 返回线性表中元素的个数
这些都是我们在后面要实现的函数,因为《大话数据结构》是用c语言写的,而我蓝桥杯选的是Java,所有我决定,用Java和Go都写一遍。因为Go的语法和C比较像,同时也是想对比着两类编程语言学习一下。
3.功能实现
0.数据准备
var (
listLength int = -1
)
const arrayLength int = 10
listLength:当列表最后一个元素的下标
arrayLength: 数组的长度,需要设置为常量
type node struct {
no int
name string
}
1.InitList()
func initList() *[arrayLength]node {
var arrayList [arrayLength]node
var p *[arrayLength]node = &arrayList
return p
}
创建了一个长度为10的数组,数组里的元素是node。将arrayList的地址赋值给指针p。go中数组的长度也是数组类型的一部分,所有这里需要带上。然后返回创建数组的指针。
2.isEmpty()
func isEmpty(arrayList *[arrayLength]node) bool {
return len(arrayList) == 0
}
这里我先开始是用if判断的,go一直给我提醒,告诉我用 len(arrayList) == 0。编译器在教我做事,卧槽。
3.clearList()
func clearList(arrayList *[arrayLength]node) {
for i := 0; i <= listLength; i++ {
arrayList[i] = node{}
}
}
将列表里的数据遍历,然后置空。
4.getElem()
func getElem(arrayList *[arrayLength]node, i int) node {
return arrayList[i]
}
直接根据下标在数组里取元素。
5.locateElem
func locateElem(arrayList *[arrayLength]node, temp node) int {
for i := 0; i < len(arrayList); i++ {
if arrayList[i].name == temp.name {
return i
}
}
return -1
}
传入一个node,遍历arrayList如果有相符合的,可以直接返回下标,如果没有找到 返回 -1
6.addElem()
func addElem(arrayList *[arrayLength]node, temp node) {
if listLength == arrayLength {
fmt.Println("列表已满")
return
}
listLength++
arrayList[listLength] = temp
}
添加元素前,想判断列表的长度是否和数组的长度是否相等。即判断数组是否满了。然后listLength加一,然后添加元素。
7.listInsert()
func listInsert(arrayList *[arrayLength]node, index int, temp node) {
if listLength == arrayLength {
fmt.Println("列表已满")
return
}
for i := listLength; i >= index-1; i-- {
arrayList[i+1] = arrayList[i]
}
arrayList[index-1] = temp
listLength++
}
先判断数组是否满了,然后把每个元素往后移最后将要插入的元素放进去,最后listLength加一。
8.listDelete()
func listDelete(arrayList *[arrayLength]node, temp node) {
for i := 0; i <= listLength; i++ {
if arrayList[i].name == temp.name {
for j := i; j <= listLength; j++ {
arrayList[j] = arrayList[j+1]
}
break
}
}
arrayList[listLength] = node{}
listLength--
}
先遍历,找到符合条件的节点,然后把符合节点之后的所有节点前移。然后将最后一个节点置零,listLength减1。
9.listDisplay()
func listDisplay(arrayList *[arrayLength]node) {
if isEmpty(arrayList) {
fmt.Println("列表为空")
return
}
for i := 0; i <= listLength; i++ {
fmt.Println(arrayList[i])
}
}
先判断列表是否为空,为空直接退出。再遍历列表,打印每个节点的信息。
总和
package main
import "fmt"
type node struct {
no int
name string
}
var (
listLength int = -1
)
const arrayLength int = 10
// 通过
// 初始化列表返回一个数组指针
func initList() *[arrayLength]node {
var arrayList [arrayLength]node
var p *[10]node = &arrayList
return p
}
// 通过
// 判断列表是否为空
func isEmpty(arrayList *[arrayLength]node) bool {
return len(arrayList) == 0
}
// 通过
// 顺利置零
func clearList(arrayList *[arrayLength]node) {
for i := 0; i <= listLength; i++ {
arrayList[i] = node{}
}
}
// 通过
// 获取相应下标的元素
func getElem(arrayList *[arrayLength]node, i int) node {
return arrayList[i]
}
// 通过
// 根据元素 获取下标
func locateElem(arrayList *[arrayLength]node, temp node) int {
for i := 0; i < len(arrayList); i++ {
if arrayList[i].name == temp.name {
return i
}
}
return -1
}
// 通过
// 添加元素
func addElem(arrayList *[arrayLength]node, temp node) {
if listLength == arrayLength {
fmt.Println("列表已满")
return
}
listLength++
arrayList[listLength] = temp
}
// 通过
// 插入元素
func listInsert(arrayList *[arrayLength]node, index int, temp node) {
if listLength == arrayLength {
fmt.Println("列表已满")
return
}
for i := listLength; i >= index-1; i-- {
arrayList[i+1] = arrayList[i]
}
arrayList[index-1] = temp
listLength++
}
// 通过
// 删除元素
func listDelete(arrayList *[arrayLength]node, temp node) {
for i := 0; i <= listLength; i++ {
if arrayList[i].name == temp.name {
for j := i; j <= listLength; j++ {
arrayList[j] = arrayList[j+1]
}
break
}
}
arrayList[listLength] = node{}
listLength--
}
// 通过
// 打印数组
func listDisplay(arrayList *[arrayLength]node) {
if isEmpty(arrayList) {
fmt.Println("列表为空")
return
}
for i := 0; i <= listLength; i++ {
fmt.Println(arrayList[i])
}
}
func main() {
n1 := node{1, "a"}
n2 := node{2, "b"}
n3 := node{3, "c"}
n4 := node{4, "d"}
n5 := node{5, "e"}
arrayList := initList()
addElem(arrayList, n1)
addElem(arrayList, n2)
addElem(arrayList, n4)
addElem(arrayList, n5)
listDisplay(arrayList)
fmt.Println("数组里的元素个数:", listLength+1)
listInsert(arrayList, 3, n3)
listDisplay(arrayList)
listDelete(arrayList, n5)
fmt.Println("删除后")
listDisplay(arrayList)
fmt.Println("数组里的元素个数:", listLength+1)
fmt.Println("列表为空:", isEmpty(arrayList))
fmt.Println(getElem(arrayList, 3))
fmt.Println(locateElem(arrayList, n1))
clearList(arrayList)
}
数组实现线性表数组 go语言的实现方式应该就是上面这样的了。应该还可以再优化一下。
4.Java实现
package _01_array_line_table;
public class ArrayLineTableDemo {
public static void main(String[] args) {
ArrayLineTable arrayListTable = new ArrayLineTable(5);
Node node1 = new Node(1, "a");
Node node2 = new Node(2, "b");
Node node3 = new Node(3, "c");
Node node4 = new Node(4, "d");
Node node5 = new Node(5, "e");
arrayListTable.addItem(node1);
// arrayListTable.display();
arrayListTable.addItem(node2);
arrayListTable.addItem(node3);
arrayListTable.addItem(node4);
arrayListTable.addItem(node5);
arrayListTable.display();
System.out.println("删除操作");
arrayListTable.deleteItem(1);
arrayListTable.display();
System.out.println("修改操作");
arrayListTable.alterItem(2, "修改");
arrayListTable.display();
System.out.println("插入操作");
Node nodeInsert = new Node(8,"insert");
arrayListTable.insertItem(2, nodeInsert);
arrayListTable.display();
}
}
class ArrayLineTable{
private int lineLength;
private int tableLength;
private Node[] list;
public ArrayLineTable(int tableLength) {
// TODO Auto-generated constructor stub
this.lineLength = -1;
list = new Node[tableLength];
this.tableLength = tableLength;
}
// 是否为空
public boolean isEmpty() {
if(lineLength == -1) {
return true;
}
return false;
}
// 是否已经满了
public boolean isFull() {
if (lineLength ==tableLength) {
return true;
}
return false;
}
// 添加元素
public void addItem(Node node) {
if (isFull()) {
System.out.println("线性表已满");
return;
}
lineLength++;
list[lineLength] = node;
System.out.println("添加成功");
}
// 删除元素
public void deleteItem(int no) {
int index = findItem(no);
if (index == -1) {
return;
}
for(int i = index;i<=lineLength-1;i++) {
list[i]=list[i+1];
}
list[lineLength]=null;
lineLength--;
System.out.println("删除成功");
}
// 修改元素
public void alterItem(int no,String name) {
int index = findItem(no);
if (index == -1) {
return;
}
list[index].setName(name);
System.out.println("修改成功");
}
// 查找元素
public int findItem(int no) {
if (isEmpty()) {
System.out.println("列表为空");
return -1;
}
for(int i=0;i<=lineLength;i++) {
if (list[i].getNo() == no) {
return i;
}
}
System.out.println("未查找到该元素");
return -1;
}
// 插入元素
public void insertItem(int no,Node node) {
if (isFull()) {
System.out.println("列表已满");
return;
}
if (no<0 || no>lineLength||no>tableLength) {
System.out.println("请检查你输入的下标");
return;
}
for(int i = lineLength;i<=no;i--) {
list[i+1]=list[i];
}
list[no]=node;
}
// 打印列表
public void display() {
if (isEmpty()) {
System.out.println("列表为空");
return;
}
for(int i =0;i<=lineLength;i++) {
System.out.println(list[i]);
}
}
}
class Node{
private int no;
private String name;
// 构造器
public Node(int no,String name) {
this.no=no;
this.name=name;
}
public int getNo() {
return no;
}
public String getName() {
return name;
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "no:"+this.no+"name:"+this.name;
}
}