翼度科技»论坛 编程开发 .net 查看内容

C#中的栈与队列/练习

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
C#栈和队列的实现

用双向链表实现一个队列
  1. public class DoubleNode
  2. {
  3.     public int Value;
  4.     public DoubleNode pre;
  5.     public DoubleNode next;
  6.     public DoubleNode(int value)
  7.     {
  8.         this.Value = value;
  9.         this.pre=null;
  10.         this.next=null;
  11.     }
  12. }
  13. public class MyQueue//使用双向链表实现队列
  14. {
  15.     public DoubleNode head;
  16.     public DoubleNode tail;
  17.     public void AddFromHead(DoubleNode node)//从队头插入节点
  18.     {
  19.         if(head == null)
  20.         {
  21.             head = node;
  22.             tail = node;
  23.         }
  24.         else
  25.         {
  26.             node.next = head;
  27.             head.pre = node;
  28.             head = node;
  29.         }
  30.     }
  31.     public void AddFromTail(DoubleNode node)//从队尾插入节点
  32.     {
  33.         if(tail == null)
  34.         {
  35.             tail = node;
  36.             head=node;
  37.         }
  38.         else
  39.         {
  40.             node.pre = tail;
  41.             tail.next = node;
  42.             tail = node;
  43.         }
  44.     }
  45.     public DoubleNode PopFromHead()//从队头弹出节点
  46.     {
  47.         if (head == null) return null;
  48.         DoubleNode res = head;
  49.         if (head==tail)
  50.         {
  51.             head=null;
  52.             tail=null;
  53.         }
  54.         else
  55.         {
  56.             head = head.next;
  57.             res.next=null;
  58.             head.pre=null;
  59.         }
  60.         return res;
  61.     }
  62.     public DoubleNode PopFromTail()//从队尾弹出节点
  63.     {
  64.         if (tail==null) return null;
  65.         DoubleNode res=tail;
  66.         if (head==tail)
  67.         {
  68.             head=null;
  69.             tail=null;
  70.         }
  71.         else
  72.         {
  73.             tail=tail.pre;
  74.             res.pre=null;
  75.             tail.next=null;
  76.         }
  77.         return res;
  78.     }
  79. }
复制代码
使用双向链表实现栈
  1. public class MyStack//使用双向链表实现栈
  2.     {
  3.         public DoubleNode Head;
  4.         public DoubleNode Tail;
  5.         
  6.         public void Push(int value)
  7.         {
  8.             DoubleNode temp = new DoubleNode(value);
  9.             if (Head == null)
  10.             {
  11.                 Head = temp;
  12.                 Tail= temp;
  13.             }
  14.             else
  15.             {
  16.                 temp.next= Head;
  17.                 Head.pre = temp;
  18.                 Head = temp;
  19.             }
  20.         }
  21.         public DoubleNode Pop()
  22.         {
  23.             if (Head == null) return null;
  24.             DoubleNode res = Head;
  25.             if (Head == Tail)
  26.             {
  27.                 Head = null;
  28.                 Tail = null;
  29.                 return res;
  30.             }
  31.             Head = Head.next;
  32.             res.next = null;
  33.             Head.pre = null;
  34.             return res;
  35.         }
  36.         public DoubleNode Peek()
  37.         {
  38.             if (Head == null) return null;
  39.             return Head;
  40.         }
  41.     }
复制代码
使用数组实现固定最大长度的栈和队列
  1. public class MyStack1//使用数组实现固定最大长度的栈,暂定为L
  2. {
  3.      public int[]nums;
  4.      public int index;
  5.      public int limit;
  6.      public MyStack1(int L)
  7.      {
  8.          limit = L;
  9.          nums= new int[limit];
  10.          index=0;
  11.      }
  12.      public void Push(int n)
  13.      {
  14.          if (index<limit)
  15.              nums[index++] = n;
  16.          else
  17.              Console.WriteLine("栈已满");
  18.      }
  19.      public int Pop()
  20.      {
  21.          int res = nums[--index];
  22.          return res;
  23.      }
  24.      public int Peek()
  25.      {
  26.          return nums[index-1];
  27.      }
  28. }
  29. public class MyQueue1//使用数组实现固定最大长度的队列,暂定为L
  30. {
  31.      public int[] nums;
  32.      public int headIndex;
  33.      public int tailIndex;
  34.      public int size;
  35.      public int limit;
  36.      public MyQueue1(int L)
  37.      {
  38.          limit = L;
  39.          nums=new int[limit];
  40.          size=0;
  41.          headIndex=0;
  42.          tailIndex=0;
  43.      }
  44.      public int NextIndex(int i)
  45.      {
  46.          return i<=limit-1? i+1:0;
  47.      }
  48.      public void Push(int n)
  49.      {
  50.         if(size==limit)
  51.          {
  52.              Console.WriteLine("队列已经满了");
  53.              return;
  54.          }
  55.         size++;
  56.          nums[tailIndex] = n;
  57.          tailIndex=NextIndex(tailIndex);
  58.      }
  59.      public int Pop()
  60.      {
  61.          if (size==0)
  62.          {
  63.              throw new Exception("队列空了");
  64.          }
  65.          size--;
  66.          int res = nums[headIndex];
  67.          headIndex=NextIndex(headIndex);
  68.          return res;
  69.      }
  70. }
复制代码
来源:https://www.cnblogs.com/honyide/archive/2023/10/24/17774253.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具