2013-12-17 3 views
1

Я пытаюсь создать Связанный список Stack with Pop и Push функциональность. Метод Push работает, но Pop нет. Я не могу понять, где вернуть значение внутри него, я думаю, что он должен работать, когда это разобрано (извините за плохую формулировку).Связанный список стек Метод pop

Вот мой код:

class Program 
{ 

    int nextFree; 
    int End; 
    int Start; 
    Names[] Stack; 
    Names greg = new Names(); 
    Names matt = new Names(); 
    Names jack = new Names(); 
    Names fred = new Names(); 

    public struct Names 
    { 
     public Int32 pointer; 
     public string data; 
    } 

    static void Main(string[] args) 
    { 

     Program prog = new Program(); 
     do 
     { 
      prog.DisplayMenu(); 
     } 
     while (true); 
    } 

    public void DisplayMenu() 
    { 
     Int32 userInput = 0; 

     Console.WriteLine("Linear Stack"); 
     Console.WriteLine("1: Add to stack"); 
     Console.WriteLine("2: Delete from stack"); 
     userInput = Int32.Parse(Console.ReadLine()); 


     switch (userInput) 
     { 
      case 1: 
       this.Push(); 
       break; 
      case 2: 
       this.Pop(); 
       break; 
     } 

    } 

    public Program() 
    { 
     Stack = new Names[6]; 

     greg.data = "Greg"; 
     greg.pointer = 1; 

     matt.data = "Matt"; 
     matt.pointer = 2; 

     jack.data = "Jack"; 
     jack.pointer = 3; 

     fred.data = "Fred"; 
     fred.pointer = -1; 

     Stack[0] = greg; 
     Stack[1] = matt; 
     Stack[2] = jack; 
     Stack[3] = fred; 
     nextFree = 4; 
     End = 5; 
     Start = 0; 
    } 


    public string Pop() 
    { 

     string value = string.Empty; 

     if (nextFree == -1) 
     { 
      Console.WriteLine("Stack is empty"); 
      Console.ReadLine(); 
     } 
     else 
     { 
      Names thisNode = Stack[End]; 
      int temp = End; 
      End = thisNode.pointer; 
      thisNode.pointer = nextFree; 
      nextFree = temp; 
     } 
     this.ListAllNames(); 
     return value; 
    } 

    public void Push() 
    { 
     if (nextFree >= Stack.Length) 
     { 
      Console.WriteLine("Stackoverflow, to many elements for the stack"); 
      Console.ReadLine(); 
     } 
     else 
     { 
      Console.WriteLine("Enter a name to be added"); 
      string input = Console.ReadLine(); 
      Stack[nextFree].data = input; 
      Stack[nextFree].pointer = End; 
      End = nextFree; 
      nextFree++;  
     } 
      this.ListAllNames(); 
    } 


    public void ListAllNames() 
    { 
     foreach (Names name in Stack) 
     { 
      Console.WriteLine("Name:" + name.data); 
     } 
    } 



} 
} 
+0

Домашнее задание? (Если это так, вы можете сделать еще кое-что, чтобы улучшить свой код). – R0MANARMY

+3

Если это должен быть связанный список, почему вы используете массив? –

+0

@ R0MANARMY Да, я уверен, что есть немало вещей, так как, будем честными, это не так хорошо закодировано: P. Мне просто нужно заставить поп-метод работать, тогда я собираюсь приукрасить его и сделать его более эффективным, если смогу. – user2852418

ответ

1

Я настоятельно рекомендую прочитать статью Eric Lippert's Immutable Stack. Это даст вам несколько действительно интересных советов по реализации.

Вот код из него:

public interface IStack<T> : IEnumerable<T> 
{ 
    IStack<T> Push(T value); 
    IStack<T> Pop(); 
    T Peek(); 
    bool IsEmpty { get; } 
} 

