Реализация массива со связанными списками с помощью консоли C # - PullRequest
0 голосов
/ 08 марта 2019

Привет, мне было интересно, кто-нибудь может мне помочь. я застрял здесь в тупике. Я не знаю, как сделать функцию быстрого просмотра. мне нужна помощь

вот мой код:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp13
{
    class Program
    {



        int nextFree;
        int End;
        int Start;

        Names[] Stack;
        Names steven = new Names();
        Names jacques = new Names();
        Names samantha = new Names();
        Names lilly = 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("Enter number of choice:");
            Console.WriteLine("=======================");
            Console.WriteLine("1: Sign up for consultation");
            Console.WriteLine("2: Begin consultation");
            Console.WriteLine("3: Enter room");
            Console.WriteLine("4: Closing time");
            Console.WriteLine("5: Exit");
            userInput = Int32.Parse(Console.ReadLine());


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





                case 5:
                    System.Environment.Exit(1);
                    break;
            }

        }

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

            steven.data = "Steven";
            steven.pointer = 1;

            jacques.data = "Jacques";
            jacques.pointer = 2;

            samantha.data = "Samantha";
            samantha.pointer = 3;

            lilly.data = "Lilly";
            lilly.pointer = -1;




            Stack[0] = steven;
            Stack[1] = jacques;
            Stack[2] = samantha;
            Stack[3] = lilly;
            nextFree = 4;
            End = 20;
            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);
                }
            }


        }
    }

как вы можете видеть, это незаконченное. Я нахожусь в тупике и не могу двигаться. У меня есть проблемы с использованием элементов здесь, чтобы я мог сделать функцию, такую ​​как Peek и Pop правильно.

...