0

Hi i was wondering if anyone could help me. im stuck in a dead end here. I dont know how to make a peek and working pop function. i need assistance.

here is my code so far:

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);
                }
            }


        }
    }

as you can see it is unfinished. im at a standstill and cannot move one. I have trouble in using the elements here so that i could make a function such as peek and pop properly.

  • 1
    I did a implementation mybe it helps https://codereview.stackexchange.com/questions/138142/linked-list-in-c – fubo Mar 08 '19 at 12:21
  • The best would be to ask your mentor or teacher. Such kind of questions are too broad for SO, unless you can narrow it down to one problem, e.g. if something is not working and you explain what you want and what happens instead. – Sinatr Mar 08 '19 at 12:29
  • Maybe this link might help you out: https://www.geeksforgeeks.org/implementing-stack-c-sharp/ – Alexandre Castro Mar 08 '19 at 13:27
  • The Stack class already implements Push(), Pop() and Peek(), you should use it and not reinvent the wheel : https://learn.microsoft.com/en-us/dotnet/api/system.collections.stack?view=netframework-4.7.2 – bN_ Mar 08 '19 at 13:36

0 Answers0