public sealed class Stack<T> : IStack<T> 
{ 
    private sealed class EmptyStack : IStack<T> 
    { 
     public bool IsEmpty { get { return true; } } 
     public T Peek() { throw new Exception("Empty stack"); } 
     public IStack<T> Push(T value) { return new Stack<T>(value, this); } 
     public IStack<T> Pop() { throw new Exception("Empty stack"); } 
     public IEnumerator<T> GetEnumerator() { yield break; } 
     IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } 
    } 
    private static readonly EmptyStack empty = new EmptyStack(); 
    public static IStack<T> Empty { get { return empty; } } 
    private readonly T head; 
    private readonly IStack<T> tail; 
    private Stack(T head, IStack<T> tail) 
    { 
     this.head = head; 
     this.tail = tail; 
    } 
    public bool IsEmpty { get { return false; } } 
    public T Peek() { return head; } 
    public IStack<T> Pop() { return tail; } 
    public IStack<T> Push(T value) { return new Stack<T>(value, this); } 
    public IEnumerator<T> GetEnumerator() 
    { 
     for(IStack<T> stack = this; !stack.IsEmpty ; stack = stack.Pop()) 
      yield return stack.Peek(); 
    } 
    IEnumerator IEnumerable.GetEnumerator() {return this.GetEnumerator();} 
} 
+2

Если он просто ищет любую реализацию «стека», он может использовать реализацию библиотеки. Все дело в том, что у него есть своя реализация, и он хочет знать, что с ним не так. Это не отвечает на это. – Servy

+0

Я так понимаю, я показывал ему пример простого, обоснованного решения для коллекции 'Stack'. Таким образом, он мог бы подумать о том, чтобы изменить его на основе предложенных идей. – Erik

0

Вы не будучи очень объектно-ориентированный. Как Никлаус Вирт отметил:

объектов = Данные + Алгоритмы

Вы должны инкапсулировать вещи, как это: Назначение

public class LinkedListStack<T> 
{ 
    /// <summary> 
    /// indicates whether or not the stack contains data 
    /// </summary> 
    public bool HasData { get { return this.Top != null ; } } 

    /// <summary> 
    /// The topmost stack frame 
    /// </summary> 
    private Node Top { get ; set; } 

    /// <summary> 
    /// constructor 
    /// </summary> 
    public LinkedListStack() 
    { 
    this.Top = null ; 
    return ; 
    } 

    /// <summary> 
    /// constructor: initializes the stack with the provied contents. 
    /// </summary> 
    /// <param name="data"></param> 
    public LinkedListStack(IEnumerable<T> data) : this() 
    { 
    if (data == null) throw new ArgumentNullException("data") ; 
    foreach (T datum in data) 
    { 
     Push(datum) ; 
    } 
    } 

    /// <summary> 
    /// remove the top item from the stack and return it 
    /// </summary> 
    /// <returns></returns> 
    public T Pop() 
    { 
    if (this.Top == null) throw new InvalidOperationException("Can't pop an empty stack!"); 
    Node top = this.Top ; 
    this.Top = top.Next ; 
    return top.Payload ; 
    } 

    /// <summary> 
    /// push an item onto the stack 
    /// </summary> 
    /// <param name="datum"></param> 
    public void Push(T datum) 
    { 
    this.Top = new Node(datum , this.Top) ; 
    return ; 
    } 

    /// <summary> 
    /// private helper class (our stack frame) 
    /// </summary> 
    private class Node 
    { 
    public Node Next ; 
    public T Payload ; 
    public Node(T payload) : this(payload,null) 
    { 
     return ; 
    } 
    public Node(T payload , Node next) 
    { 
     this.Next = next ; 
     this.Payload = payload ; 
     return ; 
    } 
    } 

} 
+0

: s/'this.Next = null;'/'this.Next = next;' – Shanthakumar

+1

@Shanthakumar, вам следует предложить отредактировать, а не комментировать. Это повышает репутацию (см. Http://meta.stackoverflow.com/questions/287956/what-are-so-incentives-for-editing-a-post) –

Смежные вопросы