数据结构是相互之间存在一种或多种特定的数据元素的集合。
集合,线性结构,树形结构,图状结构
算法:
算法的评价标准:运行时间 占用空间 相互对立。有时需要牺牲空间来换取时间,有时需要牺牲时间来换取空间
其他方面:正确性,可读性,健壮性
数据结构是相互之间存在一种或多种特定的数据元素的集合。
集合,线性结构,树形结构,图状结构
算法:
算法的评价标准:运行时间 占用空间 相互对立。有时需要牺牲空间来换取时间,有时需要牺牲时间来换取空间
其他方面:正确性,可读性,健壮性
class Node
{
private T data;//存储数据
private Node<T>next;//指针,用来指向下一个元素
public Node()
{
data = default(T);
next = null;
}
public Node(T value)
{
data = value;
next = null;
}
public Node(T value,Node<T>next)
{
this.data = value;
this.next = next;
}
public Node(Node<T>next)
{
this.next = next
}
public T Data
{
get{return data;}
set{data = value;}
}
public Node<T> Next
{
get{return next;}
set{next = value;}
}
}
public static void Main(string[] args){ }
Replace方法
public static StringDS Replace(StringDS s, StringDS t, StringDS r)//简单方法是用死循环带入indexof方法遍历T的位置,再用substring替换位置
{
StringDS message = new StringDS("0");
for (int i = 0; i <= s.GetLength() - t.GetLength(); i++)
{
bool isEqual = true;
for (int j = i; j < i + t.GetLength(); j++)
{
if (s[j] != t[j - i])
{
isEqual = false;
}
}
if (isEqual)
{
s.SubString(i, t.GetLength());
return s;
}
else
{
continue;
}
}
return message;
}
个人理解
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp15
{
/// <summary>
/// 顺序表实现方式
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SeqList<T> : IListDS<T>//继承泛型接口
{
private T[] data;//用来存储数据
private int count = 0;//表示存了多少个数据
public SeqList(int size)//size就是最大容量
{
data = new T[size];
count = 0;
}
public SeqList() : this(10)
{
}
public int GetLength()//获取长度
{
return count;
}
public void Clear() //清空操作
{
count = 0;
}
public bool IsEmpty()//判断线性表是否为空
{
return count == 0;
}
public void Add(T item) //附加操作
{
//如果下标等于集合最多能存的数量
if (count == data.Length) Console.WriteLine("当前顺序表已存满,不允许再存入");
else
{
data[count] = item;//如果没有存满,那么再结尾出存入要添加的数据
count++;//然后当前数据的下标+1
}
}
public void Insert(T item, int index) //插入操作 要存的数据 要在哪个下标存
{
for (int i = count - 1; i >= index; i--) //i等于当前下标-1 i大于等于要存存储的位置 递减
data[i + 1] = data[i]; //集合当前下标的下一个位置的数据等于当前下标的数据 就是被插入的位置的数据往后挪动
data[index] = item; //然后要插入的 数据等与要插入的位置
count++; //集合的大小加一
}
public T Delete(int index) //删除操作
{
T temp = data[index];//temp表示要删除的元素地址
for (int i = index + 1; i < count; i++)//i=要删除的元素地址+1 i<数组的长度 每次加1
{
data[i - 1] = data[i];//如果i是5 data[i-1]是4
}
count--;
return temp;
}
public T this[int index]//取表元
{
get { return GetEle(index); }
}
public T GetEle(int index)//定义一个索引器 获取元素
{
//如果到查询的位置大于等于0 并且小于等于最大位置 那么返回这个元素
if (index >= 0 && index <= count - 1) return data[index];
//如果这个位置不符合上面的条件 等于查询的元素不在这个范围内 那么返回默认值
else { Console.WriteLine("索引不存在"); return default(T); }
}
public int Locate(T value) //按值查找
{ //遍历当前集合 //如果集合的第i个等于当前要查询的值 那么返回i
for (int i = 0; i < count; i++) if (data[i].Equals(value)) return i;
return -1;//没有查到就返回一个空
}
}
}
数据结构:
1.集合,线性结构,树形结构与图形结构
2.算法可以是认为解决问题的,算法师基于数据结构的
3.数据结构是问题的核心,是算法的基础
4.数据结构的评价标准是:运行时间,占有空间,
有时需要牺牲空间换时间(空间大,时间短),有的需要牺牲时间换空间(时间长,空间小)
线性表的实现方式
1.顺序表
2.单链表
3.双向链表
4.循环链表
线性表的两种实现方式:
顺序表
单链表
List<T> 是C#内部实现的一个线性表
数据结构的分类:
1.集合
2.线程结构
3.树形结构
4.图状结构
什么是算法
算法和数据结构的关系
stack 栈 先进后出
Push 入
Peek 出删除
Peek 出 不删除
线性表 list<T>