数据结构与算法(C#)--线性表

1.1、 线性表的顺序存储结构
数组
时间复杂度为 O(1)

1.2、 线性表的链式存储结构
1.2.1、单链表

namespace 单链表
{
  class Program
  {
    static void Main(string[] args)
    {
      LinkedList ld = new LinkedList();
      ld.Insert("ZOU", "header");
      ld.Insert("YONG", "ZOU");
      ld.Insert("LOVE", "YONG");
      ld.Insert("HELLO", "LOVE");
      ld.Insert("WORLD", "HELLO");
      ld.PrintList();
      Console.ReadLine();
    }
  }
  public class Node//节点类
  {
    public Object Element;
    public Node Link;
    public Node()
    {
      Element = null;
      Link = null;
    }
    public Node(object theElement)
    {
      Element = theElement;
      Link = null;
    }
  }
  public class LinkedList//创建链表中节点之间的链接
  {
    protected Node header;
    public LinkedList()
    {
      header = new Node("header");
    }
    private Node Find(Object item) //查找节点
    {
      Node current = new Node();
      current = header;
      while (current.Element != item)
      {
        current = current.Link;
      }
      return current;
    }
    public void Insert(Object newItem, Object after) //插入节点
    {
      Node current = new Node();
      Node newNode = new Node(newItem);
      current = Find(after);
      newNode.Link = current.Link;
      current.Link = newNode;
    }
    private Node FindPrevious(Object n)//查找之前节点
    {
      Node current = header;
      while (!(current.Link == null) && (current.Link.Element != n))
        current = current.Link;
      return current;
    } 
    public void Remove(Object n) //移除节点
    {
      Node p = FindPrevious(n);
      if (!(p.Link==null))
      {
        p.Link = p.Link.Link;
      }
    }
    public void PrintList()//输出节点
    {
      Node current = new Node();
      current = header;
      while (!(current.Link == null))
      {
        Console.WriteLine(current.Link.Element);
        current = current.Link;
      }
    }
  }
}

1.2.2、双向链表

双链表上删除结点
    *p:p->prior->next=p->next;//1
    p->next->prior=p->prior;//2
namespace 双向链表
{
    class Program
    {
      static void Main(string[] args)
      {
        LinkedList ld = new LinkedList();
        ld.Insert("WORLD", "header");
        ld.Insert("HELLO", "WORLD");
        ld.Insert("LOVE", "HE");
        ld.Insert("YONG", "LOVE");
        ld.Insert("ZOU", "YONG");
        ld.PrintReverse();
        Console.ReadLine();
      }
    }
  public class Node//节点类
  {
    public Object Element;
    public Node Flink;
    public Node Blink;
    public Node()
    {
      Element = null;
      Flink = null;
      Blink = null;
    }
    public Node(object theElement)
    {
      Element = theElement;
      Flink = null;
      Blink = null;
    }
  }
  public class LinkedList//创建链表中节点之间的链接
 {
    protected Node header;
    public LinkedList()
    {
      header = new Node("header");
    }
    private Node Find(Object item) //查找节点
    {
      Node current = new Node();
      current = header;
      while (current.Element != item)
      {
        current = current.Flink;
      }
      return current;
    }
    public void Insert(Object newItem, Object after) //插入节点
    {
      Node current = new Node();
      Node newNode = new Node(newItem);
      current = Find(after);
      newNode.Flink = current.Flink;
      newNode.Blink = current;
      current.Flink = newNode;
    }
    private Node FindLast()//查找最后一个节点
    {
      Node current = new Node();
      current = header;
      while (!(current.Flink == null) )
      current = current.Flink;
      return current;
    }
    public void Remove(Object n) //移除节点
    {
      Node p = Find(n);
      if (!(p.Flink==null))
      {
        p.Blink.Flink = p.Flink;
        p.Flink.Blink = p.Blink;
        p.Flink = null;
        p.Blink = null;
      }
    }
    public void PrintReverse()//输出节点
    {
      Node current = new Node();
      current = FindLast();
      while (!(current.Blink == null))
      {
        Console.WriteLine(current.Element);
        current = current.Blink;
      }
    }
  }
}

1.2.3、循环链表

带头结点的单循环链表判断空链表的条件是head==head->next
仅设尾指针的单循环链表判断空链表的条件为rear==rear->next
namespace 循环链表
{
  class Program
  {
    static void Main(string[] args)
    {
      LinkedList ld = new LinkedList();
      ld.Insert("ZOU", "header");
      ld.Insert("YONG", "ZOU");
      ld.Insert("LOVE", "YONG");
      ld.Insert("HELLO", "LOVE");
      ld.Insert("WORLD", "HELLO");
      ld.PrintList();
      Console.WriteLine("-----------移除节点以后--------");
      ld.Remove("ZOU");
      ld.Remove("HE");
      ld.PrintList();
      Console.ReadLine();
    }
    public class Node//节点类
    {
      public Object Element;
      public Node Link;
      public Node()
      {
        Element = null;
        Link = null;
      }
      public Node(object theElement)
      {
        Element = theElement;
        Link = null;
      }
    }
    public class LinkedList//创建链表中节点之间的链接
    {
      protected Node header;
      private int count;
      public LinkedList()
      {
        count = 0;
        header = new Node("header");
        header.Link = header;
      }
      public bool IsEmpty()
      {
        return (header.Link == null);
      }
      public void MakeEmpty()
      {
        header.Link = null;
      }
      private Node Find(Object item) //查找节点
      {
        Node current = new Node();
        current = header.Link;
        while (current.Element != item)
        {
          current = current.Link;
        }
        return current;
      }
      public void Insert(Object n1, Object n2) //插入节点
      {
        Node current = new Node();
        Node newNode = new Node(n1);
        current = Find(n2);
        newNode.Link = current.Link;
        current.Link = newNode;
        count++;
      }
      public void InsertFirst(Object n)
      {
        Node current = new Node(n);
        current.Link = header;
        header.Link = current;
        count++;
      }
      private Node FindPrevious(Object n)//查找之前节点
      {
        Node current = header;
        while (!(current.Link == null) && (current.Link.Element != n))
        current = current.Link;
        return current;
      }
      public void Remove(Object n) //移除节点
      {
        Node p = FindPrevious(n);
        if (!(p.Link == null))
        {
          p.Link = p.Link.Link;
        }
        count--;
      }
      public Node Move(int n)
      {
        Node current = header.Link;
        Node temp;
        for (int i = 0; i <= n; i++)
        {
          current = current.Link;
        }
        if (current.Element.ToString()=="header")
        {
          current = current.Link;
        }
        temp = current;
        return temp;
      }
      public Node getFirst()
      {
        return header;
      }
      public void PrintList()//输出节点
      {
        Node current = new Node();
        current = header;
        while (current.Link.Element.ToString()!="header")
        {
        Console.WriteLine(current.Link.Element);
        current = current.Link;
        }
      }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值