110,546
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个大于0的整数\n-->:");
int m = Convert.ToInt32(Console.ReadLine());
Conversion(m);此处
}
//十进制转八进制
public static void Conversion(int n)
{
LinkStack<int> s = new LinkStack<int>();
while (n > 0)
{
s.Push(n % 8);
n = n / 8;
}
while (!s.IsEmpty())
{
n = s.Pop();//取出栈中的值,此处
Console.WriteLine("{0}",n);
}
}
namespace LinkedStack
{
public class LinkStack<T> : IStack<T>
{
private Node<T> top;//栈顶指示器
private int num;//栈中结点的个数,也就是元素的个数
public Node<T> Top
{
get
{
return top;
}
set
{
top = value;
}
}
public int Num
{
get
{
return num;
}
set
{
num = value;
}
}
//构造器
public LinkStack()
{
top = null;
num = 0;
}
#region IStack<T> 成员
public int GetLength()
{
return num;
}
public bool IsEmpty()
{
if ((Top == null) && (num == 0))
{
return true;
}
else
{
return false;
}
}
public void Clear()
{
top=null;
num = 0;
}
public void Push(T item)
{
Node<T> q = new Node<T>(item);
if (top == null)
{
top = q;
}
else
{
q.Next = top;
top = q;
}
}
//出栈
public T Pop()
{
if (IsEmpty())
{
Console.WriteLine("Stack is empty!");
return default(T);
}
Node<T> p = top;
top = top.Next;
--num;
return p.Data;
}
//取栈顶结点的值
public T GetTop()
{
if (IsEmpty())
{
Console.WriteLine("Stack is Empty!");
return default(T);
}
return top.Data;
}
#endregion
}
}