C# 实现 链表之背包、队列和栈

1.背包(Bag)——不可删掉元素

using UnityEngine;
using System.Collections;
using System.Collections.Generic; //迭代器命名空间

public class Bag<Item> :  IEnumerable<Item>
{   
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public IEnumerator<Item> GetEnumerator()
    {
        Node<Item> current = first;

        while (current != null)
        {
            yield return current.item;
            current = current.next;
        }
    }

    private Node<Item> first;     // top of stack
    private Node<Item> current;     // top of stack
    public static int n;                // size of the stack

    // helper linked list class
    private class Node<Item>
    {
        public Item item;
        public Node<Item> next;
    }


    public Bag()
    {
        
        first = null;
        n = 0;
    }

    public bool isEmpty()
    {
        return first == null;
    }

    public int size()
    {
        return n;
    }

    public void Add(Item item)
    {
        Node<Item> oldfirst = first;
        first = new Node<Item>();
        first.item = item;
        first.next = oldfirst;
        n++;
    }
    
    public Item peek()
    {
        if (isEmpty()) throw new System.Exception("Stack underflow");
        return first.item;
    }







2.队列(Queue)先进先出.......................................................................................


using UnityEngine;
using System.Collections;
using System.Collections.Generic; //迭代器命名空间

public class Queue<Item> :  IEnumerable<Item>
{   
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public IEnumerator<Item> GetEnumerator()
    {
        Node<Item> current = first;
        while (current != null)
        {
            yield return current.item;
            current = current.next;
        }
    }

    private Node<Item> first;     // top of stack
    private Node<Item> last;
    private Node<Item> current;     // top of stack
    public static int n;                // size of the stack

    // helper linked list class
    private class Node<Item>
    {
        public Item item;
        public Node<Item> next;
    }


    public Queue()
    {
        
        first = null;
        n = 0;
    }

    public bool isEmpty()
    {
        return first == null;
    }

    public int size()
    {
        return n;
    }

    public void Enqueue(Item item)
    {
        Node<Item> oldlast = last;

        last = new Node<Item>();
        last.item = item;
        last.next = null;

        if (isEmpty()) first = last;
        else
        oldlast.next = last;
        n++;
    }


    public Item Dequeue()
    {
       
        Item item = first.item;        // save item to return
        first = first.next;            // delete first node
        if (isEmpty()) first = null;
        n--;
        return item;                   // return the saved item
    }

    public Item peek()
    {
        if (isEmpty()) throw new System.Exception("Stack underflow");
        return first.item;
    }

}





3.栈  (Stack)  后进先出........................................................................................


using UnityEngine;
using System.Collections;
using System.Collections.Generic; //迭代器命名空间

public class Stack<Item> :  IEnumerable<Item>
{
   
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public IEnumerator<Item> GetEnumerator()
    {
        Node<Item> current = first;


        while (current != null)
        {
            yield return current.item;
            current = current.next;
        }
    }




    private Node<Item> first;     // top of stack
    private Node<Item> current;     // top of stack
    public static int n;                // size of the stack


    // helper linked list class
    private class Node<Item>
    {
        public Item item;
        public Node<Item> next;
    }


    public Stack()
    {
        
        first = null;
        n = 0;
    }

    public bool isEmpty()
    {
        return first == null;
    }


    public int size()
    {
        return n;
    }

    public void push(Item item)
    {
        Node<Item> oldfirst = first;
        first = new Node<Item>();
        first.item = item;
        first.next = oldfirst;
        n++;
    }

    public Item pop()
    {
        if (isEmpty()) throw new System.Exception("Stack underflow");
        Item item = first.item;        // save item to return
        first = first.next;            // delete first node
        n--;
        return item;                   // return the saved item
    }


    public Item peek()
    {
        if (isEmpty()) throw new System.Exception("Stack underflow");
        return first.item;
    }






   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